mybatis的通用Mapper函数库以及Example条件函数总结记录

得意时要看淡,失意时要看开。不论得意失意,切莫大意;不论成功失败,切莫止步。志得意满时,需要的是淡然,给自己留一条退路;失意落魄时,需要的是泰然,给自己觅一条出路mybatis的通用Mapper函数库以及Example条件函数总结记录,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

一. 通用Mapper接口方法

1. 查

方法 功能
User selectByPrimaryKey(Integer id) throws SQLException 按主键查询
List selectByExample(UserExample example) throws SQLException 按条件查询,返回类型为List
List selectByExampleWithBLOGs(UserExample example) throws SQLException 按条件查询(包括BLOB字段)。只有当数据表中的字段类型包含二进制的才会产生。
List select(T record) 根据实体中的属性值进行查询,查询条件使用等号
User selectOne(T record) 选择一个查询
List selectOneByExample(UserExample example) 按条件选择一个查询
selectAll() 查询所有
selectCount(T record) 根据实体中的属性查询总数,查询条件使用等号
selectCountByExample() 按条件统计
selectByRowBounds(T record, RowBounds rowBounds) 根据实体属性和RowBounds进行分页查询
selectByExampleAndRowBounds(Object example, RowBounds rowBounds) 根据example条件和RowBounds进行分页查询

2. 插入

方法 功能
String/Integer insert(User record) throws SQLException 保存一个实体,null的属性也会保存,不会使用数据库默认值
String/Integer insertSelective(User record) throws SQLException 保存一个实体,null的属性不会保存,会使用数据库默认值

3. 更新

方法 功能
int updateByPrimaryKey(User record) throws SQLException 按主键更新
int updateByPrimaryKeySelective(User record) throws SQLException 按主键更新值不为Null的字段
int updateByExample(User record, UserExample example) throws SQLException 按条件更新
int updateByExampleSelective(User record, UserExample example) throws SQLException 按条件更新值不为Null的字段

4. 删除

方法 功能
delete()
int deleteByPrimaryKey(Integer id) throws SQLException 按主键删除
int deleteByExample(UserExample example) throws SQLException 按条件删除

二. 条件Example criteria实例方法

Example o = new Example(Entity(实体类名).class);
Example.Criteria criteria = o.createCriteria();

Example

方法 功能
example.setOrderByClause(“字段名 ASC”); 添加升序排列条件,DESC为降序
example.createCriteria() 创建Criteria实例后添加条件
example.setDistinct(boolean distinct) 去除重复,参数为boolean型,true为选择不重复的记录

其余略。

Criteria

方法 功能
andLike(String property, String value) like模糊匹配
andNotLike(String property, String value) 匹配非like条件
andEqualTo(Object param) 等值匹配
andEqualTo(String property, Object value) 等值匹配
andNotEqualTo(String property, Object value) 不等匹配
andBetween(String property, Object value1, Object value2) 字段在between之间匹配
andNotBetween(String property, Object value1, Object value2) between之外匹配
andIn(String property, Iterable values) 等价于SQL中的in条件
andNotIn(String property, Iterable values) not in 匹配
andIsNotNull(String property) 字段非空匹配
andIsNull(String property) 空字段匹配
andLessThan(String property, Object value) 添加小于条件
andLessThanOrEqualTo(String property, Object value) 添加小于等于条件
andGreaterThan(String property, Object value) 添加大于条件
andGreaterThanOrEqualTo(String property, Object value) 添加大于等于条件

参考博客:

  1. MyBatis Mapper3接口大全 :https://blog.csdn.net/wtopps/article/details/70232866
  2. Mybatis之Mapper接口及Example实例函数使用详解:https://blog.csdn.net/qushaming/article/details/83502345

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/157311.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!