之前咱们就说过,MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索。MyBatis 可以使用简单的XML 或注解用于配置和原始映射。那么接下来怎么就用Mybatis进行数据的CRUD
一,链表查询
概念: 结点之间不要求连续存放,因此在保存大量数据时,不需要分配一块连续的存储空间。用户可以用new函数动态分配结点的存储空间,当删除某个结点时,给该节点赋值null,释放其占用的内存空间。
1,多对一
(1)通过链表查询
<resultMap id="My01" type="com.ykq.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.ykq.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>
(2)通过嵌套查询
<resultMap id="My02" type="com.aaa.mcl.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"/>
<!--column:把查询的哪一列作为另一个查询的条件值
select:表示引用另一个查询
-->
<association property="user" javaType="com.aaa.mcl.entity.User"
column="uid" select="com.aaa.mcl.dao.UserDao.selectById">
</association>
</resultMap>
<!--嵌套查询-->
<select id="selectById2" resultMap="My02">
select * from orders where order_id=#{id}
</select>
<select id="selectById" resultType="com.aaa.mcl.entity.User">
select * from user where id=#{id}
</select>
2,一对多
例子:根据班级id查询班级信息以及该班级下所有的学生信息。
<resultMap id="My03" type="com.ykq.entity.Clazz">
<id column="c_id" property="cid"/>
<result property="cname" column="c_name"/>
<!--
collection: 集合的意思 多的意思
property:集合对象名
ofType: 集合的泛型
-->
<collection property="students" ofType="com.ykq.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操作的时候,传入的参数对象或者参数值,根据匹配的条件,有可能需要动态的去判断是否为空,循环,拼接等情况;
1,if和where一起使用
<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对象中。
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();
}
三,模糊查询
系统允许被搜索信息和搜索提问之间存在一定的差异,这种差异就是“模糊”在搜索中的含义。例如,查找名字Smith时,就会找出与之相似的Smithe, Smythe, Smyth, Smitt等。
<select id="findAll3" resultMap="EmpMap">
select
<include refid="sql01"/>
from tbl_emp e join tbl_dept d on e.d_id=d.deptId
<where>
<if test="empName!=null and empName!=''">
empName like concat('%',#{empName},'%')
</if>
</where>
</select>
测试
@Test
public void testUpdate2(){
EmpDao empDao = session.getMapper(EmpDao.class);
List<Emp> list = empDao.findAll3("王");
System.out.println(list);
session.commit();
}
四,分页查询
1,引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
2,加入拦截
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
3,测试
@Test
public void testUpdate3(){
EmpDao empDao = session.getMapper(EmpDao.class);
//使用分页功能 request.getParamter("page") request.getParamter("pageSize")
PageHelper.startPage(2,10);
List<Emp> list = empDao.findAll3("王");
//可以把查询的结果封装到PageInfo类中 包含你想要的任何信息
PageInfo<Emp> pageInfo=new PageInfo<Emp>(list);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("当前页码的数据:"+pageInfo.getList());
}