通用Mapper基于Mybatis的插件。开发人员不需要编写SQL,只要写好实体类,就能支持相应的单表增删改查,根据ids批量查询、新增、删除方法。
直接上代码
Maven依赖:
<!-- 通用Mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.9</version>
</dependency>
SpringMVC配置:
<!-- mybatis 通用 mapper 整合-->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="markerInterface" value="com.example.dao.BaseDao"/>
<!-- 通用Mapper通过属性注入进行配置,默认不配置时会注册Mapper<T>接口 -->
<property name="properties">
<value>
<!--配置参数-->
ORDER=BEFORE
</value>
</property>
</bean>
可配属性参数介绍:
UUID:设置生成UUID的方法,需要用OGNL方式配置,不限制返回值,但是必须和字段类型匹配
IDENTITY:取回主键的方式
ORDER:seletKey中的order属性,可选值为BEFORE和AFTER
catalog:数据库的catalog,如果设置该值,查询的时候表名会带catalog设置的前缀
schema:同catalog,catalog优先级高于schema
seqFormat:序列的获取规则,使用{num}格式化参数,默认值为 {0}.nextval,针对Oracle,可选参数一共4个,对应0,1,2,3分别为 SequenceName,ColumnName, PropertyName,TableName
notEmpty:insert和update中,是否判断字符串类型!=’’,少数方法会用到
style:实体和表转换时的规则,默认驼峰转下划线,可选值为normal用实体名和字段名;camelhump是默认值,驼峰转下划线;uppercase转换为大写;lowercase转换为小写
创建DAO层:
package com.example.dao;
import com.example.entity.DemoUser;
public interface DemoUserMapper extends BaseDao<DemoUser> {
}
每个dao都必须继承BaseDao
创建一个公共的接口继承Mapper:
package com.example.dao;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.ids.DeleteByIdsMapper;
import tk.mybatis.mapper.common.ids.SelectByIdsMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;
public interface BaseDao<T> extends Mapper<T>, InsertListMapper<T>, SelectByIdsMapper<T>, DeleteByIdsMapper<T> {
}
Mapper接口:通用mapper,继承了BaseMapper, ExampleMapper, RowBoundsMapper三个组合接口。
InsertListMapper接口:批量插入,支持批量插入的数据库可以使用,另外该接口限制实体包含id属性并且必须为自增列。
SelectByIdsMapper接口:通过操作ids字符串进行查询,ids 如 “1,2,3” 这种形式的字符串。
DeleteByIdsMapper接口:根据主键字段进行删除,方法参数必须包含完整的主键属性,通过操作ids字符串进行操作,ids 如 “1,2,3” 这种形式的字符串
继承的Mapper就拥有了Mapper所有的通用方法:
/**
* 根据主键查询
*
*/
T selectByPrimaryKey(Object var1);
/**
* 根据实体中字段值进行查询
*/
List<T> select(T var1);
/**
* 通过id批量查询
* <p>
* 通过操作ids字符串进行操作,ids 如 "1,2,3" 这种形式的字符串,
* <p>
* 这个方法要求实体类中有且只有一个带有@Id注解的字段,否则会抛出异常。
*/
List<T> selectByIds(String var1);
/**
* 根据Example条件进行查询
*
* @param example 任意类型参数
*/
List<T> selectByExample(Object var1);
/**
* 保存一个实体,null的属性也会保存,不会使用数据库默认值
*/
int insert(T var1);
/**
* 保存一个实体,null的属性不会保存,会使用数据库默认值
*/
int insertSelective(T var1);
/**
* 批量插入,支持批量插入的数据库可以使用,
* 例如MySQL,H2等,
* 另外该接口限制实体包含id属性并且必须为自增列
*
*/
int insertList(List<T> var1);
/**
* 根据主键字段进行删除,方法参数必须包含完整的主键属性
*/
int deleteByPrimaryKey(Object var1);
/**
* 根据主键字段进行批量删除,方法参数必须包含完整的主键属性
* 通过操作ids字符串进行操作,ids 如 "1,2,3" 这种形式的字符串
*/
@Override
int deleteByIds(String var1);
/**
* 根据实体属性作为条件进行删除,查询条件使用等号
*/
int delete(T var1);
/**
* 根据主键更新实体全部字段,null值会被更新
* (对象中必须有主键id)
*/
int updateByPrimaryKey(T var1);
/**
* 根据主键更新属性不为null的值
* (对象中必须有主键id)
*/
int updateByPrimaryKeySelective(T var1);
这里展示一部分方法,需要别的方法可以看api。
在service层中使用:
package com.example.service.impl;
import com.example.dao.DemoUserMapper;
import com.example.entity.DemoUser;
import com.example.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
public class DemoServiceImpl implements DemoService {
@Autowired
DemoUserMapper demoUserMapper;
public void save(DemoUser user){
demoUserMapper.insert(user);
}
}
最后这是项目结构:
