- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Mybatis velocity脚本的使用教程详解(推荐)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
可能很多童鞋都还不是很清楚在mybatis可以使用各种脚本语言来定义Mapper文件里面的动态SQL;目前mybatis支持的脚本语言有XML(默认的);Velocity和Freemarker三种。使用不同的脚本语言来完成Mapper文件的编写,第一是使用自己熟悉的脚本语言,第二是能够定义更多丰富的自定义指令来简化Mapper的开发,关于MyBatis支持脚本的原理分析,自定义脚本指令后面再写文章分析,本文先介绍mybatis中velocity脚本的使用方式.
mybatis-velocity项目可以允许你方便的使用velocity作为脚本语言来完成Mapper文件中各种动态sql的编写.
注意,在脚本中大量使用velocity开发,如果不熟悉,可以先去看看velocity脚本; 。
安装 。
在maven中添加 。
1
2
3
4
5
|
<dependency>
<groupId>org.mybatis.scripting</groupId>
<artifactId>mybatis-velocity</artifactId>
<version>
1.2
</version>
</dependency>
|
注意我们使用的是mybatis-velocity1.2版本,该版本需要mybatis3.3支持; 。
在mybatis配置文件中,将velocity脚本引擎设置为默认的mapper文件引擎:
1
2
3
4
5
6
7
|
<typeAliases>
... <typeAlias type=
"org.mybatis.scripting.velocity.Driver"
alias=
"velocity"
/>
</typeAliases>
<settings>
...
<setting name=
"defaultScriptingLanguage"
value=
"velocity"
/>
</settings>
|
配置即完成.
接下来就可以在mapper文件中使用velocity脚本了:
1
2
3
4
|
<select id=
"findPerson"
lang=
"velocity"
>
#set( $pattern = $_parameter.name +
'%'
)
SELECT * FROM person WHERE name LIKE @{pattern, jdbcType=VARCHAR}
</select>
|
注意:
如果使用了velocity,参数要使用@{}来引用,因为velocity的指令就是以#开头的,比如#set #if等; 。
使用velocity脚本,在参数中也可以配置对应的javaType和jdbcType;配置格式为:@{ property, attr1=val1, attr2=val2, ... };可配置项有javaType, jdbcType, mode, numericScale, resultMap, typeHandler, jdbcTypeName; 在velocity的指令中使用context中的变量,需要使用$_parameter来作为前缀引用,比如 。
1
2
3
4
|
#
if
($_parameter.name)
#set($_name =
'%'
+$_parameter.name+
'%'
)
AND name LIKE @{_name}
#end
|
mybatis-velocity内建的指令 。
除了velocity的指令,mybatis-velocity项目为mybatis定义了一些内建的velocity指令:
trim 。
1
|
#trim( prefix prefixOverrides suffix suffixOverrides ) body #end
|
其中的参数含义同XML的trim参数含义相同; 。
一个例子:
1
2
3
4
5
6
|
#trim(
" WHERE "
" AND| OR"
)
#
if
($_parameter.name)
#set($_name =
'%'
+$_parameter.name+
'%'
)
AND name LIKE @{_name}
#end
#end
|
where 。
1
2
|
#where() body #end
#where()同XML中的<where>相同,可以替换条件前的AND/OR,替换为WHERE;注意一定是\where()有括号的;
|
mset 。
1
2
|
#mset() body #end
#mset前面加一个m,为的是和velocity本身的#set指令区别,#mset等同于XML中的<set>元素,可以在条件前加上set语句,并且去掉set块最后的分号;
|
一个例子:
1
2
3
4
5
6
7
8
9
|
<update id=
"update"
>
UPDATE USER
#mset()
#
if
($_parameter.name) name=@{name}, #end
#
if
($_parameter.age) age=@{age}, #end
#
if
($_parameter.bornDate) borndate=@{bornDate} #end
#end
WHERE id = @{id}
</update>
|
repeat 。
1
2
|
#repeat( collection var separator open close ) body #end
#repeat指令和XML中的<foreach>元素相同,能够方便的遍历集合/数组类型元素,并使用其中的每一个元素:
|
一个例子:
1
2
3
4
5
6
|
SELECT *FROM City
#where()
#repeat( $_parameter.ids $id
","
" state_id IN ("
")"
)
@{id}
#end
#end
|
in 。
1
|
#in( collection var field ) body #end
|
#in指令是一个新的指令,能够快速的专门针对SQL中的IN条件生成对应的field IN()语句;参数列表中,collection代表要遍历的IN中的内容;var代表遍历中的每一个对象的临时引用名称;field代表在IN语句之前生成的字段名称; 。
一个例子:
1
2
3
4
5
6
|
SELECT *FROM City
#where()
#in( $_parameter.ids $id
"state_id"
)
@{id}
#end
#end
|
自定义指令 。
mybatis-velocity允许你方便的自定义自己的指令用于简化开发,自定义指令的步骤为:
在classpath中添加一个 mybatis-velocity.properties配置文件; 。
创建自己的Velocity指令解析类; 。
将自己创建的Velocity指令解析类添加到配置文件中; 。
在mapper.xml文件中使用指令; 。
一个例子:
1
2
3
4
5
6
7
8
9
|
// User defined directivepackage com.myproject.directives;
//自定义的指令类需要继承Directive类;
public
class
MyDirective
extends
Directive { }
//mybatis-velocity.properties
//如果有多个自定义指令类,使用分号隔开;
userdirective=com.myproject.directives.MyDirective;
// mapper xml file
SELECT *FROM City
#myDirective() ...... #end
|
综合使用 。
如果使用velocity-mybatis,一个典型的CRUD的mapper就可以看起来是这个样子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<mapper namespace=
"mybatis.com._520it.mybatis.UserMapper"
>
<resultMap type=
"User"
id=
"user_mapping"
>
<id column=
"id"
property=
"id"
/>
<result column=
"name"
property=
"name"
/>
<result column=
"age"
property=
"age"
/>
<result column=
"borndate"
property=
"bornDate"
/>
</resultMap>
<insert id=
"add"
keyColumn=
"id"
keyProperty=
"id"
useGeneratedKeys=
"true"
>
INSERT INTO USER(name,age,borndate) VALUES (@{name,javaType=string,jdbcType=VARCHAR},@{age},@{bornDate})
</insert>
<update id=
"update"
>
UPDATE USER
#mset()
#
if
($_parameter.name) name=@{name}, #end
#
if
($_parameter.age) age=@{age}, #end
#
if
($_parameter.bornDate) borndate=@{bornDate} #end
#end
WHERE id = @{id}
</update>
<delete id=
"delete"
parameterType=
"long"
>
DELETE FROM USER WHERE id = @{id}
</delete>
<sql id=
"user_column"
>
id,name,age,borndate
</sql>
<select id=
"get"
resultMap=
"user_mapping"
>
SELECT <include refid=
"user_column"
/>
FROM USER WHERE id = @{id}
</select>
<select id=
"list"
resultMap=
"user_mapping"
>
SELECT <include refid=
"user_column"
/>
FROM USER
</select>
<select id=
"listByName"
resultMap=
"user_mapping"
parameterType=
"string"
>
SELECT <include refid=
"user_column"
/>
FROM USER WHERE name = @{name}
</select>
<select id=
"queryBy"
resultMap=
"user_mapping"
>
SELECT id,name,age,borndate
FROM USER
#where()
#
if
($_parameter.name)
#set($_name =
'%'
+$_parameter.name+
'%'
)
AND name LIKE @{_name}
#end
#end
#
if
($_parameter.orderBy)
ORDER BY @{orderBy} @{orderType}
#end
#
if
($_parameter.pageSize>-
1
)
LIMIT @{start},@{pageSize}
#end
</select>
</mapper>
|
以上所述是小编给大家介绍的Mybatis velocity脚本的使用教程详解(推荐),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:http://www.jianshu.com/p/cecc187410be 。
最后此篇关于Mybatis velocity脚本的使用教程详解(推荐)的文章就讲到这里了,如果你想了解更多关于Mybatis velocity脚本的使用教程详解(推荐)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
全称“Java Virtual Machine statistics monitoring tool”(statistics 统计;monitoring 监控;tool 工具) 用于监控虚拟机的各种运
主要是讲下Mongodb的索引的查看、创建、删除、类型说明,还有就是Explain执行计划的解释说明。 可以转载,但请注明出处。  
1>单线程或者单进程 相当于短链接,当accept之后,就开始数据的接收和数据的发送,不接受新的连接,即一个server,一个client 不存在并发。 2>循环服务器和并发服务器
详解 linux中的关机和重启命令 一 shutdown命令 shutdown [选项] 时间 选项: ?
首先,将json串转为一个JObject对象: ? 1
matplotlib官网 matplotlib库默认英文字体 添加黑体(‘SimHei')为绘图字体 代码: plt.rcParams['font.sans-serif']=['SimHei'
在并发编程中,synchronized关键字是常出现的角色。之前我们都称呼synchronized关键字为重量锁,但是在jdk1.6中对synchronized进行了优化,引入了偏向锁、轻量锁。本篇
一般我们的项目中会使用1到2个数据库连接配置,同程艺龙的数据库连接配置被收拢到统一的配置中心,由DBA统一配置和维护,业务方通过某个字符串配置拿到的是Connection对象。  
实例如下: ? 1
1. MemoryCahe NetCore中的缓存和System.Runtime.Caching很相似,但是在功能上做了增强,缓存的key支持object类型;提供了泛型支持;可以读缓存和单个缓存
argument是javascript中函数的一个特殊参数,例如下文,利用argument访问函数参数,判断函数是否执行 复制代码 代码如下: <script
一不小心装了一个Redis服务,开了一个全网的默认端口,一开始以为这台服务器没有公网ip,结果发现之后悔之莫及啊 某天发现cpu load高的出奇,发现一个minerd进程 占了大量cpu,googl
今天写这个是为了 提醒自己 编程过程 不仅要有逻辑 思想 还有要规范 代码 这样可读性 1、PHP 编程规范与编码习惯最主要的有以下几点: 1 文件说明 2 funct
摘要:虚拟机安装时一般都采用最小化安装,默认没有lspci工具。一台测试虚拟网卡性能的虚拟机,需要lspci工具来查看网卡的类型。本文描述了在一个虚拟机中安装lspci工具的具体步骤。 由于要测试
1、修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统
目录 算术运算符 基本四则运算符 增量赋值运算符 自增/自减运算符 关系运算符 逻
如下所示: ? 1
MapperScannerConfigurer之sqlSessionFactory注入方式讲解 首先,Mybatis中的有一段配置非常方便,省去我们去写DaoImpl(Dao层实现类)的时间,这个
Linux的网络虚拟化是LXC项目中的一个子项目,LXC包括文件系统虚拟化,进程空间虚拟化,用户虚拟化,网络虚拟化,等等,这里使用LXC的网络虚拟化来模拟多个网络环境。 本文从基本的网络设备讲
? 1
我是一名优秀的程序员,十分优秀!