当前位置:   article > 正文

mybatis typehandler的使用_type-handlers-package

type-handlers-package

背景:最近在做商户中心清算明细的数据生成,文件中用了两个枚举数据,以及金额的显示都需要按特定的金额来展示小数位或者取整。。。。笔者想通过在数据层做相关数据的转换,mybatis中提供typehandler刚好符合改需求。

springboot添加注册handler的扫描地址,在application.properties中添加配置

mybatis.type-handlers-package=com.XX.merchant.dal.handler

统一枚举实现:

统一枚举的抽象接口

  1. package com.XX.merchant.util.enums;
  2. /**
  3. * <p>Title: EnumBase.java</p>
  4. * <p>Description: 枚举方法抽象</p>
  5. * <p>Copyright: Copyright (c) 2017 XXXXXX技术有限公司</p>
  6. * <p>Company: XXXXXX技术有限公司</p>
  7. * <p>Author:XXXXXX</p>
  8. * <p>CreateTime:2019年11月28日下午1:47:57
  9. */
  10. public interface EnumBase {
  11. public String getCode();
  12. public String getValue();
  13. }

枚举类继承BaseTypeHandler抽象类

  1. package com.XX.merchant.dal.handler;
  2. import java.sql.CallableStatement;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.apache.ibatis.type.BaseTypeHandler;
  9. import org.apache.ibatis.type.JdbcType;
  10. import org.apache.ibatis.type.MappedJdbcTypes;
  11. import org.apache.ibatis.type.MappedTypes;
  12. import com.XX.merchant.util.enums.EnumBase;
  13. import com.XX.merchant.util.enums.OrderType;
  14. import com.XX.merchant.util.enums.RecordType;
  15. /**
  16. * <p>Title: EnumBaseHandler.java</p>
  17. * <p>Description: enum typehandler</p>
  18. * <p>Copyright: Copyright (c) 2017 XXXXXX技术有限公司</p>
  19. * <p>Company: XXXXXX技术有限公司</p>
  20. * <p>Author:XXXXXX</p>
  21. * <p>CreateTime:2019年11月28日下午1:58:02
  22. */
  23. @MappedJdbcTypes(JdbcType.VARCHAR)
  24. @MappedTypes(value = {OrderType.class,RecordType.class})
  25. public class EnumBaseHandler<E extends EnumBase> extends BaseTypeHandler< EnumBase >{
  26. private final Map<String, E> codeLookup = new HashMap<String, E>();
  27. /**
  28. * 初始化枚举
  29. * @param type
  30. */
  31. public EnumBaseHandler ( Class< E > type ) {
  32. if (type == null) throw new IllegalArgumentException("Type argument cannot be null");
  33. for (E e : type.getEnumConstants()) {
  34. codeLookup.put(e.getCode(), e);
  35. }
  36. }
  37. /**
  38. * mybatis设置值时将javatype装成jdbctype
  39. * @param ps
  40. * @param i
  41. * @param parameter
  42. * @param jdbcType
  43. * @throws SQLException
  44. */
  45. @Override
  46. public void setNonNullParameter( PreparedStatement ps, int i, EnumBase parameter, JdbcType jdbcType ) throws SQLException {
  47. if (jdbcType == null) {
  48. ps.setString(i, parameter != null ? parameter.getCode() : null);
  49. } else {
  50. ps.setObject(i, parameter != null ? parameter.getCode() : null, jdbcType.TYPE_CODE); // see r3589
  51. }
  52. }
  53. /**
  54. * Mybatis获取数据结果集时将jdbctype转换成javatype(rs通过列名获取值)
  55. * @param rs
  56. * @param columnName
  57. * @return
  58. * @throws SQLException
  59. */
  60. @Override
  61. public E getNullableResult( ResultSet rs, String columnName ) throws SQLException {
  62. String s = rs.getString(columnName);
  63. return codeLookup.get(s);
  64. }
  65. /**
  66. * Mybatis获取数据结果集时将jdbctype转换成javatype(rs通过列序号)
  67. * @param rs
  68. * @param columnIndex
  69. * @return
  70. * @throws SQLException
  71. */
  72. @Override
  73. public E getNullableResult( ResultSet rs, int columnIndex ) throws SQLException {
  74. String s = rs.getString(columnIndex);
  75. return codeLookup.get(s);
  76. }
  77. /**
  78. * Mybatis调用存储过程时将jdbctype转换成javatype(cs通过列序号)
  79. * @param cs
  80. * @param columnIndex
  81. * @return
  82. * @throws SQLException
  83. */
  84. @Override
  85. public E getNullableResult( CallableStatement cs, int columnIndex ) throws SQLException {
  86. String s = cs.getString(columnIndex);
  87. return codeLookup.get(s);
  88. }
  89. }

