五、类型处理器(转换器)

1.自带类型处理器

int——number等

2.自定义类型处理器

java——数据库(jdbc类型)

3.示例

实体类Student
boolean stuSex
true:男
false:女

表student
number stuSex
1:男
0:女

4.自定义类型转换器步骤

(1)创建转换器

通过阅读源码发现,此接口有一个实现类BaseTypeHandler ,因此要实现转换器有2种选择
a.实现接口TypeHandler接口
b.继承BaseTypeHandler

package nuc.hzb.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 BooleanAndNumberConverter extends BaseTypeHandler<Boolean> {

	// set方法是java(Boolean)到db(Number)
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)
			throws SQLException {
		// TODO Auto-generated method stub
		if (parameter) {
			// 如果是true,将该变量的值变成1
			ps.setInt(i, 1);
		} else {
			// 如果是false,将该变量的值变成0
			ps.setInt(i, 0);
		}
		
	}

	@Override
	public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
		// TODO Auto-generated method stub
		int sexNum = rs.getInt(columnName);
		/*
		 * if (sexNum == 1) { return true; } else { return false; }
		 */
		return sexNum == 1?true:false;
	}

	@Override
	public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		// TODO Auto-generated method stub
		int sexNum = rs.getInt(columnIndex);
		return sexNum == 1?true:false;
	}

	@Override
	public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		// TODO Auto-generated method stub
		int sexNum = cs.getInt(columnIndex);
		return sexNum == 1?true:false;
	}

}

(2)配置conf.xml

<typeHandlers>
	<typeHandler handler="nuc.hzb.converter.BooleanAndNumberConverter" javaType="Boolean" jdbcType="INTEGER"/>
</typeHandlers>

需要注意的问题: jdbcType为INTEGER(大写)

(3)studentMapper.xml

<select id="queryStudentByStuno" parameterType="int" resultMap="studentResult">
	select * from student where stuno = #{stuno}
</select>
	
<resultMap type="student" id="studentResult">
	<!-- 分为主键id,非主键result -->
	<id property="stuNo" column="stuno"/>
	<result property="stuName" column="stuname"/>
	<result property="stuAge" column="stuage"/>
	<result property="graName" column="graname"/>
	<result property="stuSex" column="stusex" javaType="boolean" jdbcType="INTEGER"/>
</resultMap>

<insert id="addStudent" parameterType="student">
	insert into student(stuno, stuname, stuage, graname, stusex) values(#{stuNo}, #{stuName}, #{stuAge}, #{graName}, #{stuSex, javaType=boolean, jdbcType=INTEGER})
</insert>

注意#{stuNo} 中存放的是属性值,需要严格区分大小写,可以通过id、result标签解决属性值和数据库存放的字段名不一致的问题

5.resultMap可以的实现功能

(1)类型转换

<resultMap type="student" id="studentResult">
    ...
</resultMap>

(2)属性和字段的映射关系

<id property="stuNo" column="stuno"/>
<result property="stuName" column="stuname"/>
<result property="stuAge" column="stuage"/>
<result property="graName" column="graname"/>
<result property="stuSex" column="stusex" javaType="boolean" jdbcType="INTEGER"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值