mybatis的学习和使用--TypeHandler的使用--使用自定义的handler(6)
在mybatis的学习和使用--TypeHandler的使用--使用自定义的handler(5)中实现了自定义的java.util.Date和varchar的转换。
本文章再介绍下如何实现java.util.List和varchar的转换,其实也比较简单。
代码示例:https://github.com/wangjiuong/MybatisDemo/tree/master/MyBatisListTypeHandler
建表语句如下:
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `t_user_list_handler` -- ---------------------------- DROP TABLE IF EXISTS `t_user_list_handler`; CREATE TABLE `t_user_list_handler` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `hobies` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
bean对象设计如下:
package com.wang.MyBatis.model; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; public class UserBean implements Serializable { private Integer id; private String username; private String password; private List<String> hobies; public UserBean() { } public UserBean(String username, String password, ArrayList<String> hobies) { this.username = username; this.password = password; this.hobies = hobies; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public List<String> getHobies() { return hobies; } public void setHobies(List<String> hobies) { this.hobies = hobies; } public String toString() { return new Gson().toJson(this); } }
其中用到的gson实现了序列化。
MyListTypeHandler的实现如下:
package com.wang.MyBatis.typeHandler; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; @MappedJdbcTypes({ JdbcType.VARCHAR }) @MappedTypes({ List.class }) public class MyListTypeHandler extends BaseTypeHandler<List<String>> { @Override public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException { System.out.println("1"); ps.setString(i, parameter.toString()); } @Override public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException { // TODO Auto-generated method stub String columnConetnt = rs.getString(columnName); List<String> result = new ArrayList<String>(); if (null != columnName) { String subString = columnConetnt.substring(1, columnConetnt.length() - 1); if (null != subString) { String[] hobies = subString.split(","); for (String hoby : hobies) { result.add(hoby); } } } return result; } @Override public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { // TODO Auto-generated method stub System.out.println("3" + rs.getString(columnIndex)); return null; } @Override public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { // TODO Auto-generated method stub System.out.println("4" + cs.getString(columnIndex)); return null; } }
代码执行结果如下:
数据库中信息如下:
console中输出如下: