package com.nq.utils;
|
|
|
import org.apache.ibatis.type.JdbcType;
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
import org.apache.ibatis.type.MappedTypes;
|
import org.apache.ibatis.type.TypeHandler;
|
|
import java.sql.CallableStatement;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.util.Arrays;
|
import java.util.List;
|
|
@MappedJdbcTypes(JdbcType.VARCHAR)
|
@MappedTypes({List.class})
|
/**
|
* mybatis类型转换器 将数据库重新来的varchar类型用‘ , ’分隔的字符串转为List<String>
|
*/
|
public class ListTypeHandler implements TypeHandler<List<String>> {
|
|
@Override
|
public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
|
String hobbys = StringUtil.join(parameter, ",");
|
try {
|
ps.setString(i, hobbys);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public List<String> getResult(CallableStatement cs, int columnIndex) throws SQLException {
|
String hobbys = cs.getString(columnIndex);
|
return Arrays.asList(hobbys.split(","));
|
}
|
|
@Override
|
public List<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
|
return Arrays.asList(rs.getString(columnIndex).split(","));
|
}
|
|
@Override
|
public List<String> getResult(ResultSet rs, String columnName) throws SQLException {
|
return Arrays.asList(rs.getString(columnName).split(","));
|
}
|
|
}
|