tk.mybatis3.0

简介

1、通用mapper只适用于单表操作
2、Mapper3.0接口有两种形式,一种是提供了一个方法的接口。还有一种是不提供方法,但是继承了多个单方法的接口,一般是某类方法的集合。

例如SelectMapper是一个单方法的接口,BaseSelectMapper是一个继承了4个基础查询方法的接口。

基础接口

  1. Select

    1、接口:SelectMapper<T>
    方法:List<T> select(T record);
    说明:根据实体中的属性值进行查询,查询条件使用等号
    
    2、接口:SelectByPrimaryKeyMapper<T>
    方法:T selectByPrimaryKey(Object key);
    说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
    
    3、接口:SelectAllMapper<T>
    方法:List<T> selectAll();
    说明:查询全部结果,select(null)方法能达到同样的效果
    
    4、接口:SelectOneMapper<T>
    方法:T selectOne(T record);
    说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
    
    5、接口:SelectCountMapper<T>
    方法:int selectCount(T record);
    说明:根据实体中的属性查询总数,查询条件使用等号
    
  2. Insert

    1、接口:InsertMapper<T>
     方法:int insert(T record);
     说明:保存一个实体,null的属性也会保存,不会使用数据库默认值
    
    2、接口:InsertSelectiveMapper<T>
     方法:int insertSelective(T record);
     说明:保存一个实体,null的属性不会保存,会使用数据库默认值
    
  3. Update

    1、接口:UpdateByPrimaryKeyMapper<T>
    方法:int updateByPrimaryKey(T record);
    说明:根据主键更新实体全部字段,null值会被更新
    
    2、接口:UpdateByPrimaryKeySelectiveMapper<T>
    方法:int updateByPrimaryKeySelective(T record);
    说明:根据主键更新属性不为null的值
    
  4. Delete

    1、接口:DeleteMapper<T>
    方法:int delete(T record);
    说明:根据实体属性作为条件进行删除,查询条件使用等号
    
    2、接口:DeleteByPrimaryKeyMapper<T>
    方法:int deleteByPrimaryKey(Object key);
    说明:根据主键字段进行删除,方法参数必须包含完整的主键属性
    

组合接口

  1. base组合接口

    1、接口:BaseSelectMapper<T>
    方法:包含上面Select的4个方法
    
    2、接口:BaseInsertMapper<T>
    方法:包含上面Insert的2个方法
    
    3、接口:BaseUpdateMapper<T>
    方法:包含上面Update的2个方法
    
    4、接口:BaseDeleteMapper<T>
    方法:包含上面Delete的2个方法
    
  2. CRUD组合接口

    接口:BaseMapper<T>
    方法:继承了base组合接口中的4个组合接口,包含完整的CRUD方法
    

**

Example方法

**

1、接口:SelectByExampleMapper<T>
方法:List<T> selectByExample(Object example);
说明:根据Example条件进行查询
重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列

2、接口:SelectCountByExampleMapper<T>
方法:int selectCountByExample(Object example);
说明:根据Example条件进行查询总数

3、接口:UpdateByExampleMapper<T>
方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
说明:根据Example条件更新实体record包含的全部属性,null值会被更新

4、接口:UpdateByExampleSelectiveMapper<T>
方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
说明:根据Example条件更新实体record包含的不是null的属性值

5、接口:DeleteByExampleMapper<T>
方法:int deleteByExample(Object example);
说明:根据Example条件删除数据

Example组合接口

  1. 组合接口

    接口:ExampleMapper<T>
    方法:包含上面Example中的5个方法
    

Condition方法

Condition方法和Example方法作用完全一样,只是为了避免Example带来的歧义,提供的的Condition方法

1、接口:SelectByConditionMapper<T>
方法:List<T> selectByCondition(Object condition);
说明:根据Condition条件进行查询

2、接口:SelectCountByConditionMapper<T>
方法:int selectCountByCondition(Object condition);
说明:根据Condition条件进行查询总数

3、接口:UpdateByConditionMapper<T>
方法:int updateByCondition(@Param("record") T record, @Param("example") Object condition);
说明:根据Condition条件更新实体record包含的全部属性,null值会被更新

4、接口:UpdateByConditionSelectiveMapper<T>
方法:int updateByConditionSelective(@Param("record") T record, @Param("example") Object condition);
说明:根据Condition条件更新实体record包含的不是null的属性值

5、接口:DeleteByConditionMapper<T>
方法:int deleteByCondition(Object condition);
说明:根据Condition条件删除数据

Condition组合接口

接口:ConditionMapper<T>
方法:包含上面Condition中的5个方法

RowBounds

默认为内存分页,可以配合PageHelper实现物理分页

1、接口:SelectRowBoundsMapper<T>
方法:List<T> selectByRowBounds(T record, RowBounds rowBounds);
说明:根据实体属性和RowBounds进行分页查询

2、接口:SelectByExampleRowBoundsMapper<T>
方法:List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds);
说明:根据example条件和RowBounds进行分页查询

