发表于: 2017-09-27 23:03:58
1 689
今天完成的事情:今天没做任务,因为第一次做小课堂,而且讲的题目又不太了解,所以多花时间准备了,小课堂讲了mybatis 的动态查询,总结如下:
我们在使用jdbc编程的时候,往往会去拼装sql语句,拼装的时候往往根据条件的不同拼装不同的sql语句
你就能体会到根据不同条件拼接 SQL 语句有多么痛苦。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
mybatis如何来处理这个过程呢,它用的是动态sql技术,动态sql技术说的直白一点就是动态生成sql技术,那么mybatis如何来动态生成sql呢,他是通过配置文件里的各种标记来动态生成sql的,这些标记有很多种,我们来看一下,比如说表示判断的if choose,条件标记where,赋值标记set,和trim格式化标记,foreach循环标记,下面我们一个一个来看一下
一. if标记表示单一的一个条件的判断
其中where 1=1的意思是不管条件是否为true,程序都可以运行下去
"and"是为了拼接语句的时候不出错
如果name!=null
无where1=1无and:select*from person name like '${name}%'正常
有where1=1无and:select*from person where 1=1 name like '${name}%'报错
有where1=1有and:select*from person where 1=1 and name like '${name}%'正常
无where1=1有and:select*from and person name like '${name}%' 报错
如果name==null
无where1=1:select*from person报错
有where1=1:select*from person where 1=1正常
所以如果使用if标签,要加上where1=1 和and
<!--if标签-->
<!--拼接结果select*from person where name like '张全蛋%'-->
<!--select * from user where id=#{id}-->
<select id="queryByIf" resultType="com.mycom.myapp.model.Person">
select*from person where 1=1
<if test="name!=null">
and name like '${name}%'
</if>
</select>
二.choose
if标记表示单一的一个条件的判断
choose表示多个条件的判断 ,有点像java当中的if和switch case ,它只拼接一条语句,只要一条语句满足,就拼接然后退出.
<!--choose标签,只拼接一条语句-->
<!--拼接结果 select*from person where 1=1 and name like '张全蛋%'-->
<select id="queryByChoose" resultType="com.mycom.myapp.model.Person">
select*from person where 1=1
<choose>
<when test="name!=null">
and name like '${name}%'
</when>
<when test="address!=null">
and address = #{address}
</when>
<otherwise>
order by name
</otherwise>
</choose>
</select>
3.trim标签
trim标签作用是解决where 1=1的问题,自动判断前后缀字符是否"去除"
<!--trim标签作用是解决where 1=1的问题,自动判断前后缀字符是否"去除"-->
<select id="queryByTrim" resultType="com.mycom.myapp.model.Person">
select * from person
<trim prefix="where" prefixOverrides="AND / OR">
<if test="name!=null">
name like '${name}%'
</if>
<if test="address!=null">
and address = #{address}
</if>
</trim>
</select>
4.where标签
<where>标签解决where 1=1,自动清除and,where和trim功能差不多
<!--<where>标签解决where 1=1,自动清除and,where和trim功能差不多-->
<select id="queryByWhere" resultType="com.mycom.myapp.model.Person">
select * from person
<where>
<if test="name!=null">
name like '${name}%'
</if>
<if test="address!=null">
and address = #{address}
</if>
</where>
</select>
5.foreach
<!--foreach标记用于循环的查询和循环的赋值-->
循环查询
<!--foreach标记用于循环的查询和循环的赋值-->
<select id="selectByList" resultType="com.mycom.myapp.model.Person" parameterType="list">
select * from person
<where>
id in
<foreach collection="list" index="index" open="(" separator="," close=")" item="item">
#{item}
</foreach>
</where>
</select>
<!--foreach批量赋值操作-->
<!--insert into user (id,name,age)values(#{id},#{name},#{age})-->
<insert id="insertByList" >
insert into person (id,name,money,address) values
<foreach collection="list" index="key" open="" separator="," close="" item="item" >
( #{item.id},#{key},#{item.money},#{item.address})
</foreach>
</insert>
明天计划完成的事情:明天找一个能在页面上实现增删改查的项目实现一下,任务二要抓紧时间了.
遇到的问题:今天遇到一个问题,就是用foreach标签删除数据库类容时.代码运行没问题,但是数据库无变化,不知道为什么
<!--delete from user where id =#{id}-->
<delete id="deleteByList" >
delete from person where id in
<foreach collection="list" open="(" separator="," close=")" item="haha">
${haha}
</foreach>
</delete>
收获:学习了mybatis动态语句,了解sql语句学习的重要性.
评论