1. /**
    2. * 字典数据-类型转换器
    3. * DictItem为自定义数据类型
    4. */
    5. @Component
    6. public class DictionaryTypehandler extends BaseTypeHandler<DictItem>{
    7. /*此方法是将自定义类型数据的值存入数据库*/
    8. @Override
    9. public void setNonNullParameter(PreparedStatement ps, int i, DictItem parameter, JdbcType jdbcType) throws SQLException {
    10. ps.setString(i, parameter.getValue());
    11. }
    12. /*此方法是将数据库的值取出并封装成自定义数据类型*/
    13. @Override
    14. public DictItem getNullableResult(ResultSet rs, String columnName) throws SQLException {
    15. String dictCode = rs.getString(columnName);
    16. if (ToolUtil.isEmpty(dictCode)){
    17. return null;
    18. }
    19. // 查询字典
    20. Dictionary dictionary = null;
    21. try {
    22. IDictionaryService iDictionaryService = SpringContextHolder.getBean(IDictionaryService.class);
    23. dictionary = iDictionaryService.getDictByCode(dictCode);
    24. } catch (Exception e){
    25. e.printStackTrace();
    26. }
    27. if (ToolUtil.isEmpty(dictionary)){
    28. return null;
    29. }
    30. // 封装DictItem
    31. DictItem dictItem = new DictItem();
    32. dictItem.setLabel(dictionary.getDictValue());
    33. dictItem.setValue(dictionary.getDictCode());
    34. return dictItem;
    35. }
    36. @Override
    37. public DictItem getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    38. return null;
    39. }
    40. @Override
    41. public DictItem getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    42. return null;
    43. }
    44. }