《spring boot 整合 tk.Mybatis 》
《tk.mybatis官方手册》
tk.mybatis项目中提供了大量现成的方法,这些方法可以作为扩展时的参考。
例如 tk.mybatis.mapper.common.ids
包中的 批量删除接口DeleteByIdsMapper
@RegisterMapper
public interface DeleteByIdsMapper<T> {
@DeleteProvider(
type = IdsProvider.class,
method = "dynamicSQL"
)
int deleteByIds(String var1);
}
SQL模版生产者IdsProvider.class
的源码如下,继承了MapperTemplate
模版接口
public class IdsProvider extends MapperTemplate {
public IdsProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
super(mapperClass, mapperHelper);
}
public String deleteByIds(MappedStatement ms) {
Class<?> entityClass = this.getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(SqlHelper.deleteFromTable(entityClass, this.tableName(entityClass)));
Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
if (columnList.size() == 1) {
EntityColumn column = (EntityColumn)columnList.iterator().next();
sql.append(" where ");
sql.append(column.getColumn());
sql.append(" in (${_parameter})");
return sql.toString();
} else {
throw new MapperException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
}
}
... ... //省略
上面的代码实际上就是对sql语句的拼接,由此可得我们只需要创建一个mappr接口,然后再创建一个provider继承MapperTemplate
模版接口即可
更多template的功能可以参考这篇博客 《基于tk.mybatis扩展自己的通用mapper》
注意,这个类不能被MapperScan 扫描到,否则会报错
@RegisterMapper
public interface DletetByCodesMapper<T> {
@DeleteProvider(
type = IdsProviderExt.class,
method = "dynamicSQL"
)
int deleteByCodes(String[] arr);
}
由于我的数据主键是UUID,所以依据IdsProvider进行了扩展
public class IdsProviderExt extends MapperTemplate {
public IdsProviderExt(Class<?> mapperClass, MapperHelper mapperHelper) {
super(mapperClass, mapperHelper);
}
public String deleteByCodes(MappedStatement ms) {
Class<?> entityClass = this.getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(SqlHelper.deleteFromTable(entityClass, this.tableName(entityClass)));
Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
if (columnList.size() == 1) {
EntityColumn column = (EntityColumn)columnList.iterator().next();
sql.append(" where ");
// 不指定@Parm的话,数组参数默认使用array接收
sql.append("" );
sql.append(column.getColumn() + "= #{code}");
sql.append("");
return sql.toString();
} else {
throw new MapperException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
}
}
}
/**
* tkMybatis 的 BaseMapper
* 特别注意,该接口不能被扫描到,否则会出错
*/
public interface MyMapper extends Mapper, MySqlMapper , DeleteByIdsMapper, DletetByCodesMapper {
}
TbSysUserMapper 继承了MyMapper接口,所以也就继承了扩展接口
public interface TbSysUserMapper extends MyMapper<TbSysUser> {
}
<mapper namespace="com.funtl.itoken.common.mapper.TbSysUserMapper">
<resultMap id="BaseResultMap" type="com.funtl.itoken.common.domain.entity.TbSysUser">
<id column="user_code" jdbcType="VARCHAR" property="userCode" />
<result column="login_code" jdbcType="VARCHAR" property="loginCode" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
@MapperScan(basePackages = {"com.funtl.itoken.common.mapper","com.funtl.itoken.service.admin.mapper"})//dao路径
public class ServiceAdminApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAdminApplication.class,args);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServiceAdminApplication.class)
@Transactional
public class MyBatisTests{
/**
* 注入数据查询接口
*/
@Autowired
private TbSysUserMapper tbUserMapper;
/**
* 测试批量删除
*/
@Test
@Rollback
public void deleteByCodes() {
String[] codes = {"5bb38bf6-2378-431c-88cb-7a38b69df806","c04471e1-fa3b-4d6e-ba98-e806dc5ad07c"};
int i = tbUserMapper.deleteByCodes(codes);
info("删除数量:"+i);//这个是我的自定义函数,自行打印即可
}
}