当我们在javabean中自定义了数据类型,但是在数据库中存储时往往需要转换成数据库对应的类型,并且在从数据库中取出来时也需要将数据库类型转换为javabean中的对应类型。
因为有大量类似数据的转换,手动转换类型进行存储和查询已经过于麻烦。MyBatis为我们提供了解决办法:TypeHandler类型处理器。
举例:数据库文件sql
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(30) NOT NULL,
age INT ,
interests VARCHAR(100)
)
INSERT INTO student VALUES(1,'tom',20,'游泳,踢球');
SELECT * FROM student;
大家主要关注 interests这个列,这里定义的是varchar属性的。
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String username;
private Integer age;
private String[] interests;
}
上文的三个注解我在上一篇已经讲过了是运用了lomok方法,利用注解来代替传统的get/set和构造方法。
但是重点不在这,重点在 String[] interests 上,这个使用java的数组形式存储的。在这个时候我们就要用到了TypeHandler自定义类型处理器来处理这个问题。
@MappedJdbcTypes(JdbcType.VARCHAR)//数据库中表的属性是varchar
@MappedTypes(String[].class)//javabean中定义属性是String[]
public class StringArrayTypeHandler extends BaseTypeHandler<String[]> {
@Override
/*
* PreparedStatement ps, 数据库预处理
* int i, 索引值
* String[] parameter,
* JdbcType jdbcType jdbc格式
*/
public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType)
throws SQLException {
StringBuilder stb = new StringBuilder();
for(String s : parameter) {
stb.append(s).append(",");//用,拼接加入数据库的字符串
}
stb.deleteCharAt(stb.length()-1);//删除数据库表中存储元素的最后一个,
ps.setString(i, stb.toString());//存入数据库
}
private String[] getStringArray(String columnValue) {
if(columnValue == null) {
return null;
}
return columnValue.split(",");
}
@Override
public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
return getStringArray(rs.getString(columnName));
}
@Override
public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return getStringArray(rs.getString(columnIndex));
}
@Override
public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return getStringArray(cs.getString(columnIndex));
}
}
注测类型转换器
在你的mybatis配置文件的xml中 加入以下的代码,完成注册。
<typeHandlers>
<typeHandler handler="org.apache.ibatis.type.StringTypeHandler"/>
</typeHandlers>
StudentMapper
public interface StudentMapper {
int insert(Student student);
int update(Student student);
Student getById(int id);
}
mapper
<?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.neu.mapper.StudentMapper">
<insert id="insert">
insert into student values(#{id},#{username},#{age},#{interests,typehandler=你自定义类型转换器的包})
</insert>
<select id="getById" resultType="com.neu.po.Student">
select * from student
where id = #{id}
</select>
</mapper>
在测试类中进行测试
public class StudentMapperTest {
private SqlSession session;
private StudentMapper studentMapper;
@Before
public void setup() throws IOException {
//ctrl+2 L
//读取配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//创建连接,相当于Connection
session = sessionFactory.openSession();
//返回DeptMapper接口的实现类
studentMapper = session.getMapper(StudentMapper.class);
}
@After
public void after() {
//关闭会话
session.close();
}
@Test
public void testInsert() {
int n = studentMapper.insert(new Student(null, "marry", 21, new String[] {"学java","游泳"}));
System.out.println(n);
session.commit();
}
@Test
public void testGetById() {
Student stu = studentMapper.getById(1);
System.out.println(stu);
}
}
本文例子就到这,有啥不对的大家及时补充,我也作为学习mybatis的一个笔记记录情况。