Mybatis之类型转换器的配置

类型转换器的配置流程

所需要用到的jar包   mybatis-3.5.1.jar 和 mysql-connector-java-5.1.0-bin.jar

1.配置mybatis-config.xml

创建mybatis-config.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>
        <!-- 资源配置 -->
	<properties resource="database.properties"></properties>

	<!-- 别名配置 -->
	<typeAliases>
		<!--批量设置 默认将包中的所有实体类设置别名,类名小写即别名 -->
		<package name="com.zy.pojo" />
	</typeAliases>

	<!-- 类型转换器配置
		handler:类型转换位置
		javaType:类中的属性类型
		jdbcType:数据库中的字段类型
	 -->
	<typeHandlers>
		<typeHandler handler="com.zy.converter.BooleanAndIntConverter"
			javaType="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>

        <!-- mapper配置 -->
	<mappers>
		<mapper resource="com/zy/mapper/StudentMapper.xml" />
	</mappers>
</configuration>

2.创建类--表映射

创建Student.java类

package com.zy.pojo;

public class Student {
	private Integer stuNo;
	private String stuName;
	private String stuClass;
	private int stuAge;
	private boolean stuSex;
	
	public Student(Integer stuNo, String stuName, String stuClass, int stuAge, boolean stuSex) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuClass = stuClass;
		this.stuAge = stuAge;
		this.stuSex = stuSex;
	}
	public Student() {
		super();
	}
	
	public Integer getStuNo() {
		return stuNo;
	}
	public void setStuNo(Integer stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getStuClass() {
		return stuClass;
	}
	public void setStuClass(String stuClass) {
		this.stuClass = stuClass;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	public boolean isStuSex() {
		return stuSex;
	}
	public void setStuSex(boolean stuSex) {
		this.stuSex = stuSex;
	}
	
	@Override
	public String toString() {
		return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", stuClass=" + stuClass + ", stuAge=" + stuAge
				+ ", stuSex=" + stuSex + "]";
	}
	
	
}

3.创建mapper接口

创建StudentMapper.java接口

package com.zy.mapper;

import java.util.List;

import com.zy.pojo.Student;

public interface StudentMapper {
	List<Student> queryAllStudent();
}

4.创建mapper映射文件

创建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.zy.mapper.StudentMapper">
        <!--查询学生全部信息-->
	<select id="queryAllStudent" resultType="student">
		select * from Student
	</select>
</mapper>

5.创建类型转换器

创建类型转换器BooleanAndIntConverter.java

package com.zy.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>{
	/**
	 * 类型转换器
	 * ps:PreparedStatement对象
	 * i:PreparedStatement对象的下标
	 * parameter:java的值  
	 * jdbcType:数据库中的值
	 */
//	java(boolean)-->DB(int)
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)
			throws SQLException {
		if(parameter){
			ps.setInt(i, 1);
		}else{
			ps.setInt(i, 0);
		}
	}
	
	/**
	 * 从数据库拿值有三种方式
	 * 1.根据列名拿值
	 * 2.根据下标拿值 
	 * 3.根据存储过程拿值 
	 */
//	DB(int)-->Java(boolean)
	@Override
	public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
		int number = rs.getInt(columnName); //相当于 rs.getInt("stuSex") 根据列名拿值
		return number == 1?true:false;
	}

	@Override
	public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		int number = rs.getInt(columnIndex); //相当于 rs.getInt(3)  根据下标拿值 
		return number == 1?true:false;
		
	}

	@Override
	public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		int number = cs.getInt(columnIndex); //根据存储过程拿值 
		return number == 1?true:false;
	}
	
}

6.测试类

创建测试类StudentTest.java

package com.zy.test;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 com.zy.mapper.StudentMapper;
import com.zy.pojo.Student;
import com.zy.pojo.StudentAddress;

public class StudentTest {
	public static void main(String[] args) throws IOException {
		queryAllStudent() ;

	}
	
//	全部查询(使用了类型转换器)
	private static void queryAllStudent() throws IOException {
		Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
		
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession sqlSession = factory.openSession();
		
		List<Student> allStudent = sqlSession.getMapper(StudentMapper.class).queryAllStudent();
		
		for (Student student : allStudent) {
			System.out.println(student);
		}
		
		sqlSession.close();
	}
}

7.运行结果

运行结过

8.项目结构

项目结构

9.数据库中的字段

分析一下:

  • Mybatsi的基本配置流程可以参考Mybatis动态代理方式配置(接口方式)

  • 数据库的字段类型和类中的属性类型不一致时,使用类型转换器,可以将数据库中字段和类中属性相互转换

  • 上述例子中数据库中的stuSex是INTEGER类型,而类中属性是Boolean型,在类型转换器中继承BaseTypeHandler<Boolean>类,在重写方法中转换类型

  • 需要在mybatis-config.xml中配置类型转换器

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值