在讲MyBatis动态SQL之前,先教大家怎么通过MyBatis插件自动生成mapper对应的文件和对应的bean文件。使用起来非常简单。
第一步,在pom.xml文件里面添加plugin。这里要注意configurationFile部分这个字段指明generatorConfig.xml文件的位置。generatorConfig.xml在第二步会教大家怎么配置。这里我们指定的目录是resources/generator文件夹下面,大家根据自己的实际情况来指定。
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
mysql
mysql-connector-java
5.1.35
${basedir}/src/main/resources/generatorConfig.xml
true
第二步,generatorConfig.xml文件的配置,主要改的地方就是jdbcConnection字段里面数据库相关的信息。以及javaModelGenerator,sqlMapGenerator,javaClientGenerator字段目标文件的位置。和table字段里面改成自己的表格。关于这些东西generatorConfig.xml文件里面咱们也注释的很清楚。
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
connectionURL="jdbc:mysql://192.168.13.98/dcim" userId="root" password="123456">
targetProject="src/main/java">
targetProject="src/main/resources">
targetPackage="com.pilot.dcim.alarmmanage.mapper"
implementationPackage="com.pilot.dcim.alarmmanage.service.impl"
targetProject="src/main/java">
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
第三步,上面两部都配置好之后,执行命令 mvn mybatis-generator:generate 就会在对应的目录下生成我们想要的文件。
MyBatis的动态SQL是基于OGNL的表达式的。它对SQL语句进行灵活的操作。通过表达式判断来实现对SQL的灵活拼接、组装。我们着重讲说MyBatis动态SQL里面一些元素的使用。
一、MyBatis动态SQL标签的学习
1.1、if 条件判断标签
if标签通用用于通过判断参数值来决定是否使用某个查询条件。
1.1.1、if 判断数字
if 判断数字,大部分都是判断等于或者不等于某个数字。
...
1.1.2、if 判断字符串
大部分情况下都是去判断传递过来的字符串是否为空。当然咱们也是可以添加别的条件限制的,比如以啥字符串开头,以啥字符串结尾。
1.1.2.1、判断字符串是否为空,或者字符串等于某个值
select * from user
and name = #{user.name}
1.1.2.2、判断字符串是否包含
select * from user
and name = #{user.name}
1.1.2.3、判断是否以字符串开头
select * from user
and name = #{user.name}
1.1.3、判断列表
经常判断列表是否为null,或者列表是否为空。列表判断也是很简单的直接 就搞定了。
一个简单的实例,通过用户名(list)查询用户
List selectUserByName(@Param("nameList") List nameList);
select * from user
#{i}
1.2、choose,when,otherwise标签(switch case)
choose,when,otherwise标签的作用类似与咱们java里面的switch case的作用。
#{item.name,jdbcType=VARCHAR},
null,
1.3、foreach循环标签
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。大部分都是对一个list进行循环。
foreach元素的属性主要有 item,index,collection,open,separator,close。
foreach元素
解释
collection
迭代的对象
item
迭代过程中的每个元素
index
迭代过程中的位置
open
语句以什么开始
separator
语句以什么作为分隔符
close
语句以什么结束
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list。
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array。
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以。
一个简单的实例
select * from t_blog where id in
#{item}
array
select * from t_blog where id in
#{item}
map
select * from t_blog where title like "%"#{title}"%" and id in
#{item}
上面说的都是没有指定@Param的情况,如果指定了@Param则collection的属性值是@Param指定的名字。
1.4、trim标签
trim标记是一个格式化的标记,可以完成set或者是where标记的功能。
trim属性
描述
prefix
给sql语句拼接的前缀
suffix
给sql语句拼接的后缀
prefixesToOverride
去除sql语句前面的关键字或者字符,该关键字或者字符由prefixesToOverride属性指定,假设该属性指定为”AND”,当sql语句的开头为”AND”,trim标签将会去除该”AND
suffixesToOverride
去除sql语句后面的关键字或者字符,该关键字或者字符由suffixesToOverride属性指定
state = #{state}
AND title like #{title}
AND author_name like #{author.name}
1.5、where标签
where标签是非常有用的,他有两个作用:
当where标签里面元素的元素都不满足条件的时候,不会插入where语句。
where标签里面若语句的开头为“AND”或“OR”,where 元素也会将它们去除。
有这么一个例子,我呢有一个user表(pkid, name, phone, password),现在根据我们传入的条件来查询user信息。
select * from user
pkid = #{user.pkid}
and name = #{user.name}
and phone = #{user.phone}
and password = #{user.password}
1.6、set标签
set标签和where标签一样。会自动加上set关键字,并且去掉不必要的一些字符。
AND A.userId = #{userId,jdbcType=CHAR}
AND A.userName = #{userName,jdbcType=CHAR}
1.7、bind标签
bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中。咱就可以简单的认为是声明了一个变量。
这个例子里面,我纯粹的是把user.name替换成了user.name了。
select * from user
and name = #{userName}
这例子里面咱用bind标签声明了一个模糊查询的变量
select * from user
and name like #{patternName}
二、MyBatis动态SQL实例
最后我们根据今天所学到的内容,给出两个比较复杂的MyBats动态SQL。
2.1、批量插入并且更新主键
insert into user (name, password, phone)
values
#{item.name,jdbcType=VARCHAR},
null,
#{item.password,jdbcType=VARCHAR},
null,
#{item.phone,jdbcType=VARCHAR},
null,
2.2、批量更新
update user
when pkid=#{i.pkid} then #{i.name}
when pkid=#{i.pkid} then #{i.password}
when pkid=#{i.pkid} then #{i.phone}
where
pkid=#{i.pkid}