老规矩先来回顾一下
目录
② [choose when otherwise] 和where
一、回顾
1. mybatis--->优化
(1)引入数据库属性文件
(2)引入日志文件
(3)解决列名和属性名不一致。【1】起别名--与属性名一致 【2】resultMap完成列和属性的映射。
2. 转义符: <![CDATA[sql]]> 注意尖括号和中括号成对出现哟
3. 多个参数的。@Param("参数名") #{参数名}
4. dao接口结合映射文件。namespace要和接口名一致 id要和接口中的方法名一致。
二、链表查询
1、多对一
(1)例如我们的学校或者公司,我们都有一个编号,通过这个编号可以查到我们的信息和所在的部门或班级
第一种方式 通过链表查询。
<resultMap id="My01" type="com.GP.entity.Student" >
<!--id必写-->
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="sex" column="sex"/>
<result property="age" column="sage"/>
<result property="cid" column="class_id"/>
<!--association:表示多对一
property:表示对象属性名
javaType:表示该对象所属的类型
autoMapping必须写
-->
<association property="clazz" javaType="com.Gzy.entity.Clazz" autoMapping="true">
</association>
</resultMap>
<!--注意:使用了resultMap不能在使用resultType-->
<select id="selectById" resultMap="My01" >
select * from student s join class c on s.class_id=c.classid where sid=#{id}
</select>
注:其中autoMapping有的默认就是true但是不排除特殊情况,所以建议都加上哟,以防万一。
第二种方式 通过嵌套查询。----两次查询。
注意:蓝色箭头的是需要传过去的参数,是两个表相同的内容,传递的一定要是多对一中“多”中的列名
2、一对多(了解)
第一种方式:链表查询,
例子:根据班级id查询班级信息以及该班级下所有的学生信息。
<resultMap id="My03" type="com.Gzy.entity.Clazz">
<id column="c_id" property="cid"/>
<result property="cname" column="c_name"/>
<!--
collection: 集合的意思 多的意思
property:集合对象名
ofType: 集合的泛型
-->
<collection property="students" ofType="com.Gzy.entity.Student" autoMapping="true">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
<result property="classId" column="class_id"/>
</collection>
</resultMap>
<!--这里的id必须和Dao中的方法名一致。-->
<select id="findById" resultMap="My03">
select * from class c join student s on c.c_id=s.class_id where c_id=#{id}
</select>
三、动态sql
何为动态sql顾名思义就是根据条件而发生变化的sql,我们的项目不是给我们自己用的,而是给广大客户用的,所以不同的用户在搜索时给出的条件不同,我们不可能每一个都列出来,这样既费时又费力还不一定能写全,所以需要动态sql在起到“编写sql语句”的作用。
下面我们用图书表来举例子
老样子先创建实体类并加上Dao接口
其次
①if和where一起用
<!--如果传入了书名 则按照书名进行查询 如果没有传入书名 则查询所有
where:可以帮你添加where关键 并且把第一个and | or去除
-->
<select id="selectBytj" resultMap="map">
select * from book_info
<where>
<if test="name!=null and name!=''">
and book_name=#{name}
</if>
<if test="author!=null and author!=''">
and book_author=#{author}
</if>
<if test="price!=null and price!=''">
and book_price=#{price}
</if>
<if test="pub!=null and pub!=''">
and book_pub=#{pub}
</if>
</where>
</select>
测试
② [choose when otherwise] 和where
<!--choose +where
when:当条件满足时不会继续执行,当when和other都不满足则执行otherwise
-->
<select id="findByCondition2" resultMap="map">
select * from book_info
<where>
<choose>
<when test="bookname!=null and bookname!=''">
and book_name=#{bookname}
</when>
<when test="author!=null and author!=''">
and book_author=#{author}
</when>
<otherwise>
and book_price>35
</otherwise>
</choose>
</where>
</select>
③set标签。修改部分字段
<!--修改部分列的值。
set 可以帮你添加set关键字 并且去除最后的逗号。
-->
<update id="update" >
update book_info
<set>
<if test="name!=null and name!=''">
book_name=#{name},
</if>
<if test="author!=null and author!=''">
book_author=#{author},
</if>
<if test="price!=null ">
book_price=#{price},
</if>
<if test="pub!=null and pub!=''">
book_pub=#{pub},
</if>
</set>
where book_id=#{id}
</update>
测试
④foreach批量删除或查找
<!--
delete from book_info where id in(1001,1002,1003)
in (1001,1002,1003)
foreach:
collection:要遍历的集合和数组名
item: 每次遍历时赋值的元素变量名
open: 以什么开始
close:以什么结束
separator: 分隔符
-->
<delete id="batchDelete">
delete from book_info where book_id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<select id="selectByIds" resultMap="map">
select * from book_info where book_id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
测试
总结
1. 链表查询
①多对一 第一种实现方式: 链表查询 第二种嵌套查询。
②一对多 了解。
2. 动态sql
if标签
choose when otherwise
where set 解决sql拼接问题
foreach:
循环。