Spring boot (III) integration with mybatis

Keywords: Java Spring Mybatis xml Druid

Preface

With the release of spring boot2.0. The API interface of the project team has considered the transformation to spring boot. We always use mybatis as the underlying interface, so in this article, I specially practiced the integration of mybatis in spring boot.

 

1, Preparations

1,pom.xml

 

 1  <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot</artifactId>
 5             <version>2.0.0.RELEASE</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.springframework.boot</groupId>
 9             <artifactId>spring-boot-starter-web</artifactId>
10             <version>2.0.0.RELEASE</version>
11         </dependency>
12         <dependency>
13             <groupId>com.microsoft.sqlserver</groupId>
14             <artifactId>sqljdbc4</artifactId>
15             <version>4.0</version>
16         </dependency>
17         <dependency>
18             <groupId>org.mybatis.spring.boot</groupId>
19             <artifactId>mybatis-spring-boot-starter</artifactId>
20             <version>1.3.2</version>
21         </dependency>
22         <dependency>
23             <groupId>com.alibaba</groupId>
24             <artifactId>druid</artifactId>
25             <version>1.1.9</version>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-test</artifactId>
30             <version>2.0.0.RELEASE</version>
31             <scope>test</scope>
32         </dependency>
33     </dependencies>

 

2. Project structure

The configuration file is still in the resources directory, and the spring boot supports properties and yml.

 

 

 

3. Using annotation to write UserMapper

public interface UserDao {

    @Select("select * from tb_user")
    List<User> getAllUsers();

    @Select("select * from tb_user where id=#{id}")
    User getById(int id);

    @Insert("insert into tb_user(name,address) values(#{name},#{address})")
    void insert(User user);

    @Update("update tb_user set name=#{name},address=#{address} where id=#{id}")
    void update(User user);

    @Delete("delete from tb_user where id=#{id}")
    void delete(int id);
}

Start the Application.java class. It is mainly MapperScan annotation. Configure the mapping package directory com.che168.dao

@SpringBootApplication
@MapperScan("com.che168.dao")
public class Application {

    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }
}

Unit test

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {

    @Autowired
    UserDao userDao;

    @Test
    public void getAllUsers(){
        List<User> allUsers=userDao.getAllUsers();
        System.out.println(allUsers.size());
    }

    @Test
    public void getById(){
        int id=1;
        User model=userDao.getById(id);

        System.out.println("name:"+model.getName()+",address:"+model.getAddress());
    }

    @Test
    public void insert(){
        User user=new User();
        user.setName("Yunlong");
        user.setAddress("Taiyuan, Shanxi");

        userDao.insert(user);
    }

    @Test
    public void update(){
        User user=new User();
        user.setId(4);
        user.setName("Yunlong");
        user.setAddress("Yuncheng, Shanxi");

        userDao.update(user);
    }

    @Test
    public void delete(){
        userDao.delete(4);
    }

}

 

2, How to use a profile

The way of configuration file is basically the same as the way we integrated in spring MVC before. Put XML file in mapper directory separately, put all sql related operations in XML, interface is used to write method signature, and then configure mapping. In spring The difference in boot is that mapper-config.xml and entity class mapping files need to be configured in application.properties. The following configuration is not covered here.

mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

Posted by ntnwwnet on Tue, 31 Mar 2020 02:57:27 -0700