使用mybatis的开发者,大多数都会遇到一个问题,就是要写大量的sql在xml文件中,除了特殊的业务逻辑sql之外,还有大量结构类似的增删改查sql。而且,当数据库表结构改动时,对应的所有sql以及实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,通用mapper便应运而生……
什么是通用mapper
通用mapper就是为了解决单表增删改查,基于mybatis的插件。开发人员不需要编写sql,不需要在dao中增加方法,只要写好实体类,就能支持相应的增删改查方法。
如何使用
以mysql为例,假设存在这样一张表:
create table `test_table` ( `id` bigint(20) not null auto_increment, `name` varchar(255) default '', `create_time` datetime default null, `create_user_id` varchar(32) default null, `update_time` datetime default null, `update_user_id` varchar(32) default null, `is_delete` int(8) default null, primary key (`id`) ) engine=innodb auto_increment=1 default charset=utf8;
主键是 id ,自增。下面以这张表为例介绍如何使用通用mapper。
maven依赖
<!-- 通用mapper --> <dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper</artifactid> <version>3.3.9</version> </dependency>
springmvc配置
<!-- 通用 mapper --> <beanclass="tk.mybatis.spring.mapper.mapperscannerconfigurer"> <propertyname="basepackage"value="cn.com.bluemoon.bd.service.spider.dao"/> <propertyname="properties"> <value> mappers=tk.mybatis.mapper.common.mapper </value> </property> </bean>
注意这里使用 tk.mybatis.spring.mapper.mapperscannerconfigure
替换原来mybatis的 org.mybatis.spring.mapper.mapperscannerconfigurer
。
可配参数介绍:
大多数情况下不会用到这些参数,有特殊情况可以自行研究。
实体类的写法
记住一个原则:实体类的字段数量 >= 数据库表中需要操作的字段数量。默认情况下,实体类中的所有字段都会作为表中的字段来操作,如果有额外的字段,必须加上 @transient
注解。
@table(name = "test_table") public class testtablevoimplements 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... }
说明:
dao 的写法
在传统的mybatis写法中, dao 接口需要与 mapper 文件关联,即需要编写 sql 来实现 dao 接口中的方法。而在通用mapper中, dao 只需要继承一个通用接口,即可拥有丰富的方法:
继承通用的mapper ,必须指定泛型
public interface testtabledaoextends mapper<testtablevo>{ }
一旦继承了mapper ,继承的mapper就拥有了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条件删除数据
代码中使用
在 service 中注入 dao ,即可使用。
@autowired private testtabledao testtabledao;
下面演示大概的写法:
新增
testtablevo vo = new testtablevo(); // 省略为vo设置属性... int row = testtabledao.insertselective(vo);
修改
testtablevo vo = new testtablevo(); // 省略为vo设置属性... int row = testtabledao.updatebyprimarykeyselective(vo);
查询单个
testtablevo vo = new testtablevo(); vo.setid(123l); testtablevo result = testtabledao.selectone(vo);
条件查询
// 创建example example example = new example(testtablevo.class); // 创建criteria example.criteria criteria = example.createcriteria(); // 添加条件 criteria.andequalto("isdelete", 0); criteria.andlike("name", "%abc123%"); list<testtablevo> list = testtabledao.selectbyexample(example);
总结
通用mapper的原理是通过反射获取实体类的信息,构造出相应的sql,因此我们只需要维护好实体类即可,对于应付复杂多变的需求提供了很大的便利。上文叙述的只是通用mapper的简单用法,在实际项目中,还是要根据业务,在通用mapper的基础上封装出粒度更大、更通用、更好用的方法。
附 spring boot 配置
maven
<!--mybatis--> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.3.1</version> </dependency> <!--mapper--> <dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper-spring-boot-starter</artifactid> <version>1.1.4</version> </dependency>
application.properties 配置
#mapper #mappers 多个接口时逗号隔开 mapper.mappers=tk.mybatis.mapper.common.mapper mapper.not-empty=false mapper.identity=mysql
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Postgresql结合postgis使用java的JDBC连接
网友评论