mybatis写接口 简单的实现等
mybatis书写自定义的接口
实现伪删除,排序置顶等接口
使用的mybatis是
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.0</version> </dependency>
1、定义接口
package tk.mybatis.mapper.common.base.delete; import org.apache.ibatis.annotations.DeleteProvider; import tk.mybatis.mapper.provider.base.BaseDeleteProvider; /** * @author xiebq 2018/12/24 */ public interface LogicDeleteByPrimaryKey<T> { /** * 根据主键字段进行删除,方法参数必须包含完整的主键属性 * * @param key * @return */ @DeleteProvider(type = BaseDeleteProvider.class, method = "dynamicSQL") int logicDeleteByPrimaryKey(Object key); }
2、集成接口
public interface BaseDeleteMapper<T> extends DeleteMapper<T>, LogicDeleteByPrimaryKey<T>, DeleteByPrimaryKeyMapper<T> { }
3、实现接口 在BaseDeleteProvider 类中实现接口的方法
/** * 伪删除的实现 * * @param ms */ public String logicDeleteByPrimaryKey(MappedStatement ms) { final Class<?> entityClass = getEntityClass(ms); StringBuilder sql = new StringBuilder(); sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass))); sql.append(" set is_delete = 1 "); Date date = new Date(); SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sql.append(" ,updated = '"+sdf2.format(date) +"' "); sql.append(SqlHelper.wherePKColumns(entityClass)); return sql.toString(); }
4、使用接口
@Mapper public interface UserDao extends MyMapper<User> {}
public Boolean logicDelete(Integer id) { userDao.logicDeleteByPrimaryKey(id); return true; }
tk mybatis
通过拼接sql的语句实现,显示接口定义,实现类拼接sql语句,然后实现语句
在mybatis拼接sql语句中,提供多个sql语句拼接的Provider