后端:增加 MyBatis JSONTypeHandler

后端:增加指定商品的促销价格计算
前端:商品详情页,增加促销价格计算
This commit is contained in:
YunaiV
2019-04-16 23:31:22 +08:00
parent 4aac5bd2c6
commit 3909a95495
32 changed files with 686 additions and 104 deletions

View File

@@ -0,0 +1,70 @@
package cn.iocoder.common.framework.mybatis;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* TODO 芋艿
*
* 参考 https://www.cnblogs.com/waterystone/p/5547254.html
*
* 后续,补充下注释和测试类,以及文章。
*
* @param <T>
*/
public class JSONTypeHandler<T extends Object> extends BaseTypeHandler<T> {
private Class<T> clazz;
public JSONTypeHandler(Class<T> clazz) {
if (clazz == null) throw new IllegalArgumentException("Type argument cannot be null");
this.clazz = clazz;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, this.toJson(parameter));
}
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
return this.toObject(rs.getString(columnName), clazz);
}
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return this.toObject(rs.getString(columnIndex), clazz);
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return this.toObject(cs.getString(columnIndex), clazz);
}
private String toJson(T object) {
try {
return JSON.toJSONString(object);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private T toObject(String content, Class<?> clazz) {
if (content != null && !content.isEmpty()) {
try {
return (T) JSON.parseObject(content, clazz);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
}