mybatis接口代理方式实现dao层
- 接口代理方式-实现规则
- 传统方式实现 Dao 层,我们既要写接口,还要写实现类。而 MyBatis 框架可以帮助我们省略编写 Dao 层接口实现类的步骤。程序员只需要编写接口,由 MyBatis 框架根据接口的定义来创建该接口的动态代理对象。
- 实现规则
- 映射配置文件中的名称空间必须和 Dao 层接口的全类名相同。
- 映射配置文件中的增删改查标签的 id 属性必须和 Dao 层接口的方法名相同。
- 映射配置文件中的增删改查标签的 parameterType 属性必须和 Dao 层接口方法的参数相同。
- 映射配置文件中的增删改查标签的 resultType 属性必须和 Dao 层接口方法的返回值相同。
- 代码示例
- 接口举例
package com.itheima.mapper; import com.itheima.bean.Student; import java.util.List; /* 持久层接口 */ public interface StudentMapper { //根据id查询学生信息 Student selectById(Integer id); }
- sql映射配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.mapper.StudentMapper"> <!--根据id查询用户--> <!-- id属性对应接口中的方法名 resultType:对应接口中方法的返回值类型 parameterType:对应方法的参数 #{}只有一个参数时,括号里的变量名随便写,多个参数后面再说。 --> <select id="selectById" resultType="student"> SELECT * FROM student WHERE id=#{id} </select> </mapper>
- 接口举例
动态SQL
在我们做复杂的业务逻辑的时候,简单的slq语句已经不能支持,所以我们要使用动态sql
- where标签
- 作用
- 代替slq语句中的where关键字
- 配合if标签使用,在进行多条件查询的时候,能够去除多余的and关键字
- 作用
- if标签
- 作用:进行条件判断的标签,如果条件不成立,则标签中的sql代码段就不执行
- 示例
<!--多条件查询--> <select id="selectCondition" parameterType="student" resultType="student"> SELECT * FROM student <where> <if test="id != null"> id = #{id} </if> <if test="name != null"> AND `name` = #{name} </if> <if test="age != null"> AND age = #{age} </if> </where> </select>
- forEach标签
- 作用:当参数为集合或数组类型的时候,需要为sql语句加入数量不固定的参数
- 示例
<!--使用forEach遍历多个条件查询--> <!-- collection:要遍历的参数的类型,集合就写list,数组就行array item:为遍历出来的元素起一个名字,可随意写 open:遍历开始的时候需要加入sql语句的字符 close:遍历结束后需要加入sql语句的字符 separator:每一次遍历都要加入sql语句的字符 --> <select id="selectByIds" parameterType="list" resultType="student"> SELECT * FROM student <where> <foreach collection="list" item="id" open="id IN (" close=")" separator=","> #{id} </foreach> </where> </select>
- sql标签
- 作用:用于定义使用频率高,且复杂的slq代码段
- 示例
<!-- id:表示此sql代码端的唯一标识,在使用include标签引用的时候,会使用到id属性的值 --> <sql id="all"> SELECT * FROM student </sql>
- include标签
- 作用:引用使用sql标签定义的代码段
- 示例
<!--查询全部--> <select id="selectAll" resultType="student"> <include refid="all"/> </select>
mybatis中配置分页插件
在开发中,分页是必不可少的,但是目前我们使用的mybatis中是不带分页功能的,所以我们要想使用分页插件,需要在mybatis核心文件中进行配置。
分页助手:PageHelper,第三方分页助手;将复杂的分页操作进行封装,从而让分页功能变得非常简单。
分页插件的使用步骤
- 导入jar包
- 在mybatis核心配置文件中配置分页助手插件
<!-- 配置插件 PageInterceptor:指定PageInterceptor类的全限定名 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"/> </plugins>
- 在java中使用分页助手
//查询全部学生方法测试 public void selectAll() { //进行分页(要先指定分页规则,再进行sql操作) PageHelper.startPage(1, 2); //查询所有 List<Student> students = mapper.selectAll(); students.forEach(student -> System.out.println(student)); }
- 分页助手相关API
- PageHelper:分页助手功能类
- startPage():设置分页参数
- PageInfo:分页相关参数功能类。
- getTotal():获取总条数
- getPages():获取总页数(分页后)
- getPageNum():获取当前页
- getPageSize():获取每页显示条数
- getPrePage():获取上一页
- getNextPage():获取下一页
- isIsFirstPage():获取是否是第一页
- isIsLastPage():获取是否是最后一页
- PageHelper:分页助手功能类
- 代码示例
//查询全部功能测试 @Test public void selectAll() { //进行分页(要先指定分页规则,再进行sql操作) PageHelper.startPage(1, 2); //查询所有学生信息 List<Student> students = mapper.selectAll(); //获取学生list集合的分页信息,要传入获取到的学生信息 PageInfo<Student> pageInfo = new PageInfo<>(students); //获取总记录条数 long total = pageInfo.getTotal(); //获取总分页数 int size = pageInfo.getPages(); System.out.println("总记录数:" + total); System.out.println("总分页数:" + size); students.forEach(student -> System.out.println(student)); }