mybatis动态SQL语句

   有些时候, sql 语句 where 条件中,须要一些安全判断,例如按某一条件查询时若是传入的参数是空,此时查询出的结果极可能是空的,也许咱们须要参数为空时,是查出所有的信息。使用 Oracle 的序列、 mysql 的函数生成 Id 。这时咱们可使用动态 sql

       下文均采用mysql语法和函数(例如字符串连接函数CONCAT)。java

3.1 selectKey 标签

       insert语句中,在Oracle常用序列、在MySQL中使用函数来自动生成插入表的主键,并且须要方法能返回这个生成主键。使用myBatisselectKey标签能够实现这个效果。mysql

       下面例子,使用mysql数据库自定义函数nextval('student'),用来生成一个key,并把他设置到传入的实体类中的studentId属性上。因此在执行完此方法后,边能够经过这个实体类获取生成的keysql

<mappers> <mapper resource="com/liming/manager/data/mappers/UserMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/StudentMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/ClassMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/TeacherMapper.xml" /> </mappers>

Java接口与XML文件在一个相对路径下时,能够不在myBatis配置文件的mappers中声明。数据库

 


SQL 映射XML 文件一些初级的元素:apache


1. cache – 配置给定模式的缓存
2. cache-ref – 从别的模式中引用一个缓存
3. resultMap – 这是最复杂而却强大的一个元素了,它描述如何从结果集中加载对象
4. sql – 一个能够被其余语句复用的SQL 块
5. insert – 映射INSERT 语句
6. update – 映射UPDATE 语句
7. delete – 映射DELEETE 语句
8. select  -  映射SELECT语句数组

 


2.1 resultMap

        resultMap 是MyBatis 中最重要最强大的元素了。你可让你比使用JDBC 调用结果集省掉90%的代码,也可让你作许多JDBC 不支持的事。现实上,要写一个等同相似于交互的映射这样的复杂语句,可能要上千行的代码。ResultMaps 的目的,就是这样简单的语句而不须要多余的结果映射,更多复杂的语句,除了只要一些绝对必须的语句描述关系之外,不再须要其它的。缓存

resultMap属性:type为java实体类;id为此resultMap的标识。安全

 

 resultMap能够设置的映射:app


1. constructor – 用来将结果反射给一个实例化好的类的构造器ide

a) idArg – ID 参数;将结果集标记为ID,以方便全局调用
b) arg –反射到构造器的一般结果


2. id – ID 结果,将结果集标记为ID,以方便全局调用


3. result – 反射到JavaBean 属性的普通结果


4. association – 复杂类型的结合;多个结果合成的类型

a) nested result mappings – 几resultMap 自身嵌套关联,也能够引用到一个其它上


5. collection –复杂类型集合a collection of complex types


6. nested result mappings – resultMap 的集合,也能够引用到一个其它上


7. discriminator – 使用一个结果值以决定使用哪一个resultMap

a) case – 基本一些值的结果映射的case 情形

i. nested result mappings –一个case 情形自己就是一个结果映射,所以也能够包括一些相同的元素,也能够引用一个外部resultMap。

 

 

2.1.1 id、result

id、result是最简单的映射,id为主键映射;result其余基本数据库表字段到实体类属性的映射。
  最简单的例子:

 

<!-- 插入学生 自动主键--> <insert id="createStudentAutoKey" parameterType="liming.student.manager.data.model.StudentEntity" keyProperty="studentId"> <selectKey keyProperty="studentId" resultType="String" order="BEFORE"> select nextval('student') </selectKey> INSERT INTO STUDENT_TBL(STUDENT_ID, STUDENT_NAME, STUDENT_SEX, STUDENT_BIRTHDAY, STUDENT_PHOTO, CLASS_ID, PLACE_ID) VALUES (#{studentId}, #{studentName}, #{studentSex}, #{studentBirthday}, #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}, #{classId}, #{placeId}) </insert>

调用接口方法,和获取自动生成key


StudentEntity entity = new StudentEntity(); entity.setStudentName("黎明你好"); entity.setStudentSex(1); entity.setStudentBirthday(DateUtil.parse("1985-05-28")); entity.setClassId("20000001"); entity.setPlaceId("70000001"); this.dynamicSqlMapper.createStudentAutoKey(entity); System.out.println("新增学生ID: " + entity.getStudentId());

selectKey语句属性配置细节:

 

属性 描述 取值
keyProperty selectKey 语句生成结果须要设置的属性。
resultType 生成结果类型,MyBatis 容许使用基本的数据类型,包括String int类型。
order

1BEFORE,会先选择主键,而后设置keyProperty,再执行insert语句;

2AFTER,就先运行insert 语句再运行selectKey 语句。

BEFORE

AFTER
statementType MyBatis 支持STATEMENTPREPAREDCALLABLE 的语句形式, 对应Statement PreparedStatement CallableStatement 响应

STATEMENT

PREPARED

CALLABLE

 

3.2 if标签

 

 if标签可用在许多类型的sql语句中,咱们以查询为例。首先看一个很普通的查询:

<!-- 查询学生list,like姓名 --> <select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') </select>

可是此时若是studentName或studentSex为null,此语句极可能报错或查询结果为空。此时咱们使用if动态sql语句先进行判断,若是值为null或等于空字符串,咱们就不进行此条件的判断,增长灵活性。

参数为实体类StudentEntity。将实体类中全部的属性均进行判断,若是不为空则执行判断条件。

<!-- 2 if(判断参数) - 将实体类不为空的属性做为where条件 --> <select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity"> SELECT ST.STUDENT_ID, ST.STUDENT_NAME, ST.STUDENT_SEX, ST.STUDENT_BIRTHDAY, ST.STUDENT_PHOTO, ST.CLASS_ID, ST.PLACE_ID FROM STUDENT_TBL ST WHERE <if test="studentName !=null "> ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%') </if> <if test="studentSex != null and studentSex != '' "> AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER} </if> <if test="studentBirthday != null "> AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE} </if> <if test="classId != null and classId!= '' "> AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR} </if> <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' "> AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR} </if> <if test="placeId != null and placeId != '' "> AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR} </if> <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' "> AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR} </if> <if test="studentId != null and studentId != '' "> AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR} </if> </select>

