通用Mapper

概述

通用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之间条件
三、使用实例

查询

  1. selectByPrimaryKey() //按主键查询
//相当于:select * from user where id = 100;
User user = UserMapper.selectByPrimaryKey(100); 
  1. 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);
  1. updateByPrimaryKeySelective()
//相当于:update user set password='970316' where id='123'
User user = new User();
user.setId("123");
user.setPassword("970316");
XxxMapper.updateByPrimaryKey(user);
  1. 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); 
  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙霸天

你的打赏是我不断创作的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值