Skip to content

Commit b44808a

Browse files
committed
增加tyle支持,可以控制字段和数据库的转换规则,支持下面4种:
normal, //原值 camelhump, //驼峰转下划线 uppercase, //转换为大写 lowercase //转换为小写
1 parent 031205e commit b44808a

File tree

12 files changed

+203
-229
lines changed

12 files changed

+203
-229
lines changed

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,12 @@ public class EntityHelper {
5454
public static EntityTable getEntityTable(Class<?> entityClass) {
5555
EntityTable entityTable = entityTableMap.get(entityClass);
5656
if (entityTable == null) {
57-
initEntityNameMap(entityClass);
58-
entityTable = entityTableMap.get(entityClass);
59-
}
60-
if (entityTable == null) {
61-
throw new RuntimeException("无法获取实体类" + entityClass.getCanonicalName() + "对应的表名!");
57+
throw new RuntimeException("无法获取实体类" + entityClass.getCanonicalName() + "对应的数据库信息!");
6258
}
6359
return entityTable;
6460
}
6561

62+
6663
/**
6764
* 获取默认的orderby语句
6865
*
@@ -172,11 +169,17 @@ public static String getPrimaryKeyWhere(Class<?> entityClass) {
172169
* 初始化实体属性
173170
*
174171
* @param entityClass
172+
* @param style
175173
*/
176-
public static synchronized void initEntityNameMap(Class<?> entityClass) {
174+
public static synchronized void initEntityNameMap(Class<?> entityClass, Style style) {
177175
if (entityTableMap.get(entityClass) != null) {
178176
return;
179177
}
178+
//style,该注解优先于全局配置
179+
if(entityClass.isAnnotationPresent(NameStyle.class)){
180+
NameStyle nameStyle = entityClass.getAnnotation(NameStyle.class);
181+
style = nameStyle.value();
182+
}
180183
//表名
181184
EntityTable entityTable = null;
182185
if (entityClass.isAnnotationPresent(Table.class)) {
@@ -188,9 +191,10 @@ public static synchronized void initEntityNameMap(Class<?> entityClass) {
188191
}
189192
if (entityTable == null) {
190193
entityTable = new EntityTable();
191-
//对大小写敏感的情况,这里不自动转换大小写,如果有需要,通过@Table注解实现
192-
entityTable.name = camelhumpToUnderline(entityClass.getSimpleName());
194+
//可以通过stye控制
195+
entityTable.name = convertByStyle(entityClass.getSimpleName(), style);
193196
}
197+
194198
//列
195199
List<Field> fieldList = getAllField(entityClass, null);
196200
Set<EntityColumn> columnSet = new LinkedHashSet<EntityColumn>();
@@ -210,7 +214,7 @@ public static synchronized void initEntityNameMap(Class<?> entityClass) {
210214
columnName = column.name();
211215
}
212216
if (columnName == null || columnName.equals("")) {
213-
columnName = camelhumpToUnderline(field.getName());
217+
columnName = convertByStyle(field.getName(), style);
214218
}
215219
entityColumn.setProperty(field.getName());
216220
entityColumn.setColumn(columnName);
@@ -280,6 +284,27 @@ public static synchronized void initEntityNameMap(Class<?> entityClass) {
280284
entityTableMap.put(entityClass, entityTable);
281285
}
282286

287+
/**
288+
* 根据指定的样式进行转换
289+
*
290+
* @param str
291+
* @param style
292+
* @return
293+
*/
294+
public static String convertByStyle(String str, Style style){
295+
switch (style){
296+
case camelhump:
297+
return camelhumpToUnderline(str);
298+
case uppercase:
299+
return str.toUpperCase();
300+
case lowercase:
301+
return str.toLowerCase();
302+
case normal:
303+
default:
304+
return str;
305+
}
306+
}
307+
283308
/**
284309
* 将驼峰风格替换为下划线风格
285310
*/

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ public class MapperHelper {
7575
* 对于一般的getAllIfColumnNode,是否判断!='',默认不判断
7676
*/
7777
private boolean notEmpty = false;
78+
79+
/**
80+
* 字段转换风格,默认驼峰转下划线
81+
*/
82+
private Style style;
83+
7884
private Config config = new Config();
7985

8086
/**
@@ -102,6 +108,10 @@ public void setNotEmpty(boolean notEmpty) {
102108
this.notEmpty = notEmpty;
103109
}
104110

111+
public Style getStyle() {
112+
return style;
113+
}
114+
105115
/**
106116
* 检测Spring版本号,Spring4.x以上支持泛型注入
107117
*/
@@ -492,6 +502,17 @@ public void setProperties(Properties properties) {
492502
if (notEmpty != null && notEmpty.length() > 0) {
493503
this.notEmpty = notEmpty.equalsIgnoreCase("TRUE");
494504
}
505+
String styleStr = properties.getProperty("style");
506+
if (styleStr != null && styleStr.length() > 0) {
507+
try {
508+
this.style = Style.valueOf(styleStr);
509+
} catch (IllegalArgumentException e){
510+
throw new RuntimeException(styleStr + "不是合法的Style值!");
511+
}
512+
} else {
513+
//默认驼峰
514+
this.style = Style.camelhump;
515+
}
495516
//注册通用接口
496517
String mapper = properties.getProperty("mappers");
497518
if (mapper != null && mapper.length() > 0) {

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
public abstract class MapperTemplate {
5252
private XMLLanguageDriver languageDriver = new XMLLanguageDriver();
5353
private Map<String, Method> methodMap = new HashMap<String, Method>();
54+
private Map<String, Class<?>> entityClassMap = new HashMap<String, Class<?>>();
5455
private Class<?> mapperClass;
5556
private MapperHelper mapperHelper;
5657

@@ -204,14 +205,21 @@ public void setSqlSource(MappedStatement ms) throws Exception {
204205
*/
205206
public Class<?> getSelectReturnType(MappedStatement ms) {
206207
String msId = ms.getId();
207-
Class<?> mapperClass = getMapperClass(msId);
208-
Type[] types = mapperClass.getGenericInterfaces();
209-
for (Type type : types) {
210-
if (type instanceof ParameterizedType) {
211-
ParameterizedType t = (ParameterizedType) type;
212-
if (t.getRawType() == this.mapperClass || this.mapperClass.isAssignableFrom((Class<?>) t.getRawType())) {
213-
Class<?> returnType = (Class<?>) t.getActualTypeArguments()[0];
214-
return returnType;
208+
if(entityClassMap.containsKey(msId)){
209+
return entityClassMap.get(msId);
210+
} else {
211+
Class<?> mapperClass = getMapperClass(msId);
212+
Type[] types = mapperClass.getGenericInterfaces();
213+
for (Type type : types) {
214+
if (type instanceof ParameterizedType) {
215+
ParameterizedType t = (ParameterizedType) type;
216+
if (t.getRawType() == this.mapperClass || this.mapperClass.isAssignableFrom((Class<?>) t.getRawType())) {
217+
Class<?> returnType = (Class<?>) t.getActualTypeArguments()[0];
218+
//获取该类型后,第一次对该类型进行初始化
219+
EntityHelper.initEntityNameMap(returnType, mapperHelper.getStyle());
220+
entityClassMap.put(msId, returnType);
221+
return returnType;
222+
}
215223
}
216224
}
217225
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tk.mybatis.mapper.mapperhelper;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* 名字转换样式,注解的优先级高于全局配置
10+
*/
11+
@Target({ElementType.TYPE})
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface NameStyle {
14+
15+
Style value() default Style.normal;
16+
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package tk.mybatis.mapper.mapperhelper;
2+
3+
/**
4+
* 字段转换方式
5+
*/
6+
public enum Style {
7+
normal, //原值
8+
camelhump, //驼峰转下划线
9+
uppercase, //转换为大写
10+
lowercase //转换为小写
11+
}
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,54 @@
11
package tk.mybatis.mapper.mapper;
22

3+
import org.apache.ibatis.io.Resources;
4+
import org.apache.ibatis.jdbc.ScriptRunner;
35
import org.apache.ibatis.session.SqlSession;
6+
import org.apache.ibatis.session.SqlSessionFactory;
7+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
8+
9+
import java.io.IOException;
10+
import java.io.Reader;
11+
import java.sql.Connection;
412

513
/**
614
* Description: MybatisHelper
715
* Author: liuzh
816
* Update: liuzh(2014-06-06 13:33)
917
*/
1018
public class MybatisHelper {
19+
private static SqlSessionFactory sqlSessionFactory;
20+
21+
static {
22+
try {
23+
//创建SqlSessionFactory
24+
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
25+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
26+
reader.close();
27+
//创建数据库
28+
SqlSession session = null;
29+
try {
30+
session = sqlSessionFactory.openSession();
31+
Connection conn = session.getConnection();
32+
reader = Resources.getResourceAsReader("CreateDB.sql");
33+
ScriptRunner runner = new ScriptRunner(conn);
34+
runner.setLogWriter(null);
35+
runner.runScript(reader);
36+
reader.close();
37+
} finally {
38+
if (session != null) {
39+
session.close();
40+
}
41+
}
42+
} catch (IOException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
1147
/**
1248
* 获取Session
1349
* @return
1450
*/
1551
public static SqlSession getSqlSession(){
16-
return MybatisJavaHelper.getSqlSession();
52+
return sqlSessionFactory.openSession();
1753
}
1854
}

src/test/java/tk/mybatis/mapper/mapper/MybatisJavaHelper.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/test/java/tk/mybatis/mapper/mapper/MybatisXmlHelper.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)