gpt4 book ai didi

Mybatis velocity脚本的使用教程详解(推荐)

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 26 4
gpt4 key购买 nike

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的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com