1.分页
分页的目的:减少数据的处理量
用Mybatis实现分页
- 接口
//分页查询
List<student> getstuBylimit(Map<String,Integer> map);
- Mapper.Xml
<!--分页查询-->
<select id="getstuBylimit" parameterType="map" resultType="com.wang.pojo.student">
select * from student limit #{startindex},#{pagesize};
</select>
- 测试
@Test
public void getlimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StuMapper mapper = sqlSession.getMapper(StuMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startindex",2);
map.put("pagesize",2);
List<student> students = mapper.getstuBylimit(map);
for (student student : students) {
System.out.println(student);
}
}
核心就是实现limit查询
2.使用注解开发
- 在mybatis的核心配置文件中mapper的注册可以解绑,改为绑定接口类
<mappers>
<mapper class="com.wang.dao.StuMapper"></mapper>
</mappers>
- 接口注解
//注解开发
@Select("select * from student")
List<student> getstu();
- 测试
@Test
public void getlimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StuMapper mapper = sqlSession.getMapper(StuMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startindex",2);
map.put("pagesize",2);
List<student> students = mapper.getstuBylimit(map);
for (student student : students) {
System.out.println(student);
}
}
- 使用注解开发时,若接口的方法中含有多个参数则每个参数前面必须加上@Param注解
- 在注解sql中取到的参数是根据@Param来获取的,xml方式同样!!
- @Param注解,若参数是基本数据类型需要加上,引用数据类型则不需要加上,如果只有一个就基本类型的话,可以忽略
3. 建立复杂的查询环境
1. 多对一模式
使用嵌套查询
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.dao.StudentMapper">
<select id="getStudent" resultMap="StudentTeacher">
select * from student;
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!-- 复杂的属性单独处理-->
<!-- 对象用association,集合用collection-->
<association property="teacher" column="id" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{id};
</select>
</mapper>
按照结果集查询 可以写直接的sql语句,之后将属性一一对应即可
<select id="getStudent" resultMap="StudentTeacher">
select * from teacher,student where student.id = teacher.id;
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="sid"/>
<result property="name" column="name"/>
<association property="teacher" javaType="Teacher">
<result property="id" column="tid"></result>
</association>
</resultMap>
2.一对多模式
小结
- 关联 -association 多对一
- 集合 collection 一对多
- Javatype or oftype
- javatype 用来指定实体类中的类型
- oftype 用来指定泛型的约束类型
4. 动态SQL
这个特性是解决sql语句拼接时造成的麻烦
1. if标签
<select id="if" parameterType="map">
select * from student where
<if test="title != null">
title = #{title}
</if>
</select>
2. choose标签 (类似于switch标签 )
<select id="choose" parameterType="map">
select * from student
<where>
<choose>
<when test="title != null">
title = #{title}
</when>
<when test="author != null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
更新时用set’标签
3. sql标签
是把sql语句单独写在一起,然后通过include标签 引入
sql标签里不能存在where标签
<sql id="if-t-a">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
<select id="if" parameterType="map">
select * from student
<where>
<include refid="if-t-a"></include>
</where>
</select>
4. Foreach标签(对集合遍历)
拼接
select * from student where 1=1 and (id= 1 or id= 2 or id =3)
<select id="foreach" parameterType="map" resultMap="blog">
select * from student
<where>
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
- collection 是集合名称
- item 是每一个元素的名称
- open :是拼接的开始
- close:拼接的结束
- separator:分隔符
- 测试的时候先创建一个map,在创建一个list加入map里面
- colection中的值是key 需要用集合list来接收,从list中add值来遍历
小结
- 动态sql就是根据不同的条件生成不同的sql语句
和编码一样,因为有标签的存在,更具有逻辑性和可读性!! - 建议先写好sql语句再去拼接
5.缓存
1. 一级缓存 :基于SqlSession
- 也叫本地缓存:SqlSession 默认情况下开启了一级缓存
2.二级缓存: 基于Mapper
- 需要手动开启,显示开启全局缓存
<setting name="cacheEnable" value="true"/>
在mapper.xml中使用二级缓存
也可以自定义参数
<cache/>
3.自定义缓存
Ehcache
- 导包
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.1</version>
</dependency>
- 在mapper.xml中配置
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
小结
- 缓存顺序
二级缓存 —》 一级缓存 —》 数据库