枚举对象

  1. package com.XX.merchant.util.enums;
  2. import org.apache.commons.lang3.StringUtils;
  3. /**
  4. * <p>Title: OrderType.java</p>
  5. * <p>Description: clearing表ORDER_TYPE字段</p>
  6. * <p>Copyright: Copyright (c) 2017 XXXXXXX技术有限公司</p>
  7. * <p>Company: XXXXXXX技术有限公司</p>
  8. * <p>Author:XXXXXX</p>
  9. * <p>CreateTime:2019年11月28日上午11:32:00
  10. */
  11. public enum OrderType implements EnumBase {
  12. //收入
  13. Income("I","Income"),
  14. //支出
  15. Expenses("E","Expenses");
  16. private final String code;
  17. private final String value;
  18. private OrderType ( String code, String value) {
  19. this.code = code;
  20. this.value = value;
  21. }
  22. public static OrderType getUserStateByCode(String code){
  23. if(StringUtils.isBlank(code)) {
  24. return null;
  25. }
  26. for (OrderType stateEnum: values()){
  27. if(StringUtils.equals(stateEnum.getCode(), code.toUpperCase())){
  28. return stateEnum;
  29. }
  30. }
  31. return null;
  32. }
  33. public String getCode( ) {
  34. return code;
  35. }
  36. public String getValue( ) {
  37. return value;
  38. }
  39. @Override
  40. public String toString( ) {
  41. return this.value;
  42. }
  43. }

检索的sql

  1. <select id="getClearingSettleForExcel" resultMap="BaseResultMapEnum">
  2. select
  3. <include refid="Base_Column_List" />
  4. from clearing_statement
  5. where CLEARING_DATE = #{queryDate,jdbcType=DATE} and #{merchantNo,jdbcType=VARCHAR} order BY CLEARING_TIME desc
  6. </select>

resultMap

  1. <resultMap id="BaseResultMapEnum" type="com.XX.merchant.dal.model.ClearingStatementEnum">
  2. <!--
  3. WARNING - @mbg.generated
  4. This element is automatically generated by MyBatis Generator, do not modify.
  5. -->
  6. 。。。。。。。
  7. <result column="ORDER_TYPE" jdbcType="VARCHAR" property="orderType" typeHandler="com.XX.merchant.dal.handler.EnumBaseHandler" javaType="com.XX.merchant.util.enums.OrderType"/>
  8. <result column="RECORD_TYPE" jdbcType="VARCHAR" property="recordType" typeHandler="com.XX.merchant.dal.handler.EnumBaseHandler" javaType="com.XX.merchant.util.enums.RecordType"/>
  9. <result column="PAYMENT_CURRENCY" jdbcType="VARCHAR" property="paymentCurrency" />
  10. <result column="PAYMENT_AMOUNT" jdbcType="DECIMAL" property="paymentAmount" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
  11. <result column="CLEARING_CURRENCY" jdbcType="VARCHAR" property="clearingCurrency" />
  12. <result column="INCOME" jdbcType="DECIMAL" property="income" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
  13. <result column="EXPENSES" jdbcType="DECIMAL" property="expenses" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
  14. <result column="TRANSACTION_FEE" jdbcType="DECIMAL" property="transactionFee" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
  15. <result column="REVOLVING_DEPOSIT" jdbcType="DECIMAL" property="revolvingDeposit" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
  16. 。。。。。。
  17. </resultMap>

