MyBatis核心配置文件
typeHandlers标签
由于Java的数据类型和数据库中的数据类型不太一样,所以有些特定情况下需要类型转换器,用于把Java的数据类型转换为数据库兼容的数据类型,或者把数据库兼容的数据类型转换为Java的数据类型。
准备测试数据
-- 创建数据库表
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(20),
uage INT,
birthday BIGINT
);
定义Student类
/*
注意:
student表中的 birthday字段 是BIGINT类型
Student类中的 birthday 是Date类型
*/
public class Student {
private int id;
private String uname;
private int age;
private Date birthday;
public void setBirthday(long birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", uname='" + uname + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
类型转换器的开发步骤
1.定义类型转换器类
2.注册类型转换器到mybatis-config.xml配置文件中
3.在SQL映射文件中配置<resultMap>标签和<paramterMap>,用来表示Java类属性和结果集字段的对应关系
定义类型转换类DateTypeHandler继承BaseTypeHandler
//<Date>表示Java中的数据类型
public class DateTypeHandler extends BaseTypeHandler<Date> {
//把Java的数据类型转换为数据库需要的类型
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
preparedStatement.setLong(i,date.getTime());
}
//把数据库中结果集的数据类型转换为Java的数据类型
public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
return new Date(resultSet.getLong(s));
}
//把数据库中结果集的数据类型转换为Java的数据类型
public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
return new Date(resultSet.getLong(i));
}
//把数据库中结果集的数据类型转换为Java的数据类型
public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return new Date(callableStatement.getLong(i));
}
}
将类型转换器配置到mybatis-config.xml配置文件中
<!--配置类型转换器-->
<typeHandlers>
<typeHandler handler="com.itheima.mybatis.DateTypeHandler"/>
</typeHandlers>
编写StudentMapper接口
public interface StudentMapper {
/*添加学生*/
public int addStudent(Student student);
/*查询学生*/
public List<Student> selectStudent();
}
编写StudentMapper.xml映射文件
<?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">
<insert id="addStudent" parameterType="com.itheima.domain.Student">
insert into student(uname,uage,birthday) values(#{uname},#{uage},#{birthday})
</insert>
<select id="selectStudent" resultType="com.itheima.domain.Student">
select * from student
</select>
</mapper>
测试添加操作:
@Test
public void test(){
InputStream in = null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
SqlSession ss = ssf.openSession(true);
StudentMapper mapper = ss.getMapper(StudentMapper.class);
//演示添加功能
Student stu=new Student();
stu.setUname("淘淘");
stu.setAge(1);
stu.setBirthday(new Date().getTime());
int i = mapper.addStudent(stu);
System.out.println("添加成功");
System.out.println("-------------------");
//演示查询功能
List<Student> stus = mapper.selectAllStudent();
for (Student student : stus) {
System.out.println(student);
}
} catch (IOException e) {
e.printStackTrace();
}
}
plugins标签
MyBatis可以使用第三方的插件来对功能进行扩展,分页助手PageHelper是将分页的复杂操作进行封装,使用简单的方式即可获得分页的相关数据
开发步骤:
导入通用PageHelper坐标
<!-- 分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
在mybatis核心配置文件中配置PageHelper插件
<!-- 注意:分页助手的插件 配置在通用馆mapper之前 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定方言 -->
<property name="dialect" value="mysql"/>
</plugin>
测试分页代码实现
@Test
public void test(){
//第2页,每页5条记录
PageHelper.startPage(2,5);
//查询多个用户
List<User> list = ss.selectList("com.itheima.mapper.UserMapper.selectAllUser");
for (User o : list) {
System.out.println(o);
}
}
- 获得分页相关的其他参数
//第2页,每页5条记录
PageHelper.startPage(2,5);
//查询多个用户
List<User> list = ss.selectList("com.itheima.mapper.UserMapper.selectAllUser");
for (User o : list) {
System.out.println(o);
}
//获得分页相关的参数
PageInfo<User> pageInfo = new PageInfo<User>(list);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("当前页:"+pageInfo.getPageNum());
System.out.println("每页显示长度:"+pageInfo.getPageSize());
System.out.println("是否第一页:"+pageInfo.isIsFirstPage());
System.out.println("是否最后一页:"+pageInfo.isIsLastPage());