/**
* 字典数据-类型转换器
* DictItem为自定义数据类型
*/
@Component
public class DictionaryTypehandler extends BaseTypeHandler<DictItem>{
/*此方法是将自定义类型数据的值存入数据库*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, DictItem parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, parameter.getValue());
}
/*此方法是将数据库的值取出并封装成自定义数据类型*/
@Override
public DictItem getNullableResult(ResultSet rs, String columnName) throws SQLException {
String dictCode = rs.getString(columnName);
if (ToolUtil.isEmpty(dictCode)){
return null;
}
// 查询字典
Dictionary dictionary = null;
try {
IDictionaryService iDictionaryService = SpringContextHolder.getBean(IDictionaryService.class);
dictionary = iDictionaryService.getDictByCode(dictCode);
} catch (Exception e){
e.printStackTrace();
}
if (ToolUtil.isEmpty(dictionary)){
return null;
}
// 封装DictItem
DictItem dictItem = new DictItem();
dictItem.setLabel(dictionary.getDictValue());
dictItem.setValue(dictionary.getDictCode());
return dictItem;
}
@Override
public DictItem getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return null;
}
@Override
public DictItem getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return null;
}
}