【MyBatis】类型处理器(类型转换器)

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

1.MyBatis自带一些常见的类型处理器

int  - number

2.自定义MyBatis类型处理器

java -数据库(jdbc类型)

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

表student: number stuSex
1:男
0:女

自定义类型转换器(boolean -number)步骤:

a.创建转换器:需要实现TypeHandler接口
通过阅读源码发现,此接口有一个实现类 BaseTypeHandler ,因此 要实现转换器有2种选择:
i.实现接口TypeHandler接口
ii.继承BaseTypeHandler
b.配置conf.xml

需要注意的问题: INTEGER
insert into student(stuno,stuname,stuage,graname,stusex) values(#{stuNo},#{stuName},#{stuAge},#{graName} ,#{stuSex ,javaType=boolean  ,jdbcType=INTEGER   } )
注意#{stuNo} 中存放的是 属性值,需要严格区分大小写。

3.类型转换器的方向

在这里插入图片描述

4.resultMap可以实现2个功能:

1.类型转换
2.属性-字段的映射关系

<select id="queryStudentByStuno" 	parameterType="int"  	resultMap="studentMapping" >
		select * from student where stuno = #{stuno}
	</select>
	<resultMap type="student" id="studentMapping">
			<!-- 分为主键id 和非主键 result-->
			<id property="id"  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>

5.示例代码

public class BooleanAndIntConverter extends BaseTypeHandler<Boolean> {

	//java(boolean)-DB(number)
	/*
	 * ps:PreparedStatement对象
	 * i:PreparedStatement对象操作参数的位置
	 * parameter:java值
	 * jdbcType:jdbc操作的数据库类型
	 */
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)
			throws SQLException {
			if(parameter) {
				//1
				ps.setInt(i, 1); 
			}else {
//				0
				ps.setInt(i, 0); 
			}
	}

	//db(number)->java(boolean)
	@Override
	public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
		int sexNum = rs.getInt(columnName) ;//rs.getInt("stuno") ;
//		if(sexNum == 1)
//		
//			return true;
//		else {
//			return false ;
//		}
		return sexNum == 1?true:false ;
	}

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

	@Override
	public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		int sexNum = cs.getInt(columnIndex) ;//rs.getInt(1)
		return sexNum == 1?true:false ;
	}
}
<!-- 查询 使用了类型转换器 
		1.如果类中属性和表中的字段类型能够合理识别(String-varchar2) 则可以使用resultType,否则(Boolean->int)使用resultMap
		2.如果类中属性名和表中的字段名能够合理识别(perNo->perno) 则可以使用resultType,否则(Boolean->int)使用resultMap
	-->
	<select id="queryPersonByIdWithConverter" resultMap="personResult"  parameterType="int">
		select * from person where id = #{id} 
	</select>
	<resultMap type="person" id="personResult">
		<!-- 分为主键id和非主键result -->
		<id property="id"  column="id" />
		<result property="name" column="name" />
		<result property="age" column="age" />
		<result property="sex" column="sex" javaType="Boolean" jdbcType="INTEGER"/>
	</resultMap>
发布了2 篇原创文章 · 获赞 2 · 访问量 30

猜你喜欢

转载自blog.csdn.net/weixin_42074910/article/details/104232947