Mybatis最强大的特性之一是她的动态语句功能。如果你之前经常写原生的sql,你就会和小编一样明白把sql语句条件连接在一起是多么的痛苦,一点都不能马虎。Mybatis的动态语句就为我们解决了这个问题。Mybatis可以通过映射的sql语句使用强大的动态sql来解决很多问题。就像下面这样的sql,小编在过去的一个月里,每天都在重复着这样的拼写工作。。
<span style="font-family:KaiTi_GB2312;font-size:18px;">sql2 = "SELECT t.pname,t.pjname,t.xmmc,t.totalmoney,t.jyje,(case t.isAgree when '1' then '通过' when '0' then '未通过' else '未评审' end) as jg,t.district FROM(SELECT SUBSTRING (t1.aname,0,len(t1.aname) - 2) AS pname,t.*,t1.district FROM(SELECT t.*,t1.isAgree FROM(SELECT '后补助项目' AS pjname, afterpj.aname AS xmmc,afterpj.sjje AS totalmoney,afterpj.pjje AS jyje,afterpj.provinceid FROM afterpj)t LEFT JOIN afterprojectassess t1 ON t.xmmc=t1.afterProjectName)t LEFT JOIN jujib t1 ON t.provinceid=t1.id)t LEFT JOIN spauthor sp ON t.provinceid=sp.pjcode AND spName=" + spname.Trim();</span>
一.动态语句
MyBatis使用了基于强大的OGNL(Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言)表达式来避免了大部分其他的元素。基本的语句有:IF,Choose(when,otherwise),trim(where,set),foreach。
1.IF语句
最常用的就是用条件包含一个where 子句,之前开发.net的项目的时候,都是在代码里面写if 怎样怎样 就 sql=sql+"where id = @id" 这样的写法,Mybatis可以就这样的判断放到语句中。
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <insert id="insertSelective" parameterType="mybatis.model.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="username != null" >
userName,
</if>
<if test="userage != null" >
userAge,
</if>
<if test="useraddress != null" >
userAddress,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="userage != null" >
#{userage,jdbcType=INTEGER},
</if>
<if test="useraddress != null" >
#{useraddress,jdbcType=VARCHAR},
</if>
</trim>
</insert></span>
2.choose,when,otherwise
有的时候我们并不像应用所有的条件,只是想从多项式中选择一个条件,就像case when 语句一样,之前是这样写的:
<span style="font-family:KaiTi_GB2312;font-size:18px;">sql2 = "SELECT t.pname,t.pjname,t.xmmc,t.totalmoney,t.jyje,(case t.isAgree when '1' then '通过' when '0' then '未通过' else '未评审' end) as jg,t.district FROM(SELECT SUBSTRING (t1.aname,0,len(t1.aname) - 2) AS pname,t.*,t1.district FROM(SELECT t.*,t1.isAgree FROM(SELECT '后补助项目' AS pjname, afterpj.aname AS xmmc,afterpj.sjje AS totalmoney,afterpj.pjje AS jyje,afterpj.provinceid FROM afterpj)t LEFT JOIN afterprojectassess t1 ON t.xmmc=t1.afterProjectName)t LEFT JOIN jujib t1 ON t.provinceid=t1.id)t LEFT JOIN spauthor sp ON t.provinceid=sp.pjcode AND spName=" + spname.Trim();</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"><select id="findActive" parameterType="Blog" resultType="Blog">
SELECT * FROM BLOG WHERE state = 'ACTIVE'
<choose>
<when test ="title != null">
AND title like #{title}
</when>
<otherwise>
and feathered=1
</otherwise>
</choose>
</select></span>
where标签是为了防止如果一个条件都没有,那么语句就会写成:select * from user where 这样尴尬的句子。如果使用了where标签,那么如果标签中有返回值,那么就插入一个where,如果标签返回的内容是用and或者or开头了,它就会剔除掉。
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <select id="findActive" parameterType="Blog" resultType="Blog">
SELECT * FROM BLOG
<where>
<If test ="state != null">
state = #{state}
</If>
<If test ="tietle != null">
AND title like #{title}
</If>
<If test ="author != null and author.name !=null">
AND title like #{author.name}
</If>
</where>
</select></span>
在动态语句中update语句可以动态的配置set关键字后边的字段,也是同理于上面的select语句,这次变成了set标签。
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <update id="updateByPrimaryKeySelective" parameterType="mybatis.model.User" >
update user
<set >
<if test="username != null" >
userName = #{username,jdbcType=VARCHAR},
</if>
<if test="userage != null" >
userAge = #{userage,jdbcType=INTEGER},
</if>
<if test="useraddress != null" >
userAddress = #{useraddress,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update></span>
trim的作用是自定义where和set元素,这样的写法相当于where,只不过是指定了一个trim的前缀。例如:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <trim prefix="WHERE" prefixOverrides = "AND | OR">
</trim></span>
4.foreach
之前写多个条件,都将list拼成字符串,将字符串传进来,放到IN条件的后边。Mybatis这里需要声明一个项,一个索引变量,指定开始和结束的字符。这样我可以直接把list传进来了,进行查询。
<span style="font-family:KaiTi_GB2312;font-size:18px;"><select id="selectPostIn" retultType="domain.blog.Post">
SELECT * FROM POST P WHERE ID IN
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select></span>
二. Mybatis generator 的使用
就像接触到的很多代码生成器一样,Mybatis也可以生成自己需要的类和xml文件。Mybatis的使用,最重要的就是这个写sql语句的xml文件,如果这个基本的框架可以帮我们生成,就很方便了。这时候Mybatis generator 就派上了用场。
1.需要更改生成的配置文件,大多是路径,数据库名称,实体名称的修改,修改的地方都用注释表明了。
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动包位置 -->
<!-- <classPathEntry location="D:\software\lib\mysql-connector-java-5.1.21.jar" /> -->
<classPathEntry location="D:\mysql\mysql-connector-java-5.0.4-bin.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sy" userId="sypro" password="sypro"> -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3307/mybatis" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="mybatis.model" targetProject="H:\generator\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 -->
<sqlMapGenerator targetPackage="mybatis.mapping" targetProject="H:\generator\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="mybatis.dao" targetProject="H:\generator\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="article" domainObjectName="Article" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration></span>
2.在目录下运行cmd,执行语句:java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
3.生成的目录,这样就会为我们生成基本的dao,mapping,model层了,把这个copy到我们的项目中就OK了
三.总结
1.Mybatis的动态语句的应用很大程度上减少了我们的逻辑代码,看着也很清楚。
2.之前敲代码的时候不知道这个generator工具,原来还有这么方便的工具使用呢,很多等着探索~~