您的位置:首页 > 移动开发

Mybatis中Mapper文件常用标签:

2017-02-16 00:00 447 查看
Mybatis中Mapper文件常用标签:
1.choose(when,otherwise)标签
当我们不想应用所有的条件,而只是想从多个选项中选择一个的时候,使用if标签时,只要test中的表达式为true,就会执行if标签中的条件。Mybatis提供了choose元素。if标签是与的关系,而choose是或的关系。choose标签是按照顺序判断其内部when标签中的test条件是否成立,如果有一个成立,则choose结束。当choose中所有when条件都不满足时,则执行otherwise中的sql。,类似于java中的Switch语句。
例如下面的例子,同样把所有可以限制的条件都写上,方便使用。choose会从上到西安选择一个when标签的test为true的sql执行。为了安全考虑,我们使用where将choose包起来,防止关键字多余错误。

<!--  choose(判断参数) - 按顺序将实体类 User 第一个不为空的属性作为:where条件 -->
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">
SELECT *
FROM User u
<where>
<choose>
<when test="username !=null ">
u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')
</when >
<when test="sex != null and sex != '' ">
AND u.sex = #{sex, jdbcType=INTEGER}
</when >
<when test="birthday != null ">
AND u.birthday = #{birthday, jdbcType=DATE}
</when >
<otherwise>
</otherwise>
</choose>
</where>
</select>

choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中 的 choose 很类似。如下:

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1 = 1
<choose>
<when test="title !=null">
and title = #{title}
</when>
<when test="content !=null">
and content =  #{content}
</when>
</otherwise>
</choose>
</select>

when元素表示当when中的条件满足的时候就输出其中的内容,跟JAVA中的swicth效果莱斯的是按照条件的顺序,当when中的有条件吗,满足的时候,就会跳出choose,即所有的when和otherwise条件中,只有一个会输出。当所有条件都不满足时就输出otherwise的内容。

2.selectKey标签
在insert语句中,在Oracle经常使用序列、在mysql中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键。使用mybatis的selectKey标签可以实现这个效果。下面例子,使用mysql数据库自定义函数nextval('student'),用来生成一个key,并把他设置到传入的实体类中的studentId属性上。所以在执行完此方法后,边可以通过这个实体类获取生成的key。
Xml代码:

<!-- 插入学生 自动主键-->
<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

Java代码:

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语句属性配置细节:



3.<trim>标签
事实上trim标签有点类似于replace效果。
trim 属性
prefix:前缀覆盖并增加其内容
suffix:后缀覆盖并增加其内容
prefixOverrides:前缀判断的条件
suffixOverrides:后缀判断的条件
比如:
select b.* from sys_menu b where 1 = 1

<trim suffix="WHERE" suffixOverrides="AND | OR">
<if test="id != null and id !='' ">
AND b.id =#{id}
</if>
<if test="name != null">
AND b.menu_name like #{name}
</if>
</trim>

最终打印为:
select b.*from sys_menu b where 1=1 AND b.menu_name like" WHERE
从结果可以发现:
1.<trim suffix="where" suffixOverrides="AND|OR">
suffix是针对符合suffixOverride的SQL语句追加后缀suffix值。

4.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());
}
}

5.foreach标签

在mybatis的mapper配置文件中,可以利用<foreach>标签实现sql条件的循环,可完成类似批量的sql
mybatis接受的参数分为:(1)基本类型(2)对象(3)List(4)数组(5)Map
无论传哪种参数给mybatis,他都会将参数放在一个Map中:
如果传入基本类型:变量名作为key,变量值作为value 此时生成的map只有一个元素。
如果传入对象: 对象的属性名作为key,属性值作为value,
如果传入List: "list"作为key,这个List是value (这类参数可以迭代,利用<foreach>标签实现循环)
如果传入数组: "array"作为key,数组作为value(同上)
如果传入Map: 键值不变。

<foreach>标签的用法:
六个参数:
collection:要循环的集合
index:循环索引(不知道啥用。。)
item:集合中的一个元素(item和collection,按foreach循环理解)
open:以什么开始
close:以什么结束
separator:循环内容之间以什么分隔
例如:

<update id="pubS" parameterType="Map">
UPDATE BMC_SUBPLATE
SET PLSTATUS = '02'
WHERE
<foreach collection="ids" item="plid" open="" close="" separator="OR">
PLID = #{plid}
</foreach>
</update>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: