首页  

Mybatis通用Mapper tk.mybatis 使用     所属分类 mybatis 浏览量 836
通用Mapper
解决单表增删改查,基于Mybatis插件
不需要编写SQL,不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查
通过反射获取实体类信息,构造相应的SQL 



实体类写法 实体类所有字段都会作为表中的字段来操作,如果有额外字段,加上@Transient注解 @Table(name = "test_table") public class TestTableDO implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "JDBC") private Long id; @Transient private String userId; private String name; private Timestamp createTime; private String createUserId; private Timestamp updateTime; private String updateUserId; private Integer isDelete; // get set ... } 表名默认使用类名,驼峰转下划线 , 如UserInfo 对应表名为user_info 表名可使用@Table(name = "tableName")指定 表字段名默认为Java对象字段名字驼峰转下划线 可使用@Column(name = "fieldName")指定字段名 使用@Transient注解字段,不作为表字段 @Id 主键字段, 多个@Id注解字段 联合主键 MySQL自增字段,加上@GeneratedValue(generator = "JDBC")即可
DAO写法 传统的Mybatis,DAO接口需要与Mapper文件关联,需要编写SQL来实现DAO接口中的方法 通用Mapper中,DAO只需要继承一个通用接口,即可拥有丰富的方法 继承通用的Mapper,指定泛型 public interface TestTableDao extends Mapper<TestTableDO> { } Mapper通用方法 Select List<T> select(T record); 根据实体中的属性值进行查询,查询条件使用等号 T selectByPrimaryKey(Object key); 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号 List<T> selectAll(); 查询全部结果,select(null)方法能达到同样效果 T selectOne(T record); 根据实体中的属性进行查询,只能有一个返回值,有多个结果抛出异常,查询条件使用等号 int selectCount(T record); 根据实体中的属性查询总数,查询条件使用等号 Insert int insert(T record); 保存一个实体,null的属性也会保存,不使用数据库默认值 int insertSelective(T record); 保存一个实体,null的属性不会保存,使用数据库默认值 Update int updateByPrimaryKey(T record); 根据主键更新实体全部字段,null值会被更新 int updateByPrimaryKeySelective(T record); 根据主键更新属性不为null的值 Delete 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条件更新实体record包含的全部属性,null值会被更新 int updateByExampleSelective(@Param("record") T record, @Param("example") Object example); 根据Example条件更新实体record包含的不是null的属性值 int deleteByExample(Object example); 根据Example条件删除数据
新增 TestTableDO obj = new TestTableDO(); int row = testTableDao.insertSelective(obj); 修改 TestTableDO obj = new TestTableDO(); int row = testTableDao.updateByPrimaryKeySelective(obj); 查询单个 TestTableDO obj = new TestTableDO(); vo.setId(123L); TestTableDO result = testTableDao.selectOne(obj); 条件查询 // 创建Example Example example = new Example(TestTableDO.class); // 创建Criteria Example.Criteria criteria = example.createCriteria(); // 添加条件 criteria.andEqualTo("isDelete", 0); criteria.andLike("name", "%abc123%"); List< TestTableDO> list = testTableDao.selectByExample(example);
依赖 tk.mybatis:mapper:4.0.0 <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dyyx.dao"/> <property name="properties"> <value> mappers=tk.mybatis.mapper.common.Mapper </value> </property> </bean> 使用 tk.mybatis.spring.mapper.MapperScannerConfigure 替换 Mybatis的org.mybatis.spring.mapper.MapperScannerConfigurer

上一篇     下一篇
scala hello

常用存储选型指南

Linux hostname

spark-shell 简单使用

drools StatelessKieSession 并发执行 空指针

中概互联 恒生互联网 恒生科技 指数简介及比较