环境:springboot2.3.11 + MyBatis3.5.6
依赖
-
org.springframework.boot -
spring-boot-starter-web -
-
org.mybatis.spring.boot -
mybatis-spring-boot-starter -
2.1.4 -
-
com.github.pagehelper -
pagehelper-spring-boot-starter -
1.3.0 -
应用配置
- pagehelper:
- helperDialect: mysql
- reasonable: true
- pageSizeZero: true
- offsetAsPageNum: true
- rowBoundsWithCount: true
- ---
- mybatis:
- type-aliases-package: com.pack.domain
- configuration:
- lazy-loading-enabled: false
- aggressive-lazy-loading: false
- logImpl: org.apache.ibatis.logging.stdout.StdOutImpl
- mapUnderscoreToCamelCase: true
以上配置请查看官方文档说明:
分页配置:
mybatis配置:
启动类添加Mapper扫描
- @SpringBootApplication
- @MapperScan({"com.pack.mapper"})
- public class SpringBootMyBatisApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringBootMyBatisApplication.class, args);
- }
-
- }
添加@MapperScan注解后,就不需要在每个Mapper上添加@Mapper注解了。
核心处理类:
MapperScannerRegistrar.java
- public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
- }
ImportBeanDefinitionRegistrar类的主要功能就是用来动态注册Bean的。很多的第三方库都是通过实现该接口来动态注册Bean,比如EnableFeignClients用来扫描指定包下所有类上带有@FeignClient注解的类。
ImportBeanDefinitionRegistrar需要结合ClassPathBeanDefinitionScanner使用ClassPathBeanDefinitionScanner用来扫描那些类需要被动态注册为Bean。比如Mybatis中使用AutoConfiguredMapperScannerRegistrar来动态注册具有@Mapper注解的类。
Mapper接口定义
- public interface UsersMapper {
-
-
- @Select("SELECT * FROM t_users")
- @Results(id = "users", value = {@Result(property = "name", column = "name", javaType = String.class),
- @Result(property = "password", column = "password")})
- List
findAll() ; -
-
- @Select("SELECT * FROM t_users WHERE id = #{id}")
- @ResultMap("users")
- Users findById(String id) ;
-
-
- @Insert("insert into t_users values(#{id}, #{password}, #{username})")
- int saveUsers(Users users) ;
-
-
- @Update("UPDATE t_users SET username = #{username}, password = #{password} WHERE id = #{id}")
- int updateUsers(Users users) ;
-
-
- @Delete("DELETE FROM t_users where id = #{id}")
- int deleteById(String id) ;
-
-
- @SelectProvider(type = UsersProvider.class, method = "selectLikeUsers")
- @ResultMap("users")
- List
selectLikeUsers(Users users) ; -
-
- @UpdateProvider(type = UsersProvider.class, method = "updateByProvider")
- int updateByProvider(@Param("users") Users users) ;
- }
说明:
@Results注解添加了id属性后,在其他的方法中可以通过@ResultMap("users")来引用。
@*Provider用来动态的定义SQL语句。
- public class UsersProvider {
-
- public String selectLikeUsers(Users users) {
- StringBuilder sql = new StringBuilder("SELECT * FROM t_users t where 1=1 ");
- if(users.getUsername() != null && users.getUsername().trim().length() > 0) {
- sql.append(String.format("and t.username like '%s'", "%" + users.getUsername() + "%"));
- }
- return sql.toString();
- }
-
- public String updateByProvider(@Param("users") Users users) {
- SQL sql = new SQL() ;
- sql.UPDATE("t_users")
- .SET("username = #{users.username}", "password = #{users.password}")
- .WHERE("id = #{users.id}") ;
- return sql.toString() ;
- }
-
- }
单元测试
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class UsersMapperTest {
-
- @Resource
- private UsersMapper usersMapper ;
-
- @Test
- public void testFindAll() {
- Page
page = PageHelper.startPage(1, 2) ; - usersMapper.findAll() ;
- System.out.println(page.getResult()) ;
- System.out.println(page.getTotal() + "\n" + page.getPages()) ;
- }
-
- @Test
- public void testFindById() {
- System.out.println(usersMapper.findById("1")) ;
- }
-
- @Test
- public void testSaveUsers() {
- Users users = new Users() ;
- users.setId("4") ;
- users.setPassword("123456") ;
- users.setUsername("zhangsan") ;
- System.out.println(usersMapper.saveUsers(users)) ;
- }
-
- @Test
- public void testUpdateUser() {
- Users users = new Users() ;
- users.setId("3") ;
- users.setPassword("111111") ;
- users.setUsername("zhangsan") ;
- System.out.println(usersMapper.updateUsers(users)) ;
- }
-
- @Test
- public void testDeleteById() {
- System.out.println(usersMapper.deleteById("3")) ;
- }
-
- @Test
- public void testSelectLikeUsers() {
- Users users = new Users() ;
- users.setUsername("gu") ;
- System.out.println(usersMapper.selectLikeUsers(users)) ;
- }
-
- @Test
- public void testUpdateByProvider() {
- Users users = new Users() ;
- users.setId("2") ;
- users.setPassword("111111") ;
- users.setUsername("guest") ;
- System.out.println(usersMapper.updateByProvider(users)) ;
- }
-
- }
完毕!!!