In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章给大家分享的是有关Mybatis如何自定义TypeHandler解决特殊类型转换问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
我们知道,Java和MySQL中的数据类型是不同的,Java中除了基本数据类型,还有对象。
有时候使用MySQL存储数据,或者从MySQL中读取数据时,会有一些特殊需求 weary ,比如:
将Integer数组直接存入MySQL,保存为BLOB形式,读取出来时又是正常的Integer数组 将Integer数组转换为String,然后存入MySQL,使用varchar类型,读取出来时又是正常的Integer数组
这也太难了叭!
解决办法有两种:
Basic Method:Java在存入数据之前,或读取数据之后,做手动类型转换 Clever Method:定义TypeHandler,并在Mybatis对应位置指明
关于第一种方法这里不予赘述,不够Smart。这里主要讲述如何自定义Handler,来解决Java数据->MySQL数据的特殊类型转换问题grinning
这种Handler不仅方便了我们的数据库操作,还有利于代码的复用。
这里以Integer[]数组的存储为形如,1,2,3,的varchar字符串为例。
问题示例
我们定义一个role类,与数据库的role表对应:
public class Role { private Integer id; private String name; private Integer[] accessIds; private Date createTime; // ... ignore get and set methods}
注意到里面有一个accessIds字段,它的类型是Integer[]
数据库设计:
DROP TABLE IF EXISTS `role`;CREATE TABLE `role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `access_ids` varchar(255) DEFAULT NULL, `create_time` datetime NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;-- ------------------------------ Records of role-- ----------------------------INSERT INTO `role` VALUES ('1', '测试角色', ',1,2,', '2019-11-14 13:43:14');
自定义Handler类
通过继承BaseTypeHandler类,重写其方法,定义一个Integer[]与数据库varchar类型自动转换的Handler类:
/** * Java Int数组与MySQL String转换器 * 比如[1,2,3] --> ",1,2,3," */public class StringToIntArrayHandler extends BaseTypeHandler { private static final String splitCharset = ","; @Override public void setNonNullParameter(PreparedStatement ps, int i, Integer[] objects, JdbcType jdbcType) throws SQLException { String str = arrayToString(objects); ps.setString(i, str); } @Override public Integer[] getNullableResult(ResultSet rs, String columnName) throws SQLException { String str = rs.getString(columnName); return stringToArray(str); } @Override public Integer[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String str = rs.getString(columnIndex); return stringToArray(str); } @Override public Integer[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String str = cs.getString(columnIndex); return stringToArray(str); } // --- private methods --- /** * Integer数组转String * 注:使用提前设定好的分隔符分割数组的每一项 */ private static String arrayToString(Integer[] array) { StringBuilder res = new StringBuilder(); if (array != null && array.length > 0) { for (Object o : array) { res.append(splitCharset).append(o.toString()); } res.append(splitCharset); } return res.length() > 0 ? res.toString() : null; } /** * 从String转Integer数组 * 注:String是用分隔符分割的,使用String.split方法可以分解为数组 */ private static Integer[] stringToArray(String str) { List list = new ArrayList(); if (str != null) { String[] array = str.split(splitCharset); if (array.length > 0) { for (String o : array) { if (o != null && o.length() > 0) { list.add(Integer.parseInt(o)); } } } } return list.toArray(new Integer[0]); }}
这个类的具体作用是什么呢?
当Java中类型是Integer[]时,使用这个Handler类,将Integer[]转换为以,号分割的字符串,然后存入数据库 当从数据库读取以,分割值的字符串时,可以通过这个Handler,自动将字符串转换为Integer[]数组
下面我们演示一下具体的使用smile
在Mybatis中应用自定义的Handler
Mybatis存放SQL语句的XML文件:
SELECT id, name, access_ids, create_time FROM role WHERE id = #{id} SELECT LAST_INSERT_ID() INSERT INTO role (name, create_time, access_ids) VALUES (#{name}, #{createTime} , #{accessIds, jdbcType=VARCHAR, typeHandler=com.example.model.dao.handler.StringToIntArrayHandler})
感谢各位的阅读!关于"Mybatis如何自定义TypeHandler解决特殊类型转换问题"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.