通用Mapper的增删改查
标签搜索

通用Mapper的增删改查

ChencyCT
2024-03-23 / 0 评论 / 3 阅读 / 正在检测是否收录...

什么是通用Mapper

通用Mapper是一个辅助mybatis开发的组件,让mybatis的开发更方便,可以自动生成sql语句。

通用Mapper的常用方法

查询相关

List<T> selct(T record);  //根据实体中的属性值进行查询
T selectByParimaryKey(Object key);  //更具主键字段进行查询,方法参数必须包含完整的主键属性
List<T> selectAll(); //查询全部结果

T selectOne(T record); //根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常

int selectCount(T record); //根据实体的属性查询总数

添加相关

int insert(T record);//保存一个实体,null的属性也会保存,不会使用数据库默认值
int insertSelective(T record);//保存一个实体,null的属性不会保存,会使用数据库默认值

修改相关

int updateByPrimaryKey(T record);//根据主键更新全部字段,null会被更新
int updateByPrimaryKeySelective(T record);//根据主键更新属性不为null的值

删除相关

int delete(T record);//根据实体属性作为条件进行删除
int deleteByPrimaryKey(Object key);//根据主键字段进行删除

Example

List<T> selectByExample(Object example);//根据Example条件进行查询,这个查询通过Example类指定查询列,通过selectProperties方法指定列

int selectCountByExample(Object example);//根据Example条件进行查询总数

int updateByExample(@Param("record")T record,@Param("example")Object example);//根据Example条件更新实体包含的全部属性,null值会被更新

int updateByExampleSelective(@Param("record")T record,@Param("example")Object example);//根据Example条件更新实体包含的全部属性,null值会被更新

int deleteByExample(Object example);//根据Example条件删除数据

Example使用方法

Example example = new Example(需要CRUD(与数据库对应实体类).class)
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id",id);//根据已知条件判断

设置条件:
example.orderBy(字段名).asc();  //添加升序排列条件
example.orderBy(字段名).desc();  //添加降序排序条件
example.setDistinct(false);//去除重复,boolean型,true为选择不重复的记录
criteria.andIsNull(字段名);//添加字段xx为null的条件
criteria.andIsNotNull(字段名);//添加字段xx不为byll的条件
criteria.andEqualTo(字段名,value);//添加字段xx等value的条件
criteria.andNotEqualTo(字段名,value);//添加字段xx不等于value的条件
criteria.andGreaterThan(字段名,value);//添加xx字段大于value条件
criteria.andGreaterThanOrEqualTo(字段名,value);//添加xx字段大于等于value条件
criteria.andLessThan(字段名,value);//添加xx字段小于value条件
criteria.andLessThanOrEqualTo(字段名,value);//添加xx字段小于等于value的条件
criteria.andIn(字段名,list);//添加字段值存在list条件
criteria.andNotIn(字段名,list);//添加xx字段值不存在list条件
criteria.andLike(字段名,"%"+value+"%");//添加xx字段值为value的模糊查询
criteria.andNotLike(字段名,"%"+value+"%");//添加xx字段值不为value的模糊查询
criteria.andBetween(字段名,value1,value2);//添加字段值在value1和value2之间的条件
criteria.andNotBetween(字段名,value1,value2);//添加字段值不在value1和value2之间的条件
0

评论 (0)

取消