mybatis之generator插件中example和record使用

一、Record

一个Record是一个Dao对象(继承Mapper接口),tkmybatis会将record自动映射成sql语句,record中所有非null的属性都作为sql语句,如:

image

映射的sql文如下:

SELECT eigyousyo_id,goods_id,goods_kind_id,goods_management_id,goods_management_name,reserve1,reserve2,reserve3,reserve4,sort_no,update_author,update_time,version
FROM mst_GoodsInPad
WHERE eigyousyo_id = ?

Mapper接口中的方法:

image

Mapper接口中部分方法的解析:

方法 功能说明
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

Example对象允许在sql操作时手动指定where 条件:

image

映射的sql文会自动生成where 条件:
WHERE ( goods_code = goodsFromTable.getGoodsCode() )
Example的实例函数如下:

方法 说明
example.setOrderByClause(“字段名 ASC”); 添加升序排列条件,DESC为降序
example.setDistinct(false) 去除重复,boolean型,true为选择不重复的记录。
criteria.andIsNull 添加字段为null的条件
criteria.andIsNotNull 添加字段不为null的条件
criteria.andEqualTo(value) 添加字段等于value条件
criteria.andNotEqualTo(value) 添加字段不等于value条件
criteria.andGreaterThan(value) 添加字段大于value条件
criteria.andGreaterThanOrEqualTo(value) 添加字段大于等于value条件
criteria.andLessThan(value) 添加字段小于value条件
criteria.andLessThanOrEqualTo(value) 添加字段小于等于value条件
criteria.andIn(List<?>) 添加字段值在List<?>条件
criteria.andNotIn(List<?>) 添加字段值不在List<?>条件
criteria.andLike(“%”+value+”%”) 添加字段值为value的模糊查询条件
criteria.andNotLike(“%”+value+”%”) 添加字段值不为value的模糊查询条件
criteria.andBetween(value1,value2) 添加字段值在value1和value2之间条件
criteria.andNotBetween(value1,value2) 添加字段值不在value1和value2之间条件

本文来源于:https://www.cnblogs.com/vickylinj/p/9486098.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。