没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
mybatis
动态
SQL
语句
分类: mybatis2011-04-13 13:34 7889 人阅读 评论(2) 收藏 举报
sql null list xml string java
目录(?) [+]
三、动态 SQL 语句
有些时候,sql 语句 where 条件中,需要一些安全判断,例如按性别检索,如果传入
的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部
的信息。这是我们可以使用动态 sql,增加一个判断,当参数不符合要求的时候,我们可以
不去判断此查询条件。
下文均采用 mysql 语法和函数(例如字符串链接函数 CONCAT)。
源代码 http://limingnihao.javaeye.com/admin/blogs/782190 页面最下面;
3.1 if 标签
一个很普通的查询:
Xml 代码
1 <!-- 查询学生 list,like 姓名 -->
2 <select id="getStudentListLikeName" parameterType="StudentEntity"
resultMap="studentResultMap">
3 SELECT * from STUDENT_TBL ST
4 WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
5 </select>
6 <!-- 查询学生 list,like 姓名 -->
7 <select id="getStudentListLikeName" parameterType="StudentEntity"
resultMap="studentResultMap">
8 SELECT * from STUDENT_TBL ST
9 WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
10 </select>
但是此时如果 studentName 是 null 或空字符串,此语句很可能报错或查询结果为空。此时
我们使用 if 动态 sql 语句先进行判断,如果值为 null 或等于空字符串,我们就不进行此条件
的判断。
修改为:
Xml 代码
11 <!-- 查询学生 list,like 姓名 -->
12 <select id=" getStudentListLikeName " parameterType="StudentEntity"
resultMap="studentResultMap">
13 SELECT * from STUDENT_TBL ST
14 <if test="studentName!=null and studentName!='' ">
15 WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%',
#{studentName}),'%')
16 </if>
17 </select>
18 <!-- 查询学生 list,like 姓名 -->
19 <select id=" getStudentListLikeName " parameterType="StudentEntity"
resultMap="studentResultMap">
20 SELECT * from STUDENT_TBL ST
21 <if test="studentName!=null and studentName!='' ">
22 WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%',
#{studentName}),'%')
23 </if>
24 </select>
此时,当 studentName 的值为 null 或’’的时候,我们并不进行 where 条件的判断,所以当
studentName 值为 null 或’’值,不附带这个条件,所以查询结果是全部。
由于参数是 Java 的实体类,所以我们可以把所有条件都附加上,使用时比较灵活, new
一个这样的实体类,我们需要限制那个条件,只需要附上相应的值就会 where 这个条件,
相反不去赋值就可以不在 where 中判断。
代码中的 where 标签,请参考 3.2.1.
Xml 代码
25 <!-- 查询学生 list,like 姓名,=性别、=生日、=班级,使用 where,参数 entity 类型
-->
26 <select id="getStudentListWhereEntity" parameterType="StudentEntity"
resultMap="studentResultMap">
27 SELECT * from STUDENT_TBL ST
28 <where>
29 <if test="studentName!=null and studentName!='' ">
30 ST.STUDENT_NAME LIKE CONCAT(CONCAT('%',
#{studentName}),'%')
31 </if>
32 <if test="studentSex!= null and studentSex!= '' ">
33 AND ST.STUDENT_SEX = #{studentSex}
34 </if>
35 <if test="studentBirthday!=null">
36 AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
37 </if>
38 <if test="classEntity!=null and classEntity.classID !=null and
classEntity.classID!='' ">
39 AND ST.CLASS_ID = #{classEntity.classID}
40 </if>
41 </where>
42 </select>
43 <!-- 查询学生 list,like 姓名,=性别、=生日、=班级,使用 where,参数 entity 类型
-->
44 <select id="getStudentListWhereEntity" parameterType="StudentEntity"
resultMap="studentResultMap">
45 SELECT * from STUDENT_TBL ST
46 <where>
47 <if test="studentName!=null and studentName!='' ">
48 ST.STUDENT_NAME LIKE CONCAT(CONCAT('%',
#{studentName}),'%')
49 </if>
50 <if test="studentSex!= null and studentSex!= '' ">
51 AND ST.STUDENT_SEX = #{studentSex}
52 </if>
53 <if test="studentBirthday!=null">
54 AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
55 </if>
56 <if test="classEntity!=null and classEntity.classID !=null and
classEntity.classID!='' ">
57 AND ST.CLASS_ID = #{classEntity.classID}
58 </if>
59 </where>
60 </select>
查询,姓名中有‘李’,男,生日在‘1985-05-28’,班级在‘20000002’的学生。
Java 代码
61 StudentEntity entity = new StudentEntity();
62 entity.setStudentName("李");
63 entity.setStudentSex("男");
64 entity.setStudentBirthday(StringUtil.parse("1985-05-28"));
65 entity.setClassEntity(classMapper.getClassByID("20000002"));
66 List<StudentEntity> studentList =
studentMapper.getStudentListWhereEntity(entity);
67 for( StudentEntity entityTemp : studentList){
68 System.out.println(entityTemp.toString());
69 }
70 StudentEntity entity = new StudentEntity();
71 entity.setStudentName("李");
72 entity.setStudentSex("男");
73 entity.setStudentBirthday(StringUtil.parse("1985-05-28"));
74 entity.setClassEntity(classMapper.getClassByID("20000002"));
75 List<StudentEntity> studentList =
studentMapper.getStudentListWhereEntity(entity);
76 for( StudentEntity entityTemp : studentList){
77 System.out.println(entityTemp.toString());
78 }
3.2 where、set、trim 标签
3.2.1 where
当 if 标签较多时,这样的组合可能会导致错误。例如,like 姓名,等于指定性别等:
Xml 代码
79 <!-- 查询学生 list,like 姓名,=性别 -->
80 <select id="getStudentListWhere" parameterType="StudentEntity"
resultMap="studentResultMap">
81 SELECT * from STUDENT_TBL ST
82 WHERE
83 <if test="studentName!=null and studentName!='' ">
84 ST.STUDENT_NAME LIKE CONCAT(CONCAT('%',
#{studentName}),'%')
85 </if>
86 <if test="studentSex!= null and studentSex!= '' ">
87 AND ST.STUDENT_SEX = #{studentSex}
88 </if>
89 </select>
90 <!-- 查询学生 list,like 姓名,=性别 -->
91 <select id="getStudentListWhere" parameterType="StudentEntity"
resultMap="studentResultMap">
剩余46页未读,继续阅读
资源评论
程序员阿苏
- 粉丝: 756
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功