数据库保存数据,类型为List<String>
生活随笔
收集整理的這篇文章主要介紹了
数据库保存数据,类型为List<String>
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
下面展示一些 內(nèi)聯(lián)代碼片。
數(shù)據(jù)庫保存數(shù)據(jù),類型為List<String> ,字段只需設(shè)置為varchar[],然后導(dǎo)入下述類,并在實(shí)體類的對應(yīng)字段位置加上@Type(type = "XX.ListToStringArrayUserType"),這里寫保存工具類的包路徑 /*** @Author ZGM* @DateTime 2021/8/26* @description*/ public class ListToStringArrayUserType implements UserType, ParameterizedType, Serializable {protected static final int[] SQL_TYPES = {Types.ARRAY};/*** 默認(rèn) ArrayList*/private Class listType;@Overridepublic int[] sqlTypes() {return new int[]{Types.ARRAY};}@Overridepublic Class returnedClass() {return listType;}@Overridepublic boolean equals(Object o, Object o1) throws HibernateException {if (o == null) {return o1 == null;}return o.equals(o1);}@Overridepublic int hashCode(Object o) throws HibernateException {return o.hashCode();}@Overridepublic Object nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException, SQLException {if (resultSet.getArray(strings[0]) == null) {return null;}Array array = resultSet.getArray(strings[0]);List<String> result = newList();String[] javaArray = (String[]) array.getArray();result.addAll(Arrays.asList(javaArray));return result;}@Overridepublic void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException {Connection connection = preparedStatement.getConnection();if (o == null) {preparedStatement.setNull(i, SQL_TYPES[12]);} else {List castObject = (List) o;Object[] intArr = castObject.toArray();Array array = connection.createArrayOf("VARCHAR", intArr);preparedStatement.setArray(i, array);}}@Overridepublic Object deepCopy(Object o) throws HibernateException {if (o == null) return null;List copyList = newList();copyList.addAll((List) o);return copyList;}@Overridepublic boolean isMutable() {return true;}@Override@SuppressWarnings("unchecked")public Serializable disassemble(Object o) throws HibernateException {return ((Serializable) o);}@Overridepublic Object assemble(Serializable serializable, Object o) throws HibernateException {return serializable;}@Overridepublic Object replace(Object o, Object o1, Object o2) throws HibernateException {return o;}@Overridepublic void setParameterValues(Properties properties) {String listType = (String) properties.get("listType");if (!Strings.isNullOrEmpty(listType)) {try {this.listType = Class.forName(listType);} catch (ClassNotFoundException e) {throw new HibernateException(e);}} else {this.listType = java.util.ArrayList.class;}}private List newList() {try {return (List) listType.newInstance();} catch (Exception e) {throw new HibernateException(e);}} }總結(jié)
以上是生活随笔為你收集整理的数据库保存数据,类型为List<String>的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu上mysql的安装及使用【转
- 下一篇: mysql数据库int字段String条