|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2017 the original author or authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.springframework.data.mybatis.handlers; |
| 20 | + |
| 21 | +import org.apache.ibatis.type.BaseTypeHandler; |
| 22 | +import org.apache.ibatis.type.JdbcType; |
| 23 | +import org.apache.ibatis.type.MappedJdbcTypes; |
| 24 | +import org.apache.ibatis.type.MappedTypes; |
| 25 | + |
| 26 | +import java.sql.CallableStatement; |
| 27 | +import java.sql.PreparedStatement; |
| 28 | +import java.sql.ResultSet; |
| 29 | +import java.sql.SQLException; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Jarvis Song |
| 35 | + */ |
| 36 | +@MappedJdbcTypes(value = {JdbcType.VARCHAR, JdbcType.CLOB, JdbcType.LONGVARCHAR, JdbcType.NVARCHAR}, includeNullJdbcType = true) |
| 37 | +@MappedTypes({Set.class}) |
| 38 | +public class SetStringTypeHandler extends BaseTypeHandler<Set> { |
| 39 | + |
| 40 | + |
| 41 | + @Override |
| 42 | + public void setNonNullParameter(PreparedStatement ps, int i, Set parameter, JdbcType jdbcType) throws SQLException { |
| 43 | + StringBuilder builder = new StringBuilder(); |
| 44 | + if (!parameter.isEmpty()) { |
| 45 | + for (Object o : parameter) { |
| 46 | + builder.append(o).append(","); |
| 47 | + } |
| 48 | + builder.deleteCharAt(builder.length() - 1); |
| 49 | + } |
| 50 | + ps.setString(i, builder.toString()); |
| 51 | + } |
| 52 | + |
| 53 | + private Set<String> convertStringToSet(String str) { |
| 54 | + if (null == str) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + Set<String> set = new HashSet<String>(); |
| 58 | + String[] split = str.split(","); |
| 59 | + if (null != split && split.length > 0) { |
| 60 | + for (String s : split) { |
| 61 | + if (null == s || s.length() == 0) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + set.add(s); |
| 65 | + } |
| 66 | + } |
| 67 | + return set; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Set getNullableResult(ResultSet rs, String columnName) throws SQLException { |
| 72 | + return convertStringToSet(rs.getString(columnName)); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Set getNullableResult(ResultSet rs, int columnIndex) throws SQLException { |
| 77 | + return convertStringToSet(rs.getString(columnIndex)); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Set getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { |
| 82 | + return convertStringToSet(cs.getString(columnIndex)); |
| 83 | + } |
| 84 | +} |
0 commit comments