pojo

  1. package com.XX.merchant.dal.model;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. import com.XX.merchant.util.enums.EnumBase;
  5. /**
  6. *
  7. * This class was generated by MyBatis Generator.
  8. * This class corresponds to the database table clearing_statement
  9. *
  10. * @mbg.generated do_not_delete_during_merge
  11. */
  12. public class ClearingStatementEnum implements Serializable {
  13. .......
  14. /**
  15. * Database Column Remarks:
  16. * I:(Income-收入),E:(Expenses-支出)
  17. *
  18. * This field was generated by MyBatis Generator.
  19. * This field corresponds to the database column clearing_statement.ORDER_TYPE
  20. *
  21. * @mbg.generated
  22. */
  23. private EnumBase orderType;
  24. /**
  25. * Database Column Remarks:
  26. * P:(Payment-支付),R:(Refund-退款),RF:(Refund fee-退款费),RR:(Refund Rollback-退款回滚),PF(Processing Fee-Processing费),RD(Revolving Deposit Release-循环保证金释放)
  27. *
  28. * This field was generated by MyBatis Generator.
  29. * This field corresponds to the database column clearing_statement.RECORD_TYPE
  30. *
  31. * @mbg.generated
  32. */
  33. private EnumBase recordType;
  34. /**
  35. * Database Column Remarks:
  36. * 交易金额
  37. *
  38. * This field was generated by MyBatis Generator.
  39. * This field corresponds to the database column clearing_statement.PAYMENT_AMOUNT
  40. *
  41. * @mbg.generated
  42. */
  43. private String paymentAmount;
  44. /**
  45. * Database Column Remarks:
  46. * 收入
  47. *
  48. * This field was generated by MyBatis Generator.
  49. * This field corresponds to the database column clearing_statement.INCOME
  50. *
  51. * @mbg.generated
  52. */
  53. private String income;
  54. /**
  55. * Database Column Remarks:
  56. * 支出
  57. *
  58. * This field was generated by MyBatis Generator.
  59. * This field corresponds to the database column clearing_statement.EXPENSES
  60. *
  61. * @mbg.generated
  62. */
  63. private String expenses;
  64. /**
  65. * Database Column Remarks:
  66. * 交易费
  67. *
  68. * This field was generated by MyBatis Generator.
  69. * This field corresponds to the database column clearing_statement.TRANSACTION_FEE
  70. *
  71. * @mbg.generated
  72. */
  73. private String transactionFee;
  74. /**
  75. * Database Column Remarks:
  76. * 循环保证金
  77. *
  78. * This field was generated by MyBatis Generator.
  79. * This field corresponds to the database column clearing_statement.REVOLVING_DEPOSIT
  80. *
  81. * @mbg.generated
  82. */
  83. private String revolvingDeposit;
  84. /**
  85. * This method was generated by MyBatis Generator.
  86. * This method returns the value of the database column clearing_statement.ORDER_TYPE
  87. *
  88. * @return the value of clearing_statement.ORDER_TYPE
  89. *
  90. * @mbg.generated
  91. */
  92. public EnumBase getOrderType() {
  93. return orderType;
  94. }
  95. /**
  96. * This method was generated by MyBatis Generator.
  97. * This method sets the value of the database column clearing_statement.ORDER_TYPE
  98. *
  99. * @param orderType the value for clearing_statement.ORDER_TYPE
  100. *
  101. * @mbg.generated
  102. */
  103. public void setOrderType(EnumBase orderType) {
  104. this.orderType = orderType;
  105. }
  106. /**
  107. * This method was generated by MyBatis Generator.
  108. * This method returns the value of the database column clearing_statement.RECORD_TYPE
  109. *
  110. * @return the value of clearing_statement.RECORD_TYPE
  111. *
  112. * @mbg.generated
  113. */
  114. public EnumBase getRecordType() {
  115. return recordType;
  116. }
  117. /**
  118. * This method was generated by MyBatis Generator.
  119. * This method sets the value of the database column clearing_statement.RECORD_TYPE
  120. *
  121. * @param recordType the value for clearing_statement.RECORD_TYPE
  122. *
  123. * @mbg.generated
  124. */
  125. public void setRecordType(EnumBase recordType) {
  126. this.recordType = recordType;
  127. }
  128. /**
  129. * This method was generated by MyBatis Generator.
  130. * This method returns the value of the database column clearing_statement.PAYMENT_AMOUNT
  131. *
  132. * @return the value of clearing_statement.PAYMENT_AMOUNT
  133. *
  134. * @mbg.generated
  135. */
  136. public String getPaymentAmount() {
  137. return paymentAmount;
  138. }
  139. /**
  140. * This method was generated by MyBatis Generator.
  141. * This method sets the value of the database column clearing_statement.PAYMENT_AMOUNT
  142. *
  143. * @param paymentAmount the value for clearing_statement.PAYMENT_AMOUNT
  144. *
  145. * @mbg.generated
  146. */
  147. public void setPaymentAmount(String paymentAmount) {
  148. this.paymentAmount = paymentAmount;
  149. }
  150. /**
  151. * This method was generated by MyBatis Generator.
  152. * This method returns the value of the database column clearing_statement.INCOME
  153. *
  154. * @return the value of clearing_statement.INCOME
  155. *
  156. * @mbg.generated
  157. */
  158. public String getIncome() {
  159. return income;
  160. }
  161. /**
  162. * This method was generated by MyBatis Generator.
  163. * This method sets the value of the database column clearing_statement.INCOME
  164. *
  165. * @param income the value for clearing_statement.INCOME
  166. *
  167. * @mbg.generated
  168. */
  169. public void setIncome(String income) {
  170. this.income = income;
  171. }
  172. /**
  173. * This method was generated by MyBatis Generator.
  174. * This method returns the value of the database column clearing_statement.EXPENSES
  175. *
  176. * @return the value of clearing_statement.EXPENSES
  177. *
  178. * @mbg.generated
  179. */
  180. public String getExpenses() {
  181. return expenses;
  182. }
  183. /**
  184. * This method was generated by MyBatis Generator.
  185. * This method sets the value of the database column clearing_statement.EXPENSES
  186. *
  187. * @param expenses the value for clearing_statement.EXPENSES
  188. *
  189. * @mbg.generated
  190. */
  191. public void setExpenses(String expenses) {
  192. this.expenses = expenses;
  193. }
  194. /**
  195. * This method was generated by MyBatis Generator.
  196. * This method returns the value of the database column clearing_statement.TRANSACTION_FEE
  197. *
  198. * @return the value of clearing_statement.TRANSACTION_FEE
  199. *
  200. * @mbg.generated
  201. */
  202. public String getTransactionFee() {
  203. return transactionFee;
  204. }
  205. /**
  206. * This method was generated by MyBatis Generator.
  207. * This method sets the value of the database column clearing_statement.TRANSACTION_FEE
  208. *
  209. * @param transactionFee the value for clearing_statement.TRANSACTION_FEE
  210. *
  211. * @mbg.generated
  212. */
  213. public void setTransactionFee(String transactionFee) {
  214. this.transactionFee = transactionFee;
  215. }
  216. /**
  217. * This method was generated by MyBatis Generator.
  218. * This method returns the value of the database column clearing_statement.REVOLVING_DEPOSIT
  219. *
  220. * @return the value of clearing_statement.REVOLVING_DEPOSIT
  221. *
  222. * @mbg.generated
  223. */
  224. public String getRevolvingDeposit() {
  225. return revolvingDeposit;
  226. }
  227. /**
  228. * This method was generated by MyBatis Generator.
  229. * This method sets the value of the database column clearing_statement.REVOLVING_DEPOSIT
  230. *
  231. * @param revolvingDeposit the value for clearing_statement.REVOLVING_DEPOSIT
  232. *
  233. * @mbg.generated
  234. */
  235. public void setRevolvingDeposit(String revolvingDeposit) {
  236. this.revolvingDeposit = revolvingDeposit;
  237. }
  238. .........
  239. }

