一、mybatis传参
1.0.1简单类型传参
Dao接口中方法
/**
* 查询所有部门员工
* @param jobId
* @return list
*/
List<Emp> findByJobId(Integer jobId);
mapper文件
<select id="findByJobId" parameterType="int" resultType="com.example.domain.Emp">
select * from emp where job_id=#{job}
</select>
结论:dao接口是一个简单类型参数,mapper文件获取这个参数值,使用#{任意字符}
1.0.2多个不同类型传参
Dao接口中方法
/**
* 查询满足的list集合
* @param name
* @param salary
* @return list
*/
List<Emp> findByNameOrSalary(@Param("myname") String name,@Param("mysalary") Integer salary);
mapper文件
<!--根据名字或工资查询对象-->
<select id="findByNameOrSalary" resultType="com.example.domain.Emp">
select * from emp where name = #{myname} or salary = #{mysalary}
</select>
结论:多个简单类型的参数传递时,当使用了@Param命名后,例如@Param(“myname”),在mapper文件中,使用#{命名参数},例如 #{myname}
1.0.3对象传参
Dao接口中方法
/**
* 根据对象中保存的属性查询emp对象
* @param emp
* @return
*/
List<Emp> findByEmp(Emp emp);
mapper文件
<select id="findByEmp" resultType="com.example.domain.Emp" parameterType="com.example.domain.Emp">
select * from emp where name = #{name} or salary=#{salary} or job_id = #{job_id}
</select>
结论:一个java对象作为方法的参数,使用对象的属性作为参数值使用。简单的语法:#{属性名},mybatis调用此属性的getXxx()方法获取属性值
1.0.4对象传参之指定类型
Dao接口中方法
/**
* 根据指定累心查找对象
* @return list
*/
List<Emp> selectByType(Emp emp);
mapper文件
<select id="selectByType" resultType="com.example.domain.Emp">
select * from emp where name =#{name,javaType=java.lang.String,jdbcType=VARCHAR}
or
salary=#{salary,javaType=java.lang.Integer,jdbcType=INTEGER}
</select>
结论:当dao接口中传参是一个对象无法指定时,在mapper文件中指定 传参类型(javaType=java.lang.Integer) 和 数据类型(jdbcType=INTEGER)进行传参。
1.0.5使用位置传递参数值
Dao接口中方法
/**
* 按位置传递参数
* @param name
* @param salary
* @return list
*/
List<Emp> selectByPosition(String name,Integer salary);
mapper文件
<!--按位置传递参数-->
<select id="selectByPosition" resultType="com.example.domain.Emp">
select * from emp where name=#{arg0} or salary <![CDATA[<]]> #{arg1}
</select>
结论:使用位置获取参数值,dao接口方法是多个简单类型的参数。语法:#{arg0},#{arg1} . . .
1.0.6【了解】多个参数 - 使用Map
Dao接口中方法
/**
* 使用map作为参数
* @param map
* @return list
*/
List<Emp> selectByMap(Map<String,Object> map);
mapper文件
<select id="selectByMap" parameterType="map" resultType="com.example.domain.Emp">
select * from emp where name=#{myname} or salary <![CDATA[<]]> #{mysalary}
</select>
测试,调用处
@Test
public void testSelectByMap(){
SqlSession sqlSession = BaseUnits.getSqlSession();
EmpDao empDao = sqlSession.getMapper(EmpDao.class);
Map<String, Object> map = new HashMap<>();
map.put("myname", "张艺兴");
map.put("mysalary", 5000);
List<Emp> emps = empDao.selectByMap(map);
for (Emp emp : emps) {
System.out.println(emp);
}
}
结论:Map集合可以存储多个值,使用Map向mapper文件一次传入多个参数,Map集合使用String的key,Object类型的值存储参数。mapper文件使用#{key}引用参数值。
二、#和$占位符的区别
2.0.1#占位符
语法:#{字符}
mybatis处理#{ }使用jdbc对象是PrepareStatment对象
<select id="findByJobId" parameterType="int" resultType="com.example.domain.Emp">
select * from emp where job_id=#{job}
</select>
mybatis创建PrepareStatment对象,执行sql语句
String sql = “select * from emp where job_id=?”
PrepareStatment pst = conn.prepareStatment(sql);
pst.setInt(1,1001); //传递参数
Resultset rs = pst.executeQuery(); // 执行sql语句
#{}特点:
1)使用的PrepareStatment对象,执行sql语句,效率高
2)使用的PrepareStatment对象,能避免sql语句,sql语句执行更安全
3)#{}常常作为列值使用的,位于等号的右侧,#{}位置的值和数据类型有关的
2.0.2$占位符
语法:${字符}
mybatis执行${}占位符的sql语句
<select id="findByJobId" parameterType="int" resultType="com.example.domain.Emp">
select * from emp where job_id=${job}
</select>
${}表示字符串连接,把sql语句的其他内容和 ${} 内容使用 字符串(+)连接的方式连接一起 String sql = “select * from emp where job_id=”+“1001”;
mybatis创建Statement对象,执行sql语句
Statement stmt = conn.createStatement(sql);
ResultSet rs = stmt.executeQuery();
$ {}的特点
1)使用Statement对象,执行sql语句,效率低
2)$ {}占位符的值,使用的字符串连接方式,有sql注入的风险。有代码安全的问题
3)$ {}数据是原样使用的,不会区分数据类型
4)$ {}常用作 表名或者列名,在能保证数据安全的情况下使用 ${}
三、封装MyBatis输出结果
3.0.1输出resultType对象类型
resultType属性:在执行select时使用,作为标签的属性出现的。
resultType:表示结果类型,mysql执行sql语句,得到Java对象的类型。它的值由两种
1)Java类型的全限定名称
2)使用别名
dao
/**
* 根据id查询Province对象
* @param id id
* @return Province
*/
Province findById(Integer id);
mapper
<!--根据id查询Province对象-->
<select id="findById" resultType="com.example.domian.Province">
select * from province where id = #{id}
</select>
resultType:现在使用Java啥类型的全限定名称。表示的意思mybatis执行sql,把ResultSet中的数据转为Province对象。mybatis会做以下操作:
1.调用com.example.domian.Province的无参构造方法,创建对象。
Province province = new Province(); //使用反射创建对象
2.同名的列赋值给同名的属性
province.setId(rs.getInt(“id”));
province.setName(rs.getString(“name”));
3.得到java对象,如果dao接口返回值是List集合,mybatis把province对象放入到List集合。
3.0.2输出resultType的简单类型
dao
/**
* 查询Province表的总数量
* @return
*/
long countProvince();
mapper
<!--查询Province表的总数量-->
<select id="countProvince" resultType="long">
select count(*) from province
</select>
3.0.3输出Map结构数据
dao
/***
* 查询结果封装成map集合
* @param id
* @return
*/
Map<Object,Object> selectMap(Integer id);
mapper
<!--查询结果封装成map集合-->
<select id="selectMap" resultType="map" parameterType="int">
select * from province where id=#{id}
</select>
测试
/*查询结果封装成map集合*/
@Test
public void selectMap(){
SqlSession sqlSession = BaseUtils.getSqlSession();
ProvinceDao provinceDao = sqlSession.getMapper(ProvinceDao.class);
Map<Object, Object> objectMap = provinceDao.selectMap(3);
System.out.println("id="+objectMap.get("id"));
System.out.println("name="+objectMap.get("name"));
//关闭sqlSession对象
sqlSession.close();
}
3.0.4输出vo值对象类型
dao
/*根据Province对象查询对应城市*/
List<ProvinceToCity> selectProvinceToCity(Integer id);
mapper
<!--根据Province对象查询对应城市-->
<select id="selectProvinceToCity" resultType="com.example.vo.ProvinceToCity" parameterType="int">
select p.id, p.name, c.id cid,c.name cname
from province p
INNER JOIN city c on p.id=#{id} and p.id=c.city_id;
</select>
内连接sql语句例子
select p.id, p.name, c.id cid,c.name cname
from province p
INNER JOIN city c on p.id=1 and p.id=c.city_id;
3.0.5输出resultMap类型对象
resultType:‘结果映射。自定义列名和java对象属性的对应关系。
用法:
1.先定义resultMap标签,指定列名和属性名称对应关系
2.在select标签使用resultMap属性,指定上面定义的resultMap的id的值
dao
/**
* 根据Province对象查询对应城市
* @param id
* @return
*/
List<ProvinceToCity> selectProToCityByResultMap(Integer id);
mapper中定义resultMap的映射对象,并指定数据库列与对象属性对应
<!--定义resultMap
id:给resultMap的映射关系起一个名称,唯一值
type:java类型的全限定名称
-->
<resultMap id="ProvinceToCity" type="com.example.vo.ProvinceToCity">
<!--定义列名和属性名的对应-->
<!--主键类型使用id标签-->
<id column="id" property="id"></id>
<!--非主键类型是哟了那个result标签-->
<result column="name" property="name"></result>
<!--列名和属性名相同不用定义-->
<result column="cid" property="cid"></result>
<result column="cname" property="cname"></result>
</resultMap>
<!--使用resultMap属性,指定映射关系的id-->
<select id="selectProToCityByResultMap" resultMap="ProvinceToCity">
select p.id, p.name, c.id cid,c.name cname
from province p
INNER JOIN city c on p.id=#{id} and p.id=c.city_id;
</select>
3.0.6列名和java对象属性名称不一样解决方式
1)使用resultMap:自定义列名和属性名称对应关系
2)使用resultType:使用列别名,让别名和java对象属性名称一样
3.0.7使用列别名,让别名和java对象属性名称一样
dao
/**
* 给查询字段取别名和对象属性名对应
* @param id
* @return
*/
List<ProvinceToCity> selectProToCityById(Integer id);
mapper
<!--给查询字段取别名和对象属性名对应-->
<select id="selectProToCityById" resultType="com.example.vo.ProvinceToCity" parameterType="int">
select p.id,p.name, c.id as cid,c.name as cname
from province p
INNER JOIN city c on p.id=c.city_id and p.id=#{id}
</select>
3.0.8模糊Like
模糊查询的实现有两种方式,一是java代码中给查询数据加上“%”;二是在mapper文件sql语句的条件位置加上“%”。
第一种查询数据加上“%”:
dao
/**
* 根据名字模糊查询city对象并放入列表方法1
* @param name
* @return
*/
List<City> findCityByLike1(String name);
<!--根据名字模糊查询city对象并放入列表方法1-->
<select id="findCityByLike1" resultType="com.example.domian.City" parameterType="string">
select id,name,city_id from city where name like #{name}
</select>
测试
@Test
public void findCityByLike1(){
SqlSession sqlSession = BaseUtils.getSqlSession();
CityDao cityDao = sqlSession.getMapper(CityDao.class);
List<City> cityByLike1 = cityDao.findCityByLike1("%阳%");
for (City city : cityByLike1) {
System.out.println(city);
}
}
第二种mapper文件sql语句的条件位置加上“%”:
dao
/**
* 根据名字模糊查询city对象并放入列表方法2
* @param name
* @return
*/
List<City> findCityByLike2(String name);
mapper
<!--根据名字模糊查询city对象并放入列表方法2-->
<select id="findCityByLike2" resultType="com.example.domian.City" parameterType="string">
select id,name,city_id from city where name like "%"#{name}"%"
</select>
测试
@Test
public void findCityByLike2(){
SqlSession sqlSession = BaseUtils.getSqlSession();
CityDao cityDao = sqlSession.getMapper(CityDao.class);
List<City> cityByLike1 = cityDao.findCityByLike2("大");
for (City city : cityByLike1) {
System.out.println(city);
}
}
第三种mapper文件sql语句的条件位置加上“%”【mysql数据库可用】:
mysql的CONCAT()函数的使用
dao
/**
* 根据名字模糊查询city对象并放入列表方法3
* @param name
* @return
*/
List<City> findCityByLike3(String name);
mapper
<!--根据名字模糊查询city对象并放入列表方法3
mysql的CONCAT()函数
MySQL的CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一。用法:CONCAT(str1,str2,...)
-->
<select id="findCityByLike3" resultType="com.example.domian.City" parameterType="string">
select id,name,city_id from city where name like CONCAT('%',#{name},'%')
</select>
注意:执行效果与上面的直接传参一样,但比直接传参法更好,因为与代码解耦了,但是此方法只针对mysql数据库所用,如果换成Oracle数据库则需要修改sql代码,因此此方法不通用,但可以使用。
测试
@Test
public void findCityByLike3(){
SqlSession sqlSession = BaseUtils.getSqlSession();
CityDao cityDao = sqlSession.getMapper(CityDao.class);
List<City> cityByLike1 = cityDao.findCityByLike3("山");
for (City city : cityByLike1) {
System.out.println(city);
}
}
四、关于别名设置
1)在mybatis主配置文件,使用typeAliases标签生命别名
2)在mapper文件中,resultType=“别名”
在mybatis主配置文件声明别名(mybatis主配置文件)
<typeAliases>
<!--第一种语法格式
type:java类型的全限定名称(自定义类型)
alias:自定义别名
-->
<typeAlias type="com.example.domian.Province" alias="pro"></typeAlias>
</typeAliases>
mapper文件中使用
<!--根据id查询Province对象-->
<select id="findById" resultType="pro">
select * from province where id = #{id}
</select>
五、Mybatis框架动态sql
- 动态SQL-if
- 动态SQL-where
- 动态SQL-foreach
- 动态SQL-片段
动态SQL,通过Mybatis提供的各种标签对条件作出判断以实现动态拼接SQL语句。这里的条件判断使用的表达式为OGNL表达式。常用的动态SQL标签有、、、等。
mybatis的动态SQL语句,与JSTL中的语句非常相似。
动态SQL
,主要用于解决查询条件不确定的情况:在程序运行期间,根据用户提交的查询条件进行查询。提交的查询条件不同,执行的SQL语句不同。若将每种可能的情况均逐一列出,对所有条件排序组合,将会出现大量的SQL语句。此时,可使用动态SQL来解决这样的问题。
在mapper的动态SQL中若出现大于号(>)、(<)、大于等于号(>=),小于等于号(<=)等符号,最好将其转换为实体符号。否则,XML可能会出现解析出错问题。
特别是对于小于号(<),在XML中绝不能出现的。否则解析mapper文件会出错。
实体符号表:
5.0.1 动态SQL-if
语法:
<if test="boolean判断结果">
<!--sql代码-->
</if>
<select id="findBySalary" resultType="com.example.domain.Emp">
select * from emp where 1=1
<if test="salary!=null">
and salary > #{salary}
</if>
<if test="job_id!=null">
and job_id = #{job_id}
</if>
</select>
5.0.2动态SQL-where
标签中存在一个比较麻烦的地方:需要在where后手动添加1=1子句。因为,若where后的所有条件均为false,而where后若又没有1=1子句,则SQL中就会只剩下一个空的where,SQL出错。所以,在where后,需要添加永为真子句1=1,以防止这种情况的发生。但当数据量很大时,会严重影响查询效率。
使用标签,里边时一个或多个if标签,当有一个if标签判断条件为true,where标签会转为WHERE关键字附加到sql语句的后面。如果if没有一个条件为true,忽略where和里面的if。
语法:
<where>
<if test="条件1">sql语句1</if>
<if test="条件2">sql语句2</if>
</where>
dao
//where
List<Emp> findWhere(Emp emp);
mapper
<select id="findWhere" resultType="com.example.domain.Emp">
select * from emp
<where>
<if test="salary!=null">
salary < #{salary}
</if>
<if test="job_id!=null">
and job_id=#{job_id}
</if>
</where>
</select>
5.0.3动态SQL-foreach
标签用于实现对于数组与集合的遍历。对其使用,需要注意:
- collection表示要遍历的集合类型,list,array等。
- open、close、separator为对遍历内容的SQL拼接。
语法:
<foreach collection="集合类型" open="开始的字符" close="结束的字符" separator="集合成员之间的分隔符" item="集合中成员">
#{item的值}
</foreach>
查询语句示例:
dao
//foreach
List<Emp> findForeach(List<Integer> list);
mapper
<select id="findForeach" resultType="com.example.domain.Emp" parameterType="list">
select * from emp
<where>
id in
<foreach collection="list" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</where>
</select>
添加语句示例:
dao
//foreach insert
int beachSaveForeach(@Param("emps") List<Emp> list);
mapper
<insert id="beachSaveForeach" parameterType="list">
insert into emp (name,job_id,join_date,salary) values
<foreach collection="emps" separator="," item="emp">
(#{emp.name},#{emp.job_id},#{emp.join_date},#{emp.salary})
</foreach>
</insert>
测试:
@Test
public void beachSaveForeach() {
SqlSession sqlSession = BaseUtils.getSqlSession();
EmpDao empDao = sqlSession.getMapper(EmpDao.class);
Emp emp1 = new Emp();
emp1.setName("李楠楠");
emp1.setJob_id(3);
emp1.setJoin_date(new Date());
emp1.setSalary(6300);
Emp emp2 = new Emp();
emp2.setName("王贝贝");
emp2.setJob_id(2);
emp2.setJoin_date(new Date());
emp2.setSalary(7300);
List<Emp> emps = new ArrayList<>();
emps.add(emp1);
emps.add(emp2);
int i = empDao.beachSaveForeach(emps);
System.out.println(i);
sqlSession.commit(); //注意:添加语句必须要提交
sqlSession.close();
}
5.0.4动态SQL-片段
标签用于定义SQL片段,以便其他SQL标签复用。而其他标签使用该SQL片段,需要使用子标签。该标签可以定义SQL语句中的任何部分,所以子标签可以放在SQL的任何位置。
使用方法:
1)在mapper文件中定义 sql代码片段
<sql id="唯一字符串"> 部分sql‘语句</sql>
2)在其他的位置,使用include标签引用某个代码片段
语法:
示例:
<sql id="selectEmp">
select * from emp
</sql>
引用代码片段使用标签
<select id="findIf" resultType="com.example.domain.Emp">
<include refid="selectEmp"/>
where 1=1
<if test="salary!=null">
and salary > #{salary}
</if>
<if test="job_id!=null">
and job_id = #{job_id}
</if>
</select>
六、MyBatis配置文件
mybatis配置文件两大类:1.mybatis主配置文件;2.mybatis的mapper文件
1.mybatis主配置文件,提供mybatis全局设置的。包含的内容有日志、数据源、mapper文件位置
2.mybatis的mapper文件:写sql语句的。一个表一个mapper文件
6.0.1settings部分
settings是mybatis的全局设置,影响整个mybatis的运行。这个设置一般使用默认值就可以了。
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="enhancementEnabled" value="false"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
6.0.2typeAliases部分
设置别名:设置别名在设置全限定类名时候可以使用别名来代替
<typeAliases>
<!--第一种语法格式
type:java类型的全限定名称(自定义类型)
alias:自定义别名
优点:别名可以自定义
缺点:每个类型必须单独定义
-->
<typeAlias type="com.example.domain.Emp" alias="emp"></typeAlias>
<typeAlias type="com.example.domain.Student" alias="stu"></typeAlias>
<!--第二种方式:
name:包名,mybatis会把这个包中所有类名作为别名(不用区分大小写)
优点:使用方方便,一次给多个类定义别名
缺点:别名不能自定义,必须是类名。
-->
<package name="com.example.domain"/>
<package name="com.example.domain2"/>
</typeAliases>
6.0.3配置环境
environments:环境标签,在他里面可以设置多个environment
environment:表示一个数据库的连接信息
属性:id自动逸的环境标识。唯一值
属性值:1)JDBC:使用Connection对象,由mybatis自己完成事务的处理。
2)MANAGED:管理,标识把事务的处理交给容器实现(由其他软件完成事务的提交,回滚)
dataSource:数据源,创建的Connection对象,连接数据库。
属性:type数据源的类型
属性值:1)POOLED,mybatis会在内存中创建PooledDataSource类,管理多个Connection连接对象,使用的连接池
2)UNPOOLED,不使用连接池,mybatis创建一个UnPooledDataSource这个类,每次执行sql语句先创建Connection对象,再执行sql语句,最后关闭Connection。
3)JNDI:java的命名和目录服务。
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/db2?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
6.0.4使用数据库属性配置文件
需要把数据库的配置信息放到一个单独文件中,独立管理。这个文件扩展名是properties。再这个文件中,使用自定义的key=value的格式标识数据。
使用步骤:
1.在resources目录中,创建xxxx.properties
2.在文件中,使用key=value的格式定义数据。
例如jdbc.url = jdbc:mysql://127.0.0.1:3306/db2?serverTimezone=UTC
3.在mybatis主配置文件,使用property标签引用外部的属性配置文件
<!--使用外部属性配置文件
resource:指定类路径下的某个属性配置文件-->
<properties resource="jdbc.properties"/>
4.在使用值的位置,使用${key}获取key对应的value(等号右侧的值)
例子:jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db2?serverTimezone=UTC
jdbc.username=root
jdbc.password=root
6.0.5mapper标签
mapper标签使用的格式有两个常用的方式:
<mappers>
<!--第一种方式,resources="mapper文件的路径"
优点:文件清晰。加载的文件是明确的。文件的位置比较灵活
缺点:文件比较多,代码量会比较大,管理难度大-->
<mapper resource="com/example/dao/EmpDao.xml"/>
<!--第二种方式,使用<package>
name:包名,mapper文件所在的包名
特点:把这个包中的所有mapper文件,一次加载
使用要求:
1.mapper文件和dao接口在同一目录
2.mapper文件和dao接口名称完全一样
-->
<package name="com.example.dao"/>
</mappers>
七、扩展之Mybatis通用分页插件
7.0.1Mybatis通用分页插件
使用步骤:
1.引入分页插件依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>最新版本</version>
</dependency>
2.配置拦截器插件
- 在 MyBatis 配置 xml 中配置拦截器插件
<!--
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
例子
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
- 如何在代码中使用
Mapper接口方式的调用,推荐这种使用方式。
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectIf(1);
PageHelper.startPage
静态方法调用
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);
assertEquals(2, list.get(0).getId());
assertEquals(10, list.size());
//分页时,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>
assertEquals(182, ((Page) list).getTotal());
例二:
//request: url?pageNum=1&pageSize=10
//支持 ServletRequest,Map,POJO 对象,需要配合 params 参数
PageHelper.startPage(request);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);
//后面的不会被分页,除非再次调用PageHelper.startPage
List<Country> list2 = countryMapper.selectIf(null);
//list1
assertEquals(2, list.get(0).getId());
assertEquals(10, list.size());
//分页时,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>,
//或者使用PageInfo类(下面的例子有介绍)
assertEquals(182, ((Page) list).getTotal());
//list2
assertEquals(1, list2.get(0).getId());
assertEquals(182, list2.size());
例三,使用PageInfo
的用法:
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());