赞
踩
背景:最近在做商户中心清算明细的数据生成,文件中用了两个枚举数据,以及金额的显示都需要按特定的金额来展示小数位或者取整。。。。笔者想通过在数据层做相关数据的转换,mybatis中提供typehandler刚好符合改需求。
springboot添加注册handler的扫描地址,在application.properties中添加配置
mybatis.type-handlers-package=com.XX.merchant.dal.handler
统一枚举实现:
统一枚举的抽象接口
- package com.XX.merchant.util.enums;
-
- /**
- * <p>Title: EnumBase.java</p>
- * <p>Description: 枚举方法抽象</p>
- * <p>Copyright: Copyright (c) 2017 XXXXXX技术有限公司</p>
- * <p>Company: XXXXXX技术有限公司</p>
- * <p>Author:XXXXXX</p>
- * <p>CreateTime:2019年11月28日下午1:47:57
- */
- public interface EnumBase {
-
- public String getCode();
-
- public String getValue();
- }
枚举类继承BaseTypeHandler抽象类
- package com.XX.merchant.dal.handler;
-
- import java.sql.CallableStatement;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.HashMap;
- import java.util.Map;
-
- import org.apache.ibatis.type.BaseTypeHandler;
- import org.apache.ibatis.type.JdbcType;
- import org.apache.ibatis.type.MappedJdbcTypes;
- import org.apache.ibatis.type.MappedTypes;
-
- import com.XX.merchant.util.enums.EnumBase;
- import com.XX.merchant.util.enums.OrderType;
- import com.XX.merchant.util.enums.RecordType;
- /**
- * <p>Title: EnumBaseHandler.java</p>
- * <p>Description: enum typehandler</p>
- * <p>Copyright: Copyright (c) 2017 XXXXXX技术有限公司</p>
- * <p>Company: XXXXXX技术有限公司</p>
- * <p>Author:XXXXXX</p>
- * <p>CreateTime:2019年11月28日下午1:58:02
- */
- @MappedJdbcTypes(JdbcType.VARCHAR)
- @MappedTypes(value = {OrderType.class,RecordType.class})
- public class EnumBaseHandler<E extends EnumBase> extends BaseTypeHandler< EnumBase >{
-
- private final Map<String, E> codeLookup = new HashMap<String, E>();
-
- /**
- * 初始化枚举
- * @param type
- */
- public EnumBaseHandler ( Class< E > type ) {
- if (type == null) throw new IllegalArgumentException("Type argument cannot be null");
- for (E e : type.getEnumConstants()) {
- codeLookup.put(e.getCode(), e);
- }
- }
-
- /**
- * mybatis设置值时将javatype装成jdbctype
- * @param ps
- * @param i
- * @param parameter
- * @param jdbcType
- * @throws SQLException
- */
- @Override
- public void setNonNullParameter( PreparedStatement ps, int i, EnumBase parameter, JdbcType jdbcType ) throws SQLException {
- if (jdbcType == null) {
- ps.setString(i, parameter != null ? parameter.getCode() : null);
- } else {
- ps.setObject(i, parameter != null ? parameter.getCode() : null, jdbcType.TYPE_CODE); // see r3589
- }
- }
-
- /**
- * Mybatis获取数据结果集时将jdbctype转换成javatype(rs通过列名获取值)
- * @param rs
- * @param columnName
- * @return
- * @throws SQLException
- */
- @Override
- public E getNullableResult( ResultSet rs, String columnName ) throws SQLException {
- String s = rs.getString(columnName);
- return codeLookup.get(s);
- }
-
- /**
- * Mybatis获取数据结果集时将jdbctype转换成javatype(rs通过列序号)
- * @param rs
- * @param columnIndex
- * @return
- * @throws SQLException
- */
- @Override
- public E getNullableResult( ResultSet rs, int columnIndex ) throws SQLException {
- String s = rs.getString(columnIndex);
- return codeLookup.get(s);
- }
-
- /**
- * Mybatis调用存储过程时将jdbctype转换成javatype(cs通过列序号)
- * @param cs
- * @param columnIndex
- * @return
- * @throws SQLException
- */
- @Override
- public E getNullableResult( CallableStatement cs, int columnIndex ) throws SQLException {
- String s = cs.getString(columnIndex);
- return codeLookup.get(s);
- }
-
- }
枚举对象
- package com.XX.merchant.util.enums;
-
- import org.apache.commons.lang3.StringUtils;
-
- /**
- * <p>Title: OrderType.java</p>
- * <p>Description: clearing表ORDER_TYPE字段</p>
- * <p>Copyright: Copyright (c) 2017 XXXXXXX技术有限公司</p>
- * <p>Company: XXXXXXX技术有限公司</p>
- * <p>Author:XXXXXX</p>
- * <p>CreateTime:2019年11月28日上午11:32:00
- */
- public enum OrderType implements EnumBase {
- //收入
- Income("I","Income"),
- //支出
- Expenses("E","Expenses");
-
- private final String code;
- private final String value;
-
- private OrderType ( String code, String value) {
- this.code = code;
- this.value = value;
- }
-
- public static OrderType getUserStateByCode(String code){
- if(StringUtils.isBlank(code)) {
- return null;
- }
- for (OrderType stateEnum: values()){
- if(StringUtils.equals(stateEnum.getCode(), code.toUpperCase())){
- return stateEnum;
- }
-
- }
- return null;
- }
-
- public String getCode( ) {
- return code;
- }
-
- public String getValue( ) {
- return value;
- }
-
- @Override
- public String toString( ) {
- return this.value;
- }
- }
检索的sql
- <select id="getClearingSettleForExcel" resultMap="BaseResultMapEnum">
- select
- <include refid="Base_Column_List" />
- from clearing_statement
- where CLEARING_DATE = #{queryDate,jdbcType=DATE} and #{merchantNo,jdbcType=VARCHAR} order BY CLEARING_TIME desc
- </select>
resultMap
- <resultMap id="BaseResultMapEnum" type="com.XX.merchant.dal.model.ClearingStatementEnum">
- <!--
- WARNING - @mbg.generated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- 。。。。。。。
- <result column="ORDER_TYPE" jdbcType="VARCHAR" property="orderType" typeHandler="com.XX.merchant.dal.handler.EnumBaseHandler" javaType="com.XX.merchant.util.enums.OrderType"/>
- <result column="RECORD_TYPE" jdbcType="VARCHAR" property="recordType" typeHandler="com.XX.merchant.dal.handler.EnumBaseHandler" javaType="com.XX.merchant.util.enums.RecordType"/>
- <result column="PAYMENT_CURRENCY" jdbcType="VARCHAR" property="paymentCurrency" />
- <result column="PAYMENT_AMOUNT" jdbcType="DECIMAL" property="paymentAmount" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
- <result column="CLEARING_CURRENCY" jdbcType="VARCHAR" property="clearingCurrency" />
- <result column="INCOME" jdbcType="DECIMAL" property="income" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
- <result column="EXPENSES" jdbcType="DECIMAL" property="expenses" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
- <result column="TRANSACTION_FEE" jdbcType="DECIMAL" property="transactionFee" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
- <result column="REVOLVING_DEPOSIT" jdbcType="DECIMAL" property="revolvingDeposit" typeHandler="com.XX.merchant.dal.handler.AmountShowHandler" javaType="java.lang.String"/>
- 。。。。。。
- </resultMap>
pojo
- package com.XX.merchant.dal.model;
-
- import java.io.Serializable;
- import java.util.Date;
-
- import com.XX.merchant.util.enums.EnumBase;
-
- /**
- *
- * This class was generated by MyBatis Generator.
- * This class corresponds to the database table clearing_statement
- *
- * @mbg.generated do_not_delete_during_merge
- */
- public class ClearingStatementEnum implements Serializable {
- .......
-
- /**
- * Database Column Remarks:
- * I:(Income-收入),E:(Expenses-支出)
- *
- * This field was generated by MyBatis Generator.
- * This field corresponds to the database column clearing_statement.ORDER_TYPE
- *
- * @mbg.generated
- */
- private EnumBase orderType;
-
- /**
- * Database Column Remarks:
- * P:(Payment-支付),R:(Refund-退款),RF:(Refund fee-退款费),RR:(Refund Rollback-退款回滚),PF(Processing Fee-Processing费),RD(Revolving Deposit Release-循环保证金释放)
- *
- * This field was generated by MyBatis Generator.
- * This field corresponds to the database column clearing_statement.RECORD_TYPE
- *
- * @mbg.generated
- */
- private EnumBase recordType;
-
-
-
- /**
- * Database Column Remarks:
- * 交易金额
- *
- * This field was generated by MyBatis Generator.
- * This field corresponds to the database column clearing_statement.PAYMENT_AMOUNT
- *
- * @mbg.generated
- */
- private String paymentAmount;
-
-
-
- /**
- * Database Column Remarks:
- * 收入
- *
- * This field was generated by MyBatis Generator.
- * This field corresponds to the database column clearing_statement.INCOME
- *
- * @mbg.generated
- */
- private String income;
-
- /**
- * Database Column Remarks:
- * 支出
- *
- * This field was generated by MyBatis Generator.
- * This field corresponds to the database column clearing_statement.EXPENSES
- *
- * @mbg.generated
- */
- private String expenses;
-
- /**
- * Database Column Remarks:
- * 交易费
- *
- * This field was generated by MyBatis Generator.
- * This field corresponds to the database column clearing_statement.TRANSACTION_FEE
- *
- * @mbg.generated
- */
- private String transactionFee;
-
- /**
- * Database Column Remarks:
- * 循环保证金
- *
- * This field was generated by MyBatis Generator.
- * This field corresponds to the database column clearing_statement.REVOLVING_DEPOSIT
- *
- * @mbg.generated
- */
- private String revolvingDeposit;
-
-
-
- /**
- * This method was generated by MyBatis Generator.
- * This method returns the value of the database column clearing_statement.ORDER_TYPE
- *
- * @return the value of clearing_statement.ORDER_TYPE
- *
- * @mbg.generated
- */
- public EnumBase getOrderType() {
- return orderType;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method sets the value of the database column clearing_statement.ORDER_TYPE
- *
- * @param orderType the value for clearing_statement.ORDER_TYPE
- *
- * @mbg.generated
- */
- public void setOrderType(EnumBase orderType) {
- this.orderType = orderType;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method returns the value of the database column clearing_statement.RECORD_TYPE
- *
- * @return the value of clearing_statement.RECORD_TYPE
- *
- * @mbg.generated
- */
- public EnumBase getRecordType() {
- return recordType;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method sets the value of the database column clearing_statement.RECORD_TYPE
- *
- * @param recordType the value for clearing_statement.RECORD_TYPE
- *
- * @mbg.generated
- */
- public void setRecordType(EnumBase recordType) {
- this.recordType = recordType;
- }
-
-
- /**
- * This method was generated by MyBatis Generator.
- * This method returns the value of the database column clearing_statement.PAYMENT_AMOUNT
- *
- * @return the value of clearing_statement.PAYMENT_AMOUNT
- *
- * @mbg.generated
- */
- public String getPaymentAmount() {
- return paymentAmount;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method sets the value of the database column clearing_statement.PAYMENT_AMOUNT
- *
- * @param paymentAmount the value for clearing_statement.PAYMENT_AMOUNT
- *
- * @mbg.generated
- */
- public void setPaymentAmount(String paymentAmount) {
- this.paymentAmount = paymentAmount;
- }
-
-
-
- /**
- * This method was generated by MyBatis Generator.
- * This method returns the value of the database column clearing_statement.INCOME
- *
- * @return the value of clearing_statement.INCOME
- *
- * @mbg.generated
- */
- public String getIncome() {
- return income;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method sets the value of the database column clearing_statement.INCOME
- *
- * @param income the value for clearing_statement.INCOME
- *
- * @mbg.generated
- */
- public void setIncome(String income) {
- this.income = income;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method returns the value of the database column clearing_statement.EXPENSES
- *
- * @return the value of clearing_statement.EXPENSES
- *
- * @mbg.generated
- */
- public String getExpenses() {
- return expenses;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method sets the value of the database column clearing_statement.EXPENSES
- *
- * @param expenses the value for clearing_statement.EXPENSES
- *
- * @mbg.generated
- */
- public void setExpenses(String expenses) {
- this.expenses = expenses;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method returns the value of the database column clearing_statement.TRANSACTION_FEE
- *
- * @return the value of clearing_statement.TRANSACTION_FEE
- *
- * @mbg.generated
- */
- public String getTransactionFee() {
- return transactionFee;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method sets the value of the database column clearing_statement.TRANSACTION_FEE
- *
- * @param transactionFee the value for clearing_statement.TRANSACTION_FEE
- *
- * @mbg.generated
- */
- public void setTransactionFee(String transactionFee) {
- this.transactionFee = transactionFee;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method returns the value of the database column clearing_statement.REVOLVING_DEPOSIT
- *
- * @return the value of clearing_statement.REVOLVING_DEPOSIT
- *
- * @mbg.generated
- */
- public String getRevolvingDeposit() {
- return revolvingDeposit;
- }
-
- /**
- * This method was generated by MyBatis Generator.
- * This method sets the value of the database column clearing_statement.REVOLVING_DEPOSIT
- *
- * @param revolvingDeposit the value for clearing_statement.REVOLVING_DEPOSIT
- *
- * @mbg.generated
- */
- public void setRevolvingDeposit(String revolvingDeposit) {
- this.revolvingDeposit = revolvingDeposit;
- }
-
- .........
- }
金额展示处理handType
- package com.XX.merchant.dal.handler;
-
- import java.math.BigDecimal;
- import java.sql.CallableStatement;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import org.apache.commons.lang3.StringUtils;
- import org.apache.ibatis.type.BaseTypeHandler;
- import org.apache.ibatis.type.JdbcType;
- import org.apache.ibatis.type.MappedJdbcTypes;
- import org.apache.ibatis.type.MappedTypes;
-
- import com.XX.merchant.util.StringUtil;
-
- /**
- * <p>Title: AmountShowHandler.java</p>
- * <p>Description: 币种对应下的金额展示规则</p>
- * <p>Copyright: Copyright (c) 2017 XXXXXX技术有限公司</p>
- * <p>Company: XXXXXX技术有限公司</p>
- * <p>Author:XXXXXXXX</p>
- * <p>CreateTime:2019年11月29日上午11:50:57
- */
- @MappedJdbcTypes(JdbcType.DECIMAL)
- @MappedTypes({BigDecimal.class})
- public class AmountShowHandler extends BaseTypeHandler< String > {
- @Override
- public void setNonNullParameter( PreparedStatement ps, int i, String parameter, JdbcType jdbcType ) throws SQLException {
- ps.setString(i, parameter);
- }
-
- @Override
- public String getNullableResult( ResultSet rs, String columnName ) throws SQLException {
- String paymentCurrency = null;
- if(StringUtils.equals(columnName, "PAYMENT_AMOUNT")) {
- paymentCurrency = rs.getString("PAYMENT_CURRENCY");
- }
- if(StringUtils.equalsAny(columnName, "INCOME","EXPENSES","TRANSACTION_FEE","REVOLVING_DEPOSIT")) {
- paymentCurrency = rs.getString("CLEARING_CURRENCY");
- }
- BigDecimal resultAmount = rs.getBigDecimal(columnName);
- if(StringUtils.isBlank(paymentCurrency) && null != resultAmount) {
- paymentCurrency = "other";
- }
- return StringUtil.formatString(resultAmount, paymentCurrency);
- }
-
-
- @Override
- public String getNullableResult( ResultSet rs, int columnIndex ) throws SQLException {
- return rs.getString(columnIndex);
- }
-
- @Override
- public String getNullableResult( CallableStatement cs, int columnIndex ) throws SQLException {
- return cs.getString(columnIndex);
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。