使用时比较灵活, new一个这样的实体类,咱们须要限制那个条件,只须要附上相应的值就会where这个条件,相反不去赋值就能够不在where中判断。

public void select_test_2_1() { StudentEntity entity = new StudentEntity(); entity.setStudentName(""); entity.setStudentSex(1); entity.setStudentBirthday(DateUtil.parse("1985-05-28")); entity.setClassId("20000001"); //entity.setPlaceId("70000001"); List<StudentEntity> list = this.dynamicSqlMapper.getStudentList_if(entity); for (StudentEntity e : list) { System.out.println(e.toString()); } }

3.3 if + where 的条件判断

       where中的条件使用的if标签较多时,这样的组合可能会致使错误。咱们以在3.1中的查询语句为例子,当java代码按以下方法调用时:

@Test public void select_test_2_1() { StudentEntity entity = new StudentEntity(); entity.setStudentName(null); entity.setStudentSex(1); List<StudentEntity> list = this.dynamicSqlMapper.getStudentList_if(entity); for (StudentEntity e : list) { System.out.println(e.toString()); } }

若是上面例子,参数studentNamenull,将不会进行STUDENT_NAME列的判断,则会直接导“WHERE AND”关键字多余的错误SQL

 

这时咱们可使用where动态语句来解决。这个“where”标签会知道若是它包含的标签中有返回值的话,它就插入一个‘where’。此外,若是标签返回的内容是以AND OR 开头的,则它会剔除掉。

上面例子修改成:

<!-- 3 select - where/if(判断参数) - 将实体类不为空的属性做为where条件 --> <select id="getStudentList_whereIf" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity"> SELECT ST.STUDENT_ID, ST.STUDENT_NAME, ST.STUDENT_SEX, ST.STUDENT_BIRTHDAY, ST.STUDENT_PHOTO, ST.CLASS_ID, ST.PLACE_ID FROM STUDENT_TBL ST <where> <if test="studentName !=null "> ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%') </if> <if test="studentSex != null and studentSex != '' "> AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER} </if> <if test="studentBirthday != null "> AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE} </if> <if test="classId != null and classId!= '' "> AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR} </if> <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' "> AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR} </if> <if test="placeId != null and placeId != '' "> AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR} </if> <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' "> AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR} </if> <if test="studentId != null and studentId != '' "> AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR} </if> </where> </select>

3.4 if + set 的更新语句

update语句中没有使用if标签时,若是有一个参数为null,都会致使错误。

当在update语句中使用if标签时,若是前面的if没有执行,则或致使逗号多余错误。使用set标签能够将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。

 

       使用if+set标签修改后,若是某项为null则不进行更新,而是保持数据库原值。以下示例:

<!-- 4 if/set(判断参数) - 将实体类不为空的属性更新 --> <update id="updateStudent_if_set" parameterType="liming.student.manager.data.model.StudentEntity"> UPDATE STUDENT_TBL <set> <if test="studentName != null and studentName != '' "> STUDENT_TBL.STUDENT_NAME = #{studentName}, </if> <if test="studentSex != null and studentSex != '' "> STUDENT_TBL.STUDENT_SEX = #{studentSex}, </if> <if test="studentBirthday != null "> STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, </if> <if test="studentPhoto != null "> STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}, </if> <if test="classId != '' "> STUDENT_TBL.CLASS_ID = #{classId} </if> <if test="placeId != '' "> STUDENT_TBL.PLACE_ID = #{placeId} </if> </set> WHERE STUDENT_TBL.STUDENT_ID = #{studentId}; </update>

3.5 if + trim代替where/set标签

       trim是更灵活的去处多余关键字的标签,他能够实践whereset的效果。

 

3.5.1trim代替where

