typeHandler를 써서 간단히 적용 완료.
DB 내용 searchTags 필드
value1, value2, value3
,를 구분자로 각각의 value들이 하나의 List 값이 된다.
DTO
List<String> searchTags;
public List<String> getSearchTags() {
return searchTags;
}
public void setSearchTags(List<String> searchTags) {
this.searchTags = searchTags;
}
mybatis resultmap
<result property="searchTags" column="searchTags" typeHandler="com.my.handler.SplitTypeHandler" />
StringSplitTypeHandler
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
public class SplitTypeHandler implements TypeHandler<List<String>> {
@Override
public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
if (parameter != null) {
ps.setString(i, parameter.toString());
}
}
@Override
public List<String> getResult(ResultSet rs, String columnName) throws SQLException {
String columnValueStr = rs.getString(columnName);
if (columnValueStr != null) {
return Arrays.asList(columnValueStr.split(","));
}
return null;
}
@Override
public List<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
String columnValueStr = rs.getString(columnIndex);
if (columnValueStr != null) {
return Arrays.asList(columnValueStr.split(","));
}
return null;
}
@Override
public List<String> getResult(CallableStatement cs, int columnIndex) throws SQLException {
String columnValueStr = cs.getString(columnIndex);
if (columnValueStr != null) {
return Arrays.asList(columnValueStr.split(","));
}
return null;
}
}