3、接口:SelectByConditionRowBoundsMapper<T>
方法:List<T> selectByConditionAndRowBounds(Object condition, RowBounds rowBounds);
说明:根据example条件和RowBounds进行分页查询,该方法和selectByExampleAndRowBounds完全一样,只是名字改成了Condition

RowBounds组合接口

接口:RowBoundsMapper<T>
方法:包含上面RowBounds中的前两个方法,不包含selectByConditionAndRowBounds

special特殊接口

注意:这些接口针对部分数据库设计,不是所有数据库都支持

接口:InsertListMapper<T>
方法:int insertList(List<T> recordList);
说明:批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含id属性并且必须为自增列

接口:InsertUseGeneratedKeysMapper<T>
方法:int insertUseGeneratedKeys(T record);
说明:插入数据,限制为实体包含id属性并且必须为自增列,实体配置的主键策略无效
  1. MySQL专用

    接口:MySqlMapper<T>
    继承方法:int insertList(List<T> recordList);
    继承方法:int insertUseGeneratedKeys(T record);
    说明:该接口不包含方法,继承了special中的InsertListMapper<T>和InsertUseGeneratedKeysMapper<T>
    
  2. SqlServer专用

由于sqlserver中插入自增主键时,不能使用null插入,不能在insert语句中出现id。

注意:
1、SqlServer的两个特有插入方法都使用了
@Options(useGeneratedKeys = true, keyProperty = "id")
这就要求表的主键为id,且为自增,如果主键不叫id可以看高级教程中的解决方法。
2、另外这俩方法和base中的插入方法重名,不能同时存在! 如果某种数据库和SqlServer这里类似,也可以使用这些接口(需要先测试)。

1、接口:InsertMapper
方法:int insert(T record);
说明:插入数据库,null值也会插入,不会使用列的默认值

2、接口:InsertSelectiveMapper
方法:int insertSelective(T record);
说明:插入数据库,null的属性不会保存,会使用数据库默认值

3、接口:SqlServerMapper
说明:这是上面两个接口的组合接口。

Ids接口

通过操作ids字符串进行操作,ids 如 “1,2,3” 这种形式的字符串,这个方法要求实体类中有且只有一个带有@Id注解的字段,否则会抛出异常。

接口:SelectByIdsMapper
方法:List<T> selectByIds(String ids)
说明:根据主键字符串进行查询,类中只有存在一个带有@Id注解的字段

接口:DeleteByIdsMapper
方法:int deleteByIds(String ids)
说明:根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段

Ids组合接口

接口:IdsMapper<T>
方法:包含上面Ids中的前两个方法

Mapper接口

接口:Mapper<T>
该接口兼容Mapper2.x版本,继承了BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>三个组合接口。

Example方法的使用

主要参考:https://www.liangzl.com/get-article-detail-27354.html
参考:https://blog.csdn.net/qq_35174296/article/details/81389017

如下列举四种方式,但是不止四种哦。
注意:其中weekend方式需要升级jdk到1.8及以上。

  • 方法表:
说明方法
example.setOrderByClause(“字段名 ASC”)添加升序排列条件,DESC为降序
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的模糊查询条件

接下来就是实现example查询的几种方式,核心代码如下:

解释:MybatisDemo 是数据库表的映射类即entity类 brandEntityMapper:对应的mapper文件

  • 方式一:普通Example方式(从and方法开始可以实现动态sql拼接)

     Example example = new Example(CandidateBrandEntity.class);
         example
             	.and().andEqualTo("cabDeleted",0)
             	.andLike("cabName","%d%");
         example.orderBy("cabCreatedTime").desc();   
         
         // 获得结果  
         List<CandidateBrandEntity> brands = brandEntityMapper.selectByExample(example);
    
  • 方式二:Criteria方式(可使用criteria完成动态sql拼接)

     Example example = new Example(MybatisDemo.class);
     Example.Criteria criteria = example.createCriteria();
     criteria.andEqualTo("count", 0)
             .andLike("name", "%d%");
     example.orderBy("count").desc()
     //获得结果
     List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(example);
    
  • 方式三:Example.builder 方式(其中where从句中内容可以拿出来进行动态sql拼接)

     Example.Builder example = Example.builder(MybatisDemo.class)
             .select("cabId","cabName")
             .where(Sqls.custom().andEqualTo("count", 0)
             .andLike("name", "%d%"))
             .orderByDesc("count","name")
             .build();
             获得结果
     List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(example);
    
  • 方式四:Example.builder + Weekend方式,优势:不用输入属性名,避免数据库有变动或输入错误就会出错

     Example.Builder example = Example.builder(MybatisDemo.class);
     //获得seekendsql
     WeekendSqls<MybatisDemo> sqls = WeekendSqls.<MybatisDemo>custom();
     
     //可进行动态sql拼接
     sqls = sqls.andEqualTo(MybatisDemo::getCount,0).andLike(MybatisDemo::getName,"%d%");
     
     //获得结果
     List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(example.where(sqls).orderByDesc ("count","name"));
    
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值