<!-- 5.1 if/trim代替where(判断参数) - 将实体类不为空的属性做为where条件 --> <select id="getStudentList_if_trim" resultMap="resultMap_studentEntity"> SELECT ST.STUDENT_ID, ST.STUDENT_NAME, ST.STUDENT_SEX, ST.STUDENT_BIRTHDAY, ST.STUDENT_PHOTO, ST.CLASS_ID, ST.PLACE_ID FROM STUDENT_TBL ST <trim prefix="WHERE" prefixOverrides="AND|OR"> <if test="studentName !=null "> ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%') </if> <if test="studentSex != null and studentSex != '' "> AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER} </if> <if test="studentBirthday != null "> AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE} </if> <if test="classId != null and classId!= '' "> AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR} </if> <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' "> AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR} </if> <if test="placeId != null and placeId != '' "> AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR} </if> <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' "> AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR} </if> <if test="studentId != null and studentId != '' "> AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR} </if> </trim> </select>

 

3.5.2 trim代替set

<!-- 5.2 if/trim代替set(判断参数) - 将实体类不为空的属性更新 --> <update id="updateStudent_if_trim" parameterType="liming.student.manager.data.model.StudentEntity"> UPDATE STUDENT_TBL <trim prefix="SET" suffixOverrides=","> <if test="studentName != null and studentName != '' "> STUDENT_TBL.STUDENT_NAME = #{studentName}, </if> <if test="studentSex != null and studentSex != '' "> STUDENT_TBL.STUDENT_SEX = #{studentSex}, </if> <if test="studentBirthday != null "> STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, </if> <if test="studentPhoto != null "> STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}, </if> <if test="classId != '' "> STUDENT_TBL.CLASS_ID = #{classId}, </if> <if test="placeId != '' "> STUDENT_TBL.PLACE_ID = #{placeId} </if> </trim> WHERE STUDENT_TBL.STUDENT_ID = #{studentId} </update>

3.6 choose (when, otherwise)

 

    有时候咱们并不想应用全部的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为true,就会执行if标签中的条件。 MyBatis提供了choose 元素。if标签是与(and)的关系,而choose比傲天是或(or)的关系。

    choose标签是按顺序判断其内部when标签中的test条件出否成立,若是有一个成立,则choose结束。当choose中全部when的条件都 不满则时,则执行otherwise中的sql。相似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。

    例以下面例子,一样把全部能够限制的条件都写上,方面使用。choose会从上到下选择一个when标签的test为true的sql执行。安全考虑,咱们使用where将choose包起来,放置关键字多于错误。

<!-- 6 choose(判断参数) - 按顺序将实体类第一个不为空的属性做为where条件 --> <select id="getStudentList_choose" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity"> SELECT ST.STUDENT_ID, ST.STUDENT_NAME, ST.STUDENT_SEX, ST.STUDENT_BIRTHDAY, ST.STUDENT_PHOTO, ST.CLASS_ID, ST.PLACE_ID FROM STUDENT_TBL ST <where> <choose> <when test="studentName !=null "> ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%') </when > <when test="studentSex != null and studentSex != '' "> AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER} </when > <when test="studentBirthday != null "> AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE} </when > <when test="classId != null and classId!= '' "> AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR} </when > <when test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' "> AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR} </when > <when test="placeId != null and placeId != '' "> AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR} </when > <when test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' "> AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR} </when > <when test="studentId != null and studentId != '' "> AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR} </when > <otherwise> </otherwise> </choose> </where> </select>

3.7 foreach

对于动态SQL 很是必须的,主是要迭代一个集合,一般是用于IN 条件。List 实例将使用“list”作为键,数组实例以“array 作为键。

foreach元素是很是强大的,它容许你指定一个集合,声明集合项和索引变量,它们能够用在元素体内。它也容许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。

注意:你能够传递一个List实例或者数组做为参数对象传给MyBatis。当你这么作的时候,MyBatis会自动将它包装在一个Map中,用名称在做为键。List实例将会以“list”做为键,而数组实例将会以“array”做为键。

这个部分是对关于XML配置文件和XML映射文件的而讨论的。下一部分将详细讨论Java API,因此你能够获得你已经建立的最有效的映射。

 

 

3.7.1参数为array示例的写法

 

接口的方法声明:

public List<StudentEntity> getStudentListByClassIds_foreach_array(String[] classIds);

动态SQL语句:

<!— 7.1 foreach(循环array参数) - 做为where中in的条件 --> <select id="getStudentListByClassIds_foreach_array" resultMap="resultMap_studentEntity"> SELECT ST.STUDENT_ID, ST.STUDENT_NAME, ST.STUDENT_SEX, ST.STUDENT_BIRTHDAY, ST.STUDENT_PHOTO, ST.CLASS_ID, ST.PLACE_ID FROM STUDENT_TBL ST WHERE ST.CLASS_ID IN <foreach collection="array" item="classIds" open="(" separator="," close=")"> #{classIds} </foreach> </select>

测试代码,查询学生中,在2000000120000002这两个班级的学生:

@Test public void test7_foreach() { String[] classIds = { "20000001", "20000002" }; List<StudentEntity> list = this.dynamicSqlMapper.getStudentListByClassIds_foreach_array(classIds); for (StudentEntity e : list) { System.out.println(e.toString()); } <p>}<span style="font-size: 14px; font-weight: bold; white-space: normal;">&nbsp;&nbsp;</span></p>
相关文章
相关标签/搜索