- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解Mybatis通用Mapper介绍与使用由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
使用mybatis的开发者,大多数都会遇到一个问题,就是要写大量的sql在xml文件中,除了特殊的业务逻辑sql之外,还有大量结构类似的增删改查sql。而且,当数据库表结构改动时,对应的所有sql以及实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,通用mapper便应运而生…… 。
什么是通用mapper 。
通用mapper就是为了解决单表增删改查,基于mybatis的插件。开发人员不需要编写sql,不需要在dao中增加方法,只要写好实体类,就能支持相应的增删改查方法.
如何使用 。
以mysql为例,假设存在这样一张表:
1
2
3
4
5
6
7
8
9
10
|
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依赖 。
1
2
3
4
5
6
|
<!-- 通用mapper -->
<dependency>
<groupid>tk.mybatis</groupid>
<artifactid>mapper</artifactid>
<version>
3.3
.
9
</version>
</dependency>
|
springmvc配置 。
1
2
3
4
5
6
7
8
9
|
<!-- 通用 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 注解.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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 ,必须指定泛型 。
1
2
|
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 ,即可使用.
1
2
|
@autowired
private
testtabledao testtabledao;
|
下面演示大概的写法:
新增 。
1
2
3
|
testtablevo vo =
new
testtablevo();
// 省略为vo设置属性...
int
row = testtabledao.insertselective(vo);
|
修改 。
1
2
3
|
testtablevo vo =
new
testtablevo();
// 省略为vo设置属性...
int
row = testtabledao.updatebyprimarykeyselective(vo);
|
查询单个 。
1
2
3
|
testtablevo vo =
new
testtablevo();
vo.setid(123l);
testtablevo result = testtabledao.selectone(vo);
|
条件查询 。
1
2
3
4
5
6
7
8
|
// 创建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 。
1
2
3
4
5
6
7
8
9
10
11
12
|
<!--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 配置 。
1
2
3
4
5
|
#mapper
#mappers 多个接口时逗号隔开
mapper.mappers=tk.mybatis.mapper.common.mapper
mapper.not-empty=
false
mapper.identity=mysql
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://www.ciphermagic.cn/mybatis-mapper.html 。
最后此篇关于详解Mybatis通用Mapper介绍与使用的文章就讲到这里了,如果你想了解更多关于详解Mybatis通用Mapper介绍与使用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
from sqlalchemy import * from sqlalchemy import create_engine, ForeignKey from sqlalchemy import Col
我使用 MR1 API(hadoop-core-1.2.1.jar) 编写了一个示例字数统计程序。映射器类定义如下, public interface Mapper extends JobConfi
创建maven项目,修改pom.xml文件,如下: ? 1
问题最终得到解决在底部查看我的解决方案 最近我正在尝试运行 Mahout in Action 的第 6 章( list 6.1 ~ 6.4)中的推荐系统示例。但是我遇到了一个问题,我已经用谷歌搜索了但
如果有人知道在 .NET 中执行此操作的更多方法,您对这些方法有何看法?您选择哪种方法,为什么? 下面是.NET中对象拷贝不同方式的测试。 与此原始线程相关的测试:How to copy value
我能看出参数个数的不同,但我不知道实现上的不同。 每种方法的行为是否存在重要差异? 最佳答案 第一个填充您传入的现有对象。第二个为您创建一个新对象。这是“项目”和“填充”之间的语义差异。 关于c# -
我正在将一个项目从 Glass Mapper v2 (Glass.Mapper.Sitecore) 升级到 v4 (Glass.Mapper.Sc),我遇到了一个问题,我们的解决方案是使用 Insta
我正在实现一些 hadoop 应用程序。我的编码部分几乎完成了。但是想在阅读“Lin & Chris Dryer”的映射器设计模式书后改进编码器。至于这种方法的有效实现,需要在 map 函数中保留状态
当我尝试访问该页面时发生此错误。我在创建表时没有遇到错误,但似乎仍然存在问题。 模型是这样的: class User(UserMixin, db.Model): id = db.Column(
我创建了一个使用 SQLAlchemy 的类: class DbAbsLayer(object): def __init__(self): self.setConnection
我试图在我的 MVC - Sitecore - 7.1 中的 v4.0.30319 项目中使用 Glass Mapper。 以下是我安装的 Glass Mapper 版本 Glass Mapper 版
我在尝试使用 org.apache.hadoop.mapred.lib.IdentityMapper 作为 Hadoop Streaming 1.0.3 中 -mapper 的参数时遇到了问题。 “猫
这是我的mybatis配置。这是我的pom.xml。。当我运行项目时,它显示了错误的原因:org/mybatis/spring/mapper/MapperScannerConfigurer.有没有人能
背景: 1.在数据库中有一个通知表 可以看到其中的 gmt_create、 notifier_name、 outer_title 这三个字段是有下划线的 2.这张表
hadoop配置对象仅允许在set方法中将字符串作为值 set(字符串名称,字符串值) 是否有一种简单的方法来设置任何其他对象类型?我想在映射器中检索这些对象。 我注意到在0.15左右的版本中,有一个
我要开发的更大目标如下: a)仪表板,除其他功能外,用户还可以上传文档(.pdf,.txt,.doc)。所有这些文档都转到特定目录。 b)用户还可以查询所有带有特定关键字标记的文档。 现在,我希望使用
2016-10-20 18:03:51.253 WARN 17216 --- [restartedMain] .s.c.a.CommonAnnotationBeanPostProcessor:在名为“
我正在尝试同时使用映射器和缩减器来运行作业,但映射器运行缓慢.. 如果对于相同的输入我禁用 reducers,映射器将在 3 分钟内完成而对于 mapper-reducer 作业,即使在 30 分钟后
由于一些数据在所有 map() 函数之间共享,我无法在 setup() 中生成它们,因为每个 setup() 对应于每个map() 函数,而我想做的是预先生成一些数据并将其存储在可实现的地方,然后在每
我正在使用 Hadoop,我想使用静态变量来减少必须进行的方法调用次数。以下是我如何使用静力学: public class Mapper extends Mapper { protected
我是一名优秀的程序员,十分优秀!