MySQL数据库之MyBatis动态SQL实例讲解
小标
2019-03-06
来源 :
阅读 1292
评论 0
摘要:本文主要向大家介绍了MySQL数据库之MyBatis动态SQL实例讲解 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。
本文主要向大家介绍了MySQL数据库之MyBatis动态SQL实例讲解 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。
在开发中,经常会遇到要执行的 SQL 语句其实并不是固定,而是随条件的变化而变化的。对于这种情况 MyBatis 也有解决方案。
随条件变化的 SQL
先看一个固定的 SQL 语句,查询指定 name 和 age 的人:
1 2 3 4 5 6 7 8 9 10 11 | <resultMap id= "PersonMap" type= "Person" >
<id column= "id" jdbcType= "INTEGER" property= "pid" javaType= "int" />
<result column= "name" jdbcType= "VARCHAR" property= "pname" javaType= "String" />
<result column= "age" jdbcType= "INTEGER" property= "page" javaType= "int" />
</resultMap>
<select id= "selectWithNothing" parameterType= "Person" resultMap= "PersonMap" >
SELECT id, name, age
FROM person
WHERE name = #{name} and age = #{age}
</select>
|
上面的 SQL 当 name 或 age 为空时,该查询将会抛出异常。
if 标签
在不确定查询条件是否为空时,可以用 if 标签进行检查:
1 2 3 4 5 6 7 8 9 10 11 12 | <select id= "selectWithIf" parameterType= "Person" resultMap= "PersonMap" >
select id, name, age
from person
where 1 = 1
< if test= "pname != null and pname != '' " >
and name = #{pname}
</ if >
< if test= "page != null and page != 0 " >
and age < #{page}
</ if >
</select>
|
注意:where 后的 1 = 1 建议加上,这样可以避免无一条件满足时,最终的 SQL 语句中 where 后面没有过滤条件。
调用方法如下:
1 2 3 4 5 6 7 8 9 | public void selectWithIf(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
Person entity = new Person();
entity.setPname( "Jimmy" );
List<person> persons = sqlSession.selectList( "edu.wzm.mybatis.mapping.PersonMapper.selectWithIf" , entity);
System. out .println(persons);
sqlSession. close ();
}</person>
|
where - if 标签
在 SELECT 语句中,为了简化 if 标签,可以使用 where - if 标签组合使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <select id= "selectWithIfWhere" parameterType= "Person" resultMap= "PersonMap" >
selectid, name, age
from person
<where>
< if test= "pname != null and pname != '' " >
and name = #{pname}
</ if >
< if test= "page != null and page != 0 " >
and age < #{page}
</ if >
</where>
</select>
|
在 where - if 标签中,SQL 语句可以加上 and,MyBatis 会自动识别。
调用方法:
1 2 3 4 5 6 7 8 9 10 | public void selectWithIfWhere(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
Person entity = new Person();
entity.setPname( "Jimmy" );
entity.setPage(30);
List<person> persons = sqlSession.selectList( "edu.wzm.mybatis.mapping.PersonMapper.selectWithIfWhere" , entity);
System. out .println(persons);
sqlSession. close ();
}</person>
|
set - if 标签
在 UPDATE 语句中,为了简化 if 标签,可以使用 set - if 标签组合使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <update id= "updateWithIfSet" parameterType= "Person" >
updateperson
<set>
< if test= "pname != null and pname != '' " >
name = #{pname}
</ if >
< if test= "page != null and page != 0 " >
and age = #{page}
</ if >
</set>
where id = #{pid}
</update>
|
set 标签为自动为每个 if 标签中的 SQL 语句加上逗号隔开。
调用方法:
1 2 3 4 5 6 7 8 9 10 | public void updateWithIfSet(){
SqlSession sqlSession = MyBatisUtils.getSqlSession( true );
Person entity = new Person();
entity.setPid(2);
entity.setPname( "Bill" );
int result = sqlSession. update ( "edu.wzm.mybatis.mapping.PersonMapper.updateWithIfSet" , entity);
System. out .println(result);
sqlSession. close ();
}
|
trim - if 标签
还有一种更强大的组合标签 trim - if,它既可以处理 SELECT语句,又可以处理 UPDATE 语句。如代替上面的 where - if 标签:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <select id= "selectWithIfTrim" parameterType= "Person" resultMap= "PersonMap" >
SELECT id,name, age
FROM person
<trim prefix= "WHERE" prefixOverrides= "AND|OR" >
< if test= "pname != null and pname != '' " >
name = #{pname}
</ if >
< if test= "page != null and page != 0 " >
age < #{page}
</ if >
</trim>
</select>
|
代替上面的 set - if 标签:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <update id= "updateWithIfTrim" parameterType= "Person" >
UPDATE person
<trim prefix= "SET" prefixOverrides= "," >
< if test= "pname != null and pname != '' " >
name = #{pname}
</ if >
< if test= "page != null and page != 0 " >
age < #{page}
</ if >
</trim>
WHERE id = #{pid}
</update>
|
choose - when 标签
上面的标签都是一组条件的中的每一个条件要么可选,要么不选。但是对于一组互斥的条件,只能从中选择一个,那么上面的标签就不好处理了,需要使用 choose - when 标签:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <select id= "selectWithChooseWhen" parameterType= "Person" resultMap= "PersonMap" >
SELECT id, name, age
FROM person
<where>
<choose>
<when test= "pname != null and pname != '' " >
&n
|
本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!
本文由 @小标 发布于职坐标。未经许可,禁止转载。
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢
快给朋友分享吧~
评论(0)