金额展示处理handType

  1. package com.XX.merchant.dal.handler;
  2. import java.math.BigDecimal;
  3. import java.sql.CallableStatement;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.ibatis.type.BaseTypeHandler;
  9. import org.apache.ibatis.type.JdbcType;
  10. import org.apache.ibatis.type.MappedJdbcTypes;
  11. import org.apache.ibatis.type.MappedTypes;
  12. import com.XX.merchant.util.StringUtil;
  13. /**
  14. * <p>Title: AmountShowHandler.java</p>
  15. * <p>Description: 币种对应下的金额展示规则</p>
  16. * <p>Copyright: Copyright (c) 2017 XXXXXX技术有限公司</p>
  17. * <p>Company: XXXXXX技术有限公司</p>
  18. * <p>Author:XXXXXXXX</p>
  19. * <p>CreateTime:2019年11月29日上午11:50:57
  20. */
  21. @MappedJdbcTypes(JdbcType.DECIMAL)
  22. @MappedTypes({BigDecimal.class})
  23. public class AmountShowHandler extends BaseTypeHandler< String > {
  24. @Override
  25. public void setNonNullParameter( PreparedStatement ps, int i, String parameter, JdbcType jdbcType ) throws SQLException {
  26. ps.setString(i, parameter);
  27. }
  28. @Override
  29. public String getNullableResult( ResultSet rs, String columnName ) throws SQLException {
  30. String paymentCurrency = null;
  31. if(StringUtils.equals(columnName, "PAYMENT_AMOUNT")) {
  32. paymentCurrency = rs.getString("PAYMENT_CURRENCY");
  33. }
  34. if(StringUtils.equalsAny(columnName, "INCOME","EXPENSES","TRANSACTION_FEE","REVOLVING_DEPOSIT")) {
  35. paymentCurrency = rs.getString("CLEARING_CURRENCY");
  36. }
  37. BigDecimal resultAmount = rs.getBigDecimal(columnName);
  38. if(StringUtils.isBlank(paymentCurrency) && null != resultAmount) {
  39. paymentCurrency = "other";
  40. }
  41. return StringUtil.formatString(resultAmount, paymentCurrency);
  42. }
  43. @Override
  44. public String getNullableResult( ResultSet rs, int columnIndex ) throws SQLException {
  45. return rs.getString(columnIndex);
  46. }
  47. @Override
  48. public String getNullableResult( CallableStatement cs, int columnIndex ) throws SQLException {
  49. return cs.getString(columnIndex);
  50. }
  51. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/173103
推荐阅读
相关标签
  

闽ICP备14008679号