自定义类型处理器
1.首先创建数据库,并创建如下图的表:
2.创建一个普通的java项目,并在项目的Src目录下创建一个文件夹libs(用来存放mybatis和MySQL的驱动包),如图:**
然后选中这两个jar包,右键,选中Build path,点击add。。。。,然后就是下面这个样子了:
3.创建自定义类型处理器类
package converter;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
public class BooleanAndIntConverter extends BaseTypeHandler<Boolean> {
/*
* java类型(boolean)--》jdbc类型(int)
* @param ps: 当前的PrepareStatement对象
* @param i: 当前参数的位置
* @param parameter: 当前参数值
*/
@Override
public Boolean getNullableResult(ResultSet rs, String i) throws SQLException {
int sexNum=rs.getInt(i);
if(sexNum==1) {
return true;
}else {
return false;
}
}
@Override
public Boolean getNullableResult(ResultSet arg0, int arg1) throws SQLException {
int sexNum=arg0.getInt(arg1);
if(sexNum==1) {
return true;
}else {
return false;
}
}
@Override
public Boolean getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
int sexNum=arg0.getInt(arg1);
if(sexNum==1) {
return true;
}else {
return false;
}
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Boolean p, JdbcType arg3) throws SQLException {
/*
* 如果Java类型的parameter=true,则数据库jdbc转换为1,反之为0
*/
if(p) {
ps.setInt(i, 1);
}else {
ps.setInt(i,0);
}
}
}
4.配置自定义类型处理器,在Mybatis配置文件conf.xml中配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 这里要写一个引用db.properties的配置文件 -->
<properties resource="db.properties"/>
<!--配置自定义类型处理器-->
<typeHandlers>
<typeHandler handler="converter.BooleanAndIntConverter" javaType="java.lang.Boolean" jdbcType="INTEGER"/>
</typeHandlers>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/mapper.xml"/>
</mappers>
</configuration>
5.创建db.properties:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username=root
password=666666
6.创建实体类
package entity;
public class Student {
private int sn;
private String sname;
private boolean ssex;
public int getSn() {
return sn;
}
public void setSn(int sn) {
this.sn = sn;
}
public String getsname() {
return sname;
}
public void setsname(String sname) {
this.sname = sname;
}
public boolean getSsex() {
return ssex;
}
public void setSsex(boolean ssex) {
this.ssex = ssex;
}
}
7.MQL映射文件mapper.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="dao.StudentDao">
<select id="converter" parameterType="int" resultMap="studentResult">
select * from student1 where sn=#{sn}
</select>
<resultMap id="studentResult" type="entity.Student">
<id property="sn" column="sn"/>
<result property="sname" column="sname"/>
<result property="ssex" column="ssex" javaType="java.lang.Boolean" jdbcType="INTEGER" />
</resultMap>
</mapper>
8.定义接口
package dao;
import entity.Student;
public interface StudentDao {
Student converter(int sn);
}
9.编写一个测试方法和类
package test;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import dao.StudentDao;
import entity.Student;
public class Test1 {
public static void testmetheod()throws IOException{
String resource="conf.xml";
Reader reader=Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(reader);
SqlSession session =sessionFactory.openSession();
StudentDao st=session.getMapper(StudentDao.class);
Student student=st.converter(101);
System.out.println(student.getSn()+","+student.getsname()+", "+student.getSsex());
session.close();
}
public static void main(String [] agrs) throws IOException {
testmetheod();
}
}
10 运行测试类,查看结果
类型转换器转换成功