概述
通用Mapper都可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。
极其方便的使用MyBatis单表的增删改查。
支持单表操作,不支持通用的多表联合查询。
使用
如果你使用 Maven,只需要添加如下依赖:
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
一、方法解析
方法 | 功能 |
---|---|
int countByExample(UserExample example) thorws SQLException | 按条件计数 |
int deleteByPrimaryKey(Integer id) thorws SQLException | 按主键删除 |
int deleteByExample(UserExample example) thorws SQLException | 按条件查询 |
String/Integer insert(User record) thorws SQLException | 插入数据(返回值为ID) |
User selectByPrimaryKey(Integer id) thorws SQLException | 按主键查询 |
List selectByExample(UserExample example) thorws SQLException | 按条件查询 |
List selectByExampleWithBLOGs(UserExample example) thorws SQLException | 按条件查询(包括BLOB字段)只有当数据表中的字段类型有为二进制的才会产生 |
int updateByPrimaryKey(User record) thorws SQLException | 按主键更新 |
int updateByPrimaryKeySelective(User record) thorws SQLException | 按主键更新值不为null的字段 |
int updateByExample(User record, UserExample example) thorws SQLException | 按条件更新 |
int updateByExampleSelective(User record, UserExample example) thorws SQLException | 按条件更新值不为null的字段 |
二、Example实例
mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分
import tk.mybatis.mapper.entity.Example;
Example example = new Example(JavaBean.class);
Example.Criteria criteria = example.createCriteria();
方法 | 说明 |
---|---|
example.setOrderByClause(“字段名 ASC”); | 添加升序排列条件,DESC为降序 |
example.setDistinct(false) | 去除重复,boolean型,true为选择不重复的记录 |
criteria.andXxxIsNull | 添加字段xxx为null的条件 |
criteria.andXxxIsNotNull | 添加字段xxx不为null的条件 |
criteria.andXxxEqualTo(value) | 添加xxx字段等于value条件 |
criteria.andXxxNotEqualTo(value) | 添加xxx字段不等于value条件 |
criteria.andXxxGreaterThan(value) | 添加xxx字段大于value条件 |
criteria.andXxxGreaterThanOrEqualTo(value) | 添加xxx字段大于等于value条件 |
criteria.andXxxLessThan(value) | 添加xxx字段小于value条件 |
criteria.andXxxLessThanOrEqualTo(value) | 添加xxx字段小于等于value条件 |
criteria.andXxxIn(List<?>) | 添加xxx字段值在List<?>条件 |
criteria.andXxxNotIn(List<?>) | 添加xxx字段值不在List<?>条件 |
criteria.andXxxLike(“%”+value+”%”) | 添加xxx字段值为value的模糊查询条件 |
criteria.andXxxNotLike(“%”+value+”%”) | 添加xxx字段值不为value的模糊查询条件 |
criteria.andXxxBetween(value1,value2) | 添加xxx字段值在value1和value2之间条件 |
criteria.andXxxNotBetween(value1,value2) | 添加xxx字段值不在value1和value2之间条件 |
三、使用实例
查询
- selectByPrimaryKey() //按主键查询
//相当于:select * from user where id = 100;
User user = UserMapper.selectByPrimaryKey(100);
- selectByExample() //使用example条件查询
// 相当于 select * from table where type = ‘手机’ and itemPrice >= 1000 and itemPrice <= 3000 order by item_price desc
Example example = new Example(jdModel.getClass());
//where条件
Example.Criteria criteria = example.createCriteria();
if (type != null) {
criteria.andEqualTo("type", 手机);
}
if (first != null && end != null) {
f = Integer.valueOf(first);
e = Integer.valueOf(end);
if (f >= 0 && e >= f) {
criteria.andGreaterThan("itemPrice", 1000);
criteria.andLessThan("itemPrice", 3000);
}
}
example.setOrderByClause("item_price desc");//("username asc,email desc")
List<JdModel> list = generic_jdMapper.selectByExample(example);//使用通用mapper
插入数据
3. insert()
//相当于:insert into user(ID,username,password,email) values
//('123','sun','970316','1327398885@qq.com');
User user = new User();
user.setId("123");
user.setUsername("sun");
user.setPassword("970316")
user.setEmail("1327398885@qq.com");
XxxMapper.insert(user);
更新数据
4. updateByPrimaryKey()
//相当于:update user set username='sun', password='970316',
//email='1327398885@qq.com' where id='123'
User user =new User();
user.setId("123");
user.setUsername("sun");
user.setPassword("970316");
user.setEmail("1327398885@qq.com");
XxxMapper.updateByPrimaryKey(user);
- updateByPrimaryKeySelective()
//相当于:update user set password='970316' where id='123'
User user = new User();
user.setId("123");
user.setPassword("970316");
XxxMapper.updateByPrimaryKey(user);
- updateByExample() 和 updateByExampleSelective()
//相当于:update user set password='970316' where username='sun'
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("sun");
User user = new User();
user.setPassword("970316");
XxxMapper.updateByPrimaryKeySelective(user,example);
删除
7. deleteByPrimaryKey()
//相当于:delete from user where id=1
XxxMapper.deleteByPrimaryKey(1);
- deleteByExample()
//相当于:delete from user where username='sun'
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("sun");
XxxMapper.deleteByExample(example);
查询数据数量
9. countByExample()
//相当于:select count(*) from user where username='sun'
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("sun");
int count = XxxMapper.countByExample(example);
参考-https://blog.csdn.net/tian_ci/article/details/85244768