Spring Boot整合使用xml方式的MyBatis

在官网新建一个spring boot项目,导入到IDE

配置

  • pom文件中添加依赖


    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        1.3.1
    

    
        mysql
        mysql-connector-java
    


  • application.properties文件中配置数据源和mybatis扫描路径和配置文件路径
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.configLocation=classpath:MybatisConfig.xml
mybatis.mapperLocations=classpath:mapping/*.xml
  • 在resources文件夹中新增mapping文件夹和MybatisConfig.xml配置文件

MybatisConfig.xml文件





  • 配置自动扫描
@SpringBootApplication
@MapperScan("com.learn.dao")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

使用MyBatis

  • 在Mysql中创建User表,包含id(BIGINT)、name(VARCHAR)、age(INT)字段。同时,创建映射对象User
public class User {

    private Long id;
    private String name;
    private Integer age;

    // getter和setter

}
  • 创建User映射的操作UserMapper,为了后续单元测试验证,实现插入和查询操作
package com.learn.dao;

public interface UserMapper {

    User findByName(@Param("name") String name);

    int insert(@Param("name") String name, @Param("age") Integer age);
    
}
  • 在mapping文件夹中创建映射文件UserMapper.xml




    

    
      INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})
    


  • 创建单元测试
    • 测试逻辑:插入一条name=AAA,age=20的记录,然后根据name=AAA查询,并判断age是否为20
    • 测试结束回滚数据,保证测试单元每次运行的数据环境独立
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    @Rollback
    public void findByName() throws Exception {
        userMapper.insert("AAA", 20);
        User u = userMapper.findByName("AAA");
        Assert.assertEquals(20, u.getAge().intValue());
    }

}

添加翻页插件

  • pom文件中加入依赖

    
    
        com.github.miemiedev
        mybatis-paginator
        1.2.17
    

  • 在MybatisConfig.xml中加入

  
    
      
    
  

  • UserMapper中新增接口findAll
public interface UserMapper {
    List findAll(PageBounds pageBounds);
}
  • UserMapper.xml中新增映射

    

  • 创建测试用例
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ApplicationTests {

    @Resource
    private UserMapper userMapper;

    @Test
    @Rollback
    public void findAll() throws Exception {
        userMapper.insert("小米", 21);
        userMapper.insert("小兰", 22);
        userMapper.insert("小四", 23);
        userMapper.insert("张三", 24);
        userMapper.insert("李四", 25);
        userMapper.insert("王五", 26);
        userMapper.insert("泽六", 27);
        PageBounds pageBounds = new PageBounds(1, 5, Order.formString("age.desc"));
        PageList users = (PageList) userMapper.findAll(pageBounds);
        System.out.println("--------------------page-------------------------");
        System.out.println(users);
        System.out.println("总数量:" + users.getPaginator().getTotalCount());
        System.out.println("总页数:" + users.getPaginator().getTotalPages());
        System.out.println("当前页:" + users.getPaginator().getPage());
        System.out.println("每页条数:" + users.getPaginator().getLimit());
        System.out.println("--------------------end-------------------------");
    }
}

运行可以看到结果

--------------------page-------------------------
[User{id=58, name='泽六', age=27}, User{id=57, name='王五', age=26}, User{id=56, name='李四', age=25}, User{id=55, name='张三', age=24}, User{id=54, name='小四', age=23}]
总数量:7
总页数:2
当前页:1
每页条数:5
--------------------end-------------------------

项目地址

你可能感兴趣的:(Spring Boot整合使用xml方式的MyBatis)