Skip to content

Commit 6a69ee7

Browse files
committed
新增注解@ColumnType可以设置typeHandler
1 parent 7c222fe commit 6a69ee7

File tree

9 files changed

+178
-21
lines changed

9 files changed

+178
-21
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package tk.mybatis.mapper.annotation;
2+
3+
import org.apache.ibatis.type.JdbcType;
4+
import org.apache.ibatis.type.TypeHandler;
5+
import org.apache.ibatis.type.UnknownTypeHandler;
6+
7+
import java.lang.annotation.ElementType;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.RetentionPolicy;
10+
import java.lang.annotation.Target;
11+
12+
/**
13+
* 针对列的复杂属性配置
14+
*
15+
* @author liuzh
16+
* @since 2015-10-29 22:00
17+
*/
18+
@Target({ElementType.FIELD})
19+
@Retention(RetentionPolicy.RUNTIME)
20+
public @interface ColumnType {
21+
String column() default "";
22+
23+
JdbcType jdbcType() default JdbcType.UNDEFINED;
24+
25+
Class<? extends TypeHandler<?>> typeHandler() default UnknownTypeHandler.class;
26+
}

src/main/java/tk/mybatis/mapper/entity/EntityColumn.java

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package tk.mybatis.mapper.entity;
22

3+
import org.apache.ibatis.type.JdbcType;
4+
import org.apache.ibatis.type.TypeHandler;
5+
import tk.mybatis.mapper.util.StringUtil;
6+
37
/**
48
* 数据库表对应的列
59
*
@@ -10,6 +14,8 @@ public class EntityColumn {
1014
private String property;
1115
private String column;
1216
private Class<?> javaType;
17+
private JdbcType jdbcType;
18+
private Class<? extends TypeHandler<?>> typeHandler;
1319
private String sequenceName;
1420
private boolean id = false;
1521
private boolean uuid = false;
@@ -56,6 +62,22 @@ public void setJavaType(Class<?> javaType) {
5662
this.javaType = javaType;
5763
}
5864

65+
public JdbcType getJdbcType() {
66+
return jdbcType;
67+
}
68+
69+
public void setJdbcType(JdbcType jdbcType) {
70+
this.jdbcType = jdbcType;
71+
}
72+
73+
public Class<? extends TypeHandler<?>> getTypeHandler() {
74+
return typeHandler;
75+
}
76+
77+
public void setTypeHandler(Class<? extends TypeHandler<?>> typeHandler) {
78+
this.typeHandler = typeHandler;
79+
}
80+
5981
public String getSequenceName() {
6082
return sequenceName;
6183
}
@@ -104,6 +126,49 @@ public void setOrderBy(String orderBy) {
104126
this.orderBy = orderBy;
105127
}
106128

129+
/**
130+
* 返回格式如:colum = #{age,jdbcType=NUMERIC,typeHandler=MyTypeHandler}
131+
*
132+
* @return
133+
*/
134+
public String getColumnEqualsHolder() {
135+
return this.column + " = " + getColumnHolder();
136+
}
137+
138+
/**
139+
* 返回格式如:#{age,jdbcType=NUMERIC,typeHandler=MyTypeHandler}
140+
*
141+
* @return
142+
*/
143+
public String getColumnHolder() {
144+
return getColumnHolder(null);
145+
}
146+
147+
/**
148+
* 返回格式如:#{entityName.age,jdbcType=NUMERIC,typeHandler=MyTypeHandler}
149+
*
150+
* @param entityName
151+
* @return
152+
*/
153+
public String getColumnHolder(String entityName) {
154+
StringBuffer sb = new StringBuffer("#{");
155+
if (StringUtil.isNotEmpty(entityName)) {
156+
sb.append(entityName);
157+
sb.append(".");
158+
}
159+
sb.append(this.property);
160+
if (this.jdbcType != null) {
161+
sb.append(",jdbcType=");
162+
sb.append(this.jdbcType.toString());
163+
}
164+
if (this.typeHandler != null) {
165+
sb.append(",typeHandler=");
166+
sb.append(this.typeHandler.getCanonicalName());
167+
}
168+
sb.append("}");
169+
return sb.toString();
170+
}
171+
107172
@Override
108173
public boolean equals(Object o) {
109174
if (this == o) return true;
@@ -112,24 +177,28 @@ public boolean equals(Object o) {
112177
EntityColumn that = (EntityColumn) o;
113178

114179
if (id != that.id) return false;
115-
if (identity != that.identity) return false;
116180
if (uuid != that.uuid) return false;
181+
if (identity != that.identity) return false;
182+
if (table != null ? !table.equals(that.table) : that.table != null) return false;
183+
if (property != null ? !property.equals(that.property) : that.property != null) return false;
117184
if (column != null ? !column.equals(that.column) : that.column != null) return false;
118-
if (generator != null ? !generator.equals(that.generator) : that.generator != null) return false;
119185
if (javaType != null ? !javaType.equals(that.javaType) : that.javaType != null) return false;
120-
if (orderBy != null ? !orderBy.equals(that.orderBy) : that.orderBy != null) return false;
121-
if (property != null ? !property.equals(that.property) : that.property != null) return false;
122-
if (sequenceName != null ? !sequenceName.equals(that.sequenceName) : that.sequenceName != null)
123-
return false;
186+
if (jdbcType != that.jdbcType) return false;
187+
if (typeHandler != null ? !typeHandler.equals(that.typeHandler) : that.typeHandler != null) return false;
188+
if (sequenceName != null ? !sequenceName.equals(that.sequenceName) : that.sequenceName != null) return false;
189+
if (generator != null ? !generator.equals(that.generator) : that.generator != null) return false;
190+
return !(orderBy != null ? !orderBy.equals(that.orderBy) : that.orderBy != null);
124191

125-
return true;
126192
}
127193

128194
@Override
129195
public int hashCode() {
130-
int result = property != null ? property.hashCode() : 0;
196+
int result = table != null ? table.hashCode() : 0;
197+
result = 31 * result + (property != null ? property.hashCode() : 0);
131198
result = 31 * result + (column != null ? column.hashCode() : 0);
132199
result = 31 * result + (javaType != null ? javaType.hashCode() : 0);
200+
result = 31 * result + (jdbcType != null ? jdbcType.hashCode() : 0);
201+
result = 31 * result + (typeHandler != null ? typeHandler.hashCode() : 0);
133202
result = 31 * result + (sequenceName != null ? sequenceName.hashCode() : 0);
134203
result = 31 * result + (id ? 1 : 0);
135204
result = 31 * result + (uuid ? 1 : 0);

src/main/java/tk/mybatis/mapper/mapperhelper/EntityHelper.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
package tk.mybatis.mapper.mapperhelper;
2626

27+
import org.apache.ibatis.type.JdbcType;
28+
import org.apache.ibatis.type.UnknownTypeHandler;
29+
import tk.mybatis.mapper.annotation.ColumnType;
2730
import tk.mybatis.mapper.annotation.NameStyle;
2831
import tk.mybatis.mapper.code.IdentityDialect;
2932
import tk.mybatis.mapper.code.Style;
@@ -165,7 +168,7 @@ public static String getPrimaryKeyWhere(Class<?> entityClass) {
165168
Set<EntityColumn> entityColumns = getPKColumns(entityClass);
166169
StringBuilder whereBuilder = new StringBuilder();
167170
for (EntityColumn column : entityColumns) {
168-
whereBuilder.append(column.getColumn()).append(" = #{").append(column.getProperty()).append("} AND ");
171+
whereBuilder.append(column.getColumnEqualsHolder()).append(" AND ");
169172
}
170173
return whereBuilder.substring(0, whereBuilder.length() - 4);
171174
}
@@ -221,7 +224,22 @@ public static synchronized void initEntityNameMap(Class<?> entityClass, Config c
221224
Column column = field.getAnnotation(Column.class);
222225
columnName = column.name();
223226
}
224-
if (columnName == null || columnName.equals("")) {
227+
//ColumnType
228+
if (field.isAnnotationPresent(ColumnType.class)) {
229+
ColumnType columnType = field.getAnnotation(ColumnType.class);
230+
//column可以起到别名的作用
231+
if(StringUtil.isEmpty(columnName) && StringUtil.isNotEmpty(columnType.column())){
232+
columnName = columnType.column();
233+
}
234+
if(columnType.jdbcType() != JdbcType.UNDEFINED){
235+
entityColumn.setJdbcType(columnType.jdbcType());
236+
}
237+
if(columnType.typeHandler() != UnknownTypeHandler.class){
238+
entityColumn.setTypeHandler(columnType.typeHandler());
239+
}
240+
}
241+
//表名
242+
if (StringUtil.isEmpty(columnName)) {
225243
columnName = StringUtil.convertByStyle(field.getName(), style);
226244
}
227245
entityColumn.setProperty(field.getName());
@@ -302,7 +320,7 @@ public static synchronized void initEntityNameMap(Class<?> entityClass, Config c
302320
*/
303321
private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldList) {
304322
if (fieldList == null) {
305-
fieldList = new LinkedList<Field>();
323+
fieldList = new ArrayList<Field>();
306324
}
307325
if (entityClass.equals(Object.class)) {
308326
return fieldList;

src/main/java/tk/mybatis/mapper/mapperhelper/MapperTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ protected SqlNode getIfCacheIsNull(EntityColumn column, SqlNode columnNode) {
477477
* @return
478478
*/
479479
protected SqlNode getColumnEqualsProperty(EntityColumn column, boolean first) {
480-
return new StaticTextSqlNode((first ? "" : " AND ") + column.getColumn() + " = #{" + column.getProperty() + "} ");
480+
return new StaticTextSqlNode((first ? "" : " AND ") + column.getColumnEqualsHolder());
481481
}
482482

483483
/**

src/main/java/tk/mybatis/mapper/provider/SpecialProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public String insertList(MappedStatement ms) {
7777
if(!first) {
7878
sql.append(",");
7979
}
80-
sql.append("#{record.").append(column.getProperty()).append("}");
80+
sql.append(column.getColumnHolder("record"));
8181
first = false;
8282
}
8383
sql.append(")");
@@ -118,7 +118,7 @@ public String insertUseGeneratedKeys(MappedStatement ms) {
118118
if(!first) {
119119
sql.append(",");
120120
}
121-
sql.append("#{").append(column.getProperty()).append("}");
121+
sql.append(column.getColumnHolder());
122122
first = false;
123123
}
124124
sql.append(")");

src/main/java/tk/mybatis/mapper/provider/SqlServerProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public String insert(MappedStatement ms) {
8080
if(!first) {
8181
sql.append(",");
8282
}
83-
sql.append("#{").append(column.getProperty()).append("}");
83+
sql.append(column.getColumnHolder());
8484
first = false;
8585
}
8686
sql.append(")");
@@ -120,7 +120,7 @@ public SqlNode insertSelective(MappedStatement ms) {
120120
for (EntityColumn column : columnList) {
121121
//当参数中的属性值不为空的时候,使用传入的值
122122
if (!column.isId()) {
123-
ifNodes.add(new IfSqlNode(new StaticTextSqlNode("#{" + column.getProperty() + "},"), column.getProperty() + " != null "));
123+
ifNodes.add(new IfSqlNode(new StaticTextSqlNode(column.getColumnHolder() +","), column.getProperty() + " != null "));
124124
}
125125
}
126126
//values(#{property},#{property}...)

src/main/java/tk/mybatis/mapper/provider/base/BaseInsertProvider.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,19 @@ public SqlNode insert(MappedStatement ms) {
100100
ifNodes.add(getIfCacheNotNull(column, new StaticTextSqlNode("#{" + column.getProperty() + "_cache },")));
101101
} else {
102102
//其他情况值仍然存在原property中
103-
ifNodes.add(getIfNotNull(column, new StaticTextSqlNode("#{" + column.getProperty() + "},")));
103+
ifNodes.add(getIfNotNull(column, new StaticTextSqlNode(column.getColumnHolder() + ",")));
104104
}
105105
//当属性为null时,如果存在主键策略,会自动获取值,如果不存在,则使用null
106106
//序列的情况
107107
if (StringUtil.isNotEmpty(column.getSequenceName())) {
108108
ifNodes.add(getIfIsNull(column, new StaticTextSqlNode(getSeqNextVal(column) + " ,")));
109109
} else if (column.isIdentity()) {
110-
ifNodes.add(getIfCacheIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + " },")));
110+
ifNodes.add(getIfCacheIsNull(column, new StaticTextSqlNode(column.getColumnHolder() + ",")));
111111
} else if (column.isUuid()) {
112112
ifNodes.add(getIfIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + "_bind },")));
113113
} else {
114114
//当null的时候,如果不指定jdbcType,oracle可能会报异常,指定VARCHAR不影响其他
115-
ifNodes.add(getIfIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + ",jdbcType=VARCHAR},")));
115+
ifNodes.add(getIfIsNull(column, new StaticTextSqlNode(column.getColumnHolder() + ",")));
116116
}
117117
}
118118
//values(#{property},#{property}...)
@@ -178,12 +178,12 @@ public SqlNode insertSelective(MappedStatement ms) {
178178
if (column.isIdentity()) {
179179
ifNodes.add(new IfSqlNode(new StaticTextSqlNode("#{" + column.getProperty() + "_cache },"), column.getProperty() + "_cache != null "));
180180
} else {
181-
ifNodes.add(new IfSqlNode(new StaticTextSqlNode("#{" + column.getProperty() + "},"), column.getProperty() + " != null "));
181+
ifNodes.add(new IfSqlNode(new StaticTextSqlNode(column.getColumnHolder() + ","), column.getProperty() + " != null "));
182182
}
183183
if (StringUtil.isNotEmpty(column.getSequenceName())) {
184184
ifNodes.add(getIfIsNull(column, new StaticTextSqlNode(getSeqNextVal(column) + " ,")));
185185
} else if (column.isIdentity()) {
186-
ifNodes.add(getIfCacheIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + " },")));
186+
ifNodes.add(getIfCacheIsNull(column, new StaticTextSqlNode(column.getColumnHolder() + ",")));
187187
} else if (column.isUuid()) {
188188
ifNodes.add(getIfIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + "_bind },")));
189189
}

src/test/java/tk/mybatis/mapper/model/Country.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package tk.mybatis.mapper.model;
22

3+
import org.apache.ibatis.type.JdbcType;
4+
import tk.mybatis.mapper.annotation.ColumnType;
35
import tk.mybatis.mapper.entity.IDynamicTableName;
6+
import tk.mybatis.mapper.typehandler.StringType2Handler;
47

58
import javax.persistence.Column;
69
import javax.persistence.Id;
@@ -21,6 +24,7 @@ public class Country implements Serializable, IDynamicTableName {
2124
private Integer id;
2225

2326
@Column
27+
@ColumnType(jdbcType = JdbcType.VARCHAR, typeHandler = StringType2Handler.class)
2428
private String countryname;
2529
private String countrycode;
2630

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tk.mybatis.mapper.typehandler;
2+
3+
import org.apache.ibatis.type.BaseTypeHandler;
4+
import org.apache.ibatis.type.JdbcType;
5+
6+
import java.sql.CallableStatement;
7+
import java.sql.PreparedStatement;
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
11+
/**
12+
* @author liuzh_3nofxnp
13+
* @since 2015-10-29 22:48
14+
*/
15+
public class StringType2Handler extends BaseTypeHandler<String> {
16+
17+
@Override
18+
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
19+
throws SQLException {
20+
ps.setString(i, parameter + "_hehe");
21+
}
22+
23+
@Override
24+
public String getNullableResult(ResultSet rs, String columnName)
25+
throws SQLException {
26+
return rs.getString(columnName) + "_hehe";
27+
}
28+
29+
@Override
30+
public String getNullableResult(ResultSet rs, int columnIndex)
31+
throws SQLException {
32+
return rs.getString(columnIndex) + "_hehe";
33+
}
34+
35+
@Override
36+
public String getNullableResult(CallableStatement cs, int columnIndex)
37+
throws SQLException {
38+
return cs.getString(columnIndex) + "_hehe";
39+
}
40+
}

0 commit comments

Comments
 (0)