1.链表查询
1.1多对一
(1) 根据订单id查询订单信息以及该订单对应的用户信息。
第一种方式 通过链表查询。
<resultMap id="My01" type="com.aaa.entity.Order" >
<!--id必写-->
<id property="id" column="order_id"/>
<result property="no" column="order_no"/>
<result property="price" column="order_price"/>
<result property="num" column="num"/>
<!--association:表示多对一
property:表示对象属性名
javaType:表示该对象所属的类型
autoMapping必须写
-->
<association property="user" javaType="com.aaa.entity.User" autoMapping="true">
<!--User和User表的对应关系-->
<id property="id" column="id"/>
</association>
</resultMap>
<!--注意:使用了resultMap不能在使用resultType-->
<select id="selectById" resultMap="My01" >
select * from orders o join users u on o.uid=u.id where o.order_id=#{id}
</select>
第二种方式 通过嵌套查询。----两次查询。
1.2:一对多
1.从一的一方查询多的一方。
比如: 班级--1---n-->学生.
CREATE TABLE class(
c_id INT PRIMARY KEY AUTO_INCREMENT,
c_name VARCHAR(20),
);
INSERT INTO class(c_name) VALUES('QY145');
INSERT INTO class(c_name) VALUES('QY143');
INSERT INTO class(c_name) VALUES('QY142');
CREATE TABLE student(
s_id INT PRIMARY KEY AUTO_INCREMENT,
s_name VARCHAR(20),
class_id INT
);
INSERT INTO student(s_name, class_id) VALUES('xs_A', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_B', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_C', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_D', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_E', 3);
INSERT INTO student(s_name, class_id) VALUES('xs_F', 3);
第一种方式:链表查询,
例子:根据班级id查询班级信息以及该班级下所有的学生信息。
<resultMap id="My03" type="com.aaa.entity.Clazz">
<id column="c_id" property="cid"/>
<result property="cname" column="c_name"/>
<!--
collection: 集合的意思 多的意思
property:集合对象名
ofType: 集合的泛型
-->
<collection property="students" ofType="com.aaa.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>
2.动态sql语句
sql语句根据条件而发生改变。
(1)if和where一起用
<!--如果传入了书名 则按照书名进行查询 如果没有传入书名 则查询所有
where:可以帮你添加where关键 并且把第一个and | or去除
-->
<select id="findByCondition" resultMap="map">
select * from book_info
<where>
<if test="bookname!=null and bookname!=''">
and book_name=#{bookname}
</if>
<if test="author!=null and author!=''">
and book_author=#{author}
</if>
</where>
</select>
测试:
@Test
public void testSelect01(){
BookDao bookDao = session.getMapper(BookDao.class);
Map<String,Object> map=new HashMap<String,Object>();//从网页中得到参数值 并且封装到map对象中。
map.put("bookname1","金瓶梅");
map.put("author","罗贯中");
List<Book> list = bookDao.findByCondition(map);
}
(2) [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>
@Test
public void testSelect02(){
BookDao bookDao = session.getMapper(BookDao.class);
Map<String,Object> map=new HashMap<String,Object>();//从网页中得到参数值 并且封装到map对象中。
// map.put("bookname","金瓶梅");
// map.put("author","罗贯中");
List<Book> list = bookDao.findByCondition2(map);
}
(3)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="pub!=null and pub!=''">
book_pub=#{pub},
</if>
<if test="price!=null">
book_price=#{price},
</if>
</set>
where book_id=#{id}
</update>
@Test
public void testUpdate(){
BookDao bookDao = session.getMapper(BookDao.class);
Book book=new Book();
book.setAuthor("金庸");
book.setName("神雕效率");
book.setId(1002);
bookDao.update(book);
session.commit();
}
(4) 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>
@Test
public void testUpdat2e(){
BookDao bookDao = session.getMapper(BookDao.class);
int [] ids={1001,1002,1003};
bookDao.batchDelete(ids);
session.commit();
}