Skip to content

Commit ab409bd

Browse files
committed
feat: 自定义 mybatis handler 包调整
1 parent 1652f81 commit ab409bd

3 files changed

Lines changed: 104 additions & 2 deletions

File tree

framework/src/main/java/top/cadecode/uniboot/framework/convert/mybatis/DefaultEnumTypeHandler.java renamed to common/src/main/java/top/cadecode/uniboot/common/mybatis/DefaultEnumTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package top.cadecode.uniboot.framework.convert.mybatis;
1+
package top.cadecode.uniboot.common.mybatis;
22

33
import org.apache.ibatis.type.BaseTypeHandler;
44
import org.apache.ibatis.type.JdbcType;
5-
import top.cadecode.uniboot.framework.convert.EnumConvertor;
5+
import top.cadecode.uniboot.common.convertor.EnumConvertor;
66

77
import java.sql.CallableStatement;
88
import java.sql.PreparedStatement;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package top.cadecode.uniboot.common.mybatis.handler;
2+
3+
import org.apache.ibatis.type.BaseTypeHandler;
4+
import org.apache.ibatis.type.JdbcType;
5+
import org.apache.ibatis.type.MappedJdbcTypes;
6+
import org.apache.ibatis.type.MappedTypes;
7+
8+
import java.sql.CallableStatement;
9+
import java.sql.PreparedStatement;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
13+
/**
14+
* java 布尔值和 db 数字类型转换
15+
*
16+
* @author Cade Li
17+
* @since 2023/4/25
18+
*/
19+
@MappedTypes({Boolean.class})
20+
@MappedJdbcTypes({JdbcType.INTEGER})
21+
public class BoolToIntTypeHandler extends BaseTypeHandler<Boolean> {
22+
23+
@Override
24+
public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {
25+
if (parameter) {
26+
ps.setInt(i, 1);
27+
return;
28+
}
29+
ps.setInt(i, 0);
30+
}
31+
32+
@Override
33+
public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
34+
int i = rs.getInt(columnName);
35+
return i != 0;
36+
}
37+
38+
@Override
39+
public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
40+
int i = rs.getInt(columnIndex);
41+
return i != 0;
42+
}
43+
44+
@Override
45+
public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
46+
int i = cs.getInt(columnIndex);
47+
return i != 0;
48+
}
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package top.cadecode.uniboot.common.mybatis.handler;
2+
3+
import org.apache.ibatis.type.BaseTypeHandler;
4+
import org.apache.ibatis.type.JdbcType;
5+
import org.apache.ibatis.type.MappedJdbcTypes;
6+
import org.apache.ibatis.type.MappedTypes;
7+
import top.cadecode.uniboot.common.util.JacksonUtil;
8+
9+
import java.sql.CallableStatement;
10+
import java.sql.PreparedStatement;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.util.List;
14+
15+
/**
16+
* java 对象和 db varchar 转换,即 json 转换
17+
*
18+
* @author Cade Li
19+
* @since 2023/5/10
20+
*/
21+
@MappedTypes({Object.class, List.class})
22+
@MappedJdbcTypes(JdbcType.VARCHAR)
23+
public class ObjToStrTypeHandler extends BaseTypeHandler<Object> {
24+
25+
private final Class<?> type;
26+
27+
public ObjToStrTypeHandler(Class<?> type) {
28+
this.type = type;
29+
}
30+
31+
@Override
32+
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
33+
ps.setString(i, JacksonUtil.toJson(parameter));
34+
}
35+
36+
@Override
37+
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
38+
final String json = rs.getString(columnName);
39+
return JacksonUtil.toBean(json, type);
40+
}
41+
42+
@Override
43+
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
44+
final String json = rs.getString(columnIndex);
45+
return JacksonUtil.toBean(json, type);
46+
}
47+
48+
@Override
49+
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
50+
final String json = cs.getString(columnIndex);
51+
return JacksonUtil.toBean(json, type);
52+
}
53+
}

0 commit comments

Comments
 (0)