Processor type (type converter)
1.MyBatis some common type of processor comes
int - number
2. Custom Types processor MyBatis
java <=> database (jdbc type)
Example:
the entity class has member variables Student representative of M boolean stuSex true false means female
table studen in t: number stuSex 1: 0 Male: Female
Custom type converter (boolean <=> number) steps of:
- Creating converter: TypeHandler need to implement the interface
by reading the source code was found, there is a class that implements this interface BaseTypeHandler, so to achieve converters have two options:
. I implement the interface TypeHandler interfaces
. Ii inherited BaseTypeHandler
get & set :
package org.lanqiao.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;
//BaseTypeHandler<java类型>
public class BooleanAndIntConverter extends BaseTypeHandler<Boolean>{//这里要加上泛型
/*
* 下面的参数中:
* ps:PreparedStatement对象
* i:PreparedStatement对象操作参数的位置
* parameter:java值
* jdbcType:jdbc操作的数据库类型
*/
//java代码(boolean)-DB类型(number)
@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") ;
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 ;
}
}
You can see it get overloaded three ways:
//通过resultSet & 列名
public Boolean getNullableResult(ResultSet rs, String columnName)
//通过resultSet & 下标
public Boolean getNullableResult(ResultSet rs, int columnIndex)
//通过结果集拿
public Boolean getNullableResult(CallableStatement cs, int columnIndex)
- Configuration conf.xml
<typeHandlers>
<typeHandler handler="org.lanqiao.converter.BooleanAndIntConverter" javaType="Boolean" jdbcType="INTEGER" />
</typeHandlers>
resultMap
resultMap can be used to solve the types of problems
are mainly two:
- Java type and database type are not the same, such as the above boolean false and not interconversion
- For example, the entity class Student.java There is a field StudentNumber, the database is the number, this is not matched
both cases we can use resultMap, other cases (such as a.String and varchar2 direct identification) directly on resultType can
<select id="queryStudentByStuno" parameterType="int" resultMap="studentMapping" >
select * from student where stuno = #{stuno}
</select>
<!--注意这里的stuSex涉及到类型转换,要在大括号里写上javaType&jdbcType-->
<insert id="addStudentWithConverter" parameterType="student" >
insert into student(stuno,stuname,stuage,graname,stusex) values(#{stuNo},#{stuName},#{stuAge},#{graName} ,#{stuSex ,javaType=boolean ,jdbcType=INTEGER } )
</insert>
<resultMap type="student" id="studentMapping">
<!-- 分为主键id 和非主键 result-->
<id property="id" column="stuno" /><!--主键用id-->
<!--别的用property-->
<result property="studentName" column="name" />
<result property="studentAge" column="age" />
<result property="studentName" column="name" />
<!--这里有类型转换,在后面加上javaType和jdbcType就可以-->
<result property="studentSex" column="stusex" javaType="boolean" jdbcType="INTEGER"/>
</resultMap>
Requires attention to the problem:
insert into student(stuno,stuname,stuage,graname,stusex) values(#{stuNo},#{stuName},#{stuAge},#{graName} ,#{stuSex ,javaType=boolean ,jdbcType=INTEGER } )
Note # {stuNo} is stored in the property value, the need for strict case-sensitive.
focusky
the commit applied CRUD