依赖
正常情况下,在原有依赖基础上增加的 mapper-spring。
1<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring --> 2<dependency> 3 <groupId>tk.mybatis</groupId> 4 <artifactId>mapper-spring</artifactId> 5 <version>1.0.5</version> 6</dependency>
如果想使用其他版本的依赖文件,可以在Maven仓库上搜索“tk.mybatis”。
配置
MapperScannerConfigurer xml
1<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> 2 <property name="basePackage" value="tk.mybatis.mapper.mapper"/> 3 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 4 <property name="properties"> 5 <value> 6 mappers=tk.mybatis.mapper.common.Mapper 7 </value> 8 </property> 9</bean>
@MapperScan 注解
Spring Boot 环境中使用 application.properties] 配置文件
在 Spring Boot 中使用 Mapper 时,如果选择使用注解方式,可以不引入 mapper-starter 依赖。
特别提醒:Spring Boot 中常见的是配置文件方式,使用环境变量或者运行时的参数都可以配置,这些配置都可以对通用 Mapper 生效。
在 propertie 配置中:
1mapper.mappers=tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.Mapper2 2mapper.not-empty=true
tk.mybatis.mapper.session.Configuration 配置
使用要求:MyBatis (3.4.0+) 和 mybatis-spring (1.3.0+)
注意该类的包名,这个类继承了 MyBatis 的 Configuration 类,并且重写了 addMappedStatement 方法,如下:
1@Override 2public void addMappedStatement(MappedStatement ms) { 3 try { 4 super.addMappedStatement(ms); 5 //在这里处理时,更能保证所有的方法都会被正确处理 6 if (this.mapperHelper != null) { 7 this.mapperHelper.processMappedStatement(ms); 8 } 9 } catch (IllegalArgumentException e) { 10 //这里的异常是导致 Spring 启动死循环的关键位置,为了避免后续会吞异常,这里直接输出 11 e.printStackTrace(); 12 throw new RuntimeException(e); 13 } 14}
tk.mybatis.mapper.session.Configuration 提供了 3 种配置通用 Mapper 的方式,如下所示:
1/** 2 * 直接注入 mapperHelper 3 * 4 * @param mapperHelper 5 */ 6public void setMapperHelper(MapperHelper mapperHelper) { 7 this.mapperHelper = mapperHelper; 8} 9 10/** 11 * 使用属性方式配置 12 * 13 * @param properties 14 */ 15public void setMapperProperties(Properties properties) { 16 if (this.mapperHelper == null) { 17 this.mapperHelper = new MapperHelper(); 18 } 19 this.mapperHelper.setProperties(properties); 20} 21 22/** 23 * 使用 Config 配置 24 * 25 * @param config 26 */ 27public void setConfig(Config config) { 28 if (mapperHelper == null) { 29 mapperHelper = new MapperHelper(); 30 } 31 mapperHelper.setConfig(config); 32}
这里直接配置一个 tk 中提供的 Configuration,然后注入到 SqlSessionFactoryBean 中。
使用 tk.mybatis.mapper.session.Configuration 和 Spring 集成
1@Bean 2public SqlSessionFactory sqlSessionFactory() throws Exception { 3 SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); 4 sessionFactory.setDataSource(dataSource()); 5 //创建 Configuration,设置通用 Mapper 配置 6 tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration(); 7 //有 3 种配置方式 8 configuration.setMapperHelper(new MapperHelper()); 9 sessionFactory.setConfiguration(configuration); 10 11 return sessionFactory.getObject(); 12}
代码演示
创建一个超类接口,继承通用mapper内部的所有方法,然后就可以直接调用了。
1public interface BaseMapper<T> extends InsertSelectiveMapper<T>, UpdateByExampleSelectiveMapper<T>, UpdateByPrimaryKeySelectiveMapper<T>, 2 SelectOneMapper<T>, SelectByPrimaryKeyMapper<T>, SelectMapper<T>, SelectByExampleMapper<T>, SelectByExampleRowBoundsMapper<T>, 3 SelectCountByExampleMapper<T> {}
下面介绍一下通用Mapper的内置方法
countByExample --- 根据条件查询数量
1int countByExample(UserExample example); 2//完整案例 3UserExample example=new UserExample(); 4Criteria criteria = example.createCriteria(); 5criteria.andAgeEqualTo(23); 6int count=userDAO.countByExample(example); 7//相当于:select count(*) from user where age=23;
deleteByExample --- 根据条件删除多条
1int deleteByExample(AccountExample example); 2 3//完整的案例 4 5UserExample example = new UserExample(); 6 7 Criteria criteria = example.createCriteria(); 8 9 criteria.andUsernameEqualTo("joe"); 10 11 userDAO.deleteByExample(example); 12 13 //相当于:delete from user where username='joe'
deleteByPrimaryKey ---根据主键删除
1int deleteByPrimaryKey(Integer id); 2 3//完整案例 4userDAO.deleteByPrimaryKey(101); 5 6//相当于:delete from user where id=101
insertSelective --- 插入数据
1int insertSelective(Account record); 2//完整的案例 3User user = new User(); 4user.setUsername("test"); 5user.setPassword("123456") 6user.setEmail("674531003@qq.com"); 7userDAO.insertSelective(user); 8//相当于:insert into user(username,password,email) values('test','123456','674531003@qq.com');
selectByExample --- 根据条件查询数据
1List<Account> selectByExample(AccountExample example); 2//完整的案例 3UserExample example = new UserExample(); 4Criteria criteria = example.createCriteria(); 5criteria.andUsernameEqualTo("joe"); 6criteria.andUsernameIsNull(); 7example.setOrderByClause("username asc,email desc"); 8List<?> list = userDAO.selectByExample(example); 9//相当于:select * from user where username = 'joe' and username is null order by username asc,email desc 10//注:在myBatis 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。
selectByPrimaryKey --- 根据主键查询数据
1Account selectByPrimaryKey(Integer id); 2//相当于select * from user where id = id
updateByExampleSelective --- 按条件更新值不为null的字段
1int updateByExampleSelective(@Param("record") Account record, @Param("example") AccountExample example); 2 //完整的案列 3UserExample example = new UserExample(); 4Criteria criteria = example.createCriteria(); 5criteria.andUsernameEqualTo("joe"); 6 User user = new User(); 7user.setPassword("123"); userDAO.updateByPrimaryKeySelective(user,example); 8//相当于:update user set password='123' where username='joe'
updateByPrimaryKeySelective --- 根据主键更新
1int updateByPrimaryKeySelective(Account record); 2 //完整的案例 3User user = new User(); 4user.setId(101); 5user.setPassword("joe"); 6userDAO.updateByPrimaryKeySelective(user); 7//相当于:update user set password='joe' where id=101
最后补一张从网上盗的关于Example的图
