Commit 0815dfd7 authored by yanzg's avatar yanzg

实体生成

parent 827f4987
...@@ -840,4 +840,45 @@ public class StringHelper { ...@@ -840,4 +840,45 @@ public class StringHelper {
return sb.toString(); return sb.toString();
} }
/**
* 获取CamelCase命名,去掉下划线
*
* @param from 原名称
* @return 修改后的名称
*/
public static String getCamelCase(String from) {
StringBuilder result = new StringBuilder();
String a[] = from.split("_");
for (String s : a) {
if (result.length() == 0) {
result.append(s.substring(0, 1).toLowerCase());
result.append(s.substring(1));
} else {
result.append(s.substring(0, 1).toUpperCase());
result.append(s.substring(1));
}
}
return result.toString();
}
/***
* 驼峰命名转为下划线命名大写
*
* @param from
* 驼峰命名的字符串
*/
public static String getUnderLine(String from) {
if (from.contains("_")) {
return from;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < from.length(); i++) {
char c = from.charAt(i);
if (Character.isUpperCase(c)) {
sb.append("_");
}
sb.append(c);
}
return sb.toString().toUpperCase();
}
} }
...@@ -39,4 +39,13 @@ public class DaoConst { ...@@ -39,4 +39,13 @@ public class DaoConst {
* GROUP查询数据的SQL语句 * GROUP查询数据的SQL语句
*/ */
public static final String GroupQuery = "GroupQuery"; public static final String GroupQuery = "GroupQuery";
/**
* 驼峰式命名
*/
public static final int RowNameTypeCamelCase = 0;
/**
* 原字段
*/
public static final int RowNameTypeNoChange = 1;
} }
package com.yanzuoguang.db.Impl; package com.yanzuoguang.db.Impl;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.extend.ConfigDb; import com.yanzuoguang.extend.ConfigDb;
import com.yanzuoguang.util.base.ObjectHelper; import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.MapRow; import com.yanzuoguang.util.vo.MapRow;
import org.springframework.beans.*; import org.springframework.beans.*;
...@@ -110,7 +112,17 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -110,7 +112,17 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
* @param name the string containing original name * @param name the string containing original name
* @return the converted name * @return the converted name
*/ */
public static String underscoreName(String name) { public String underscoreName(String name) {
return underscoreNameBase(name);
}
/**
* 获取列明
*
* @param name
* @return
*/
private static String underscoreNameBase(String name) {
if (!StringUtils.hasLength(name)) { if (!StringUtils.hasLength(name)) {
return ""; return "";
} }
...@@ -126,7 +138,7 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -126,7 +138,7 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
public static MapRow toLoweRow(MapRow from) { public static MapRow toLoweRow(MapRow from) {
MapRow to = new MapRow(); MapRow to = new MapRow();
for (String key : from.keySet()) { for (String key : from.keySet()) {
to.put(underscoreName(key), from.get(key)); to.put(underscoreNameBase(key), from.get(key));
} }
return to; return to;
} }
...@@ -138,9 +150,9 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -138,9 +150,9 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
* @return * @return
*/ */
public static Object getLoweRowField(MapRow from, String field) { public static Object getLoweRowField(MapRow from, String field) {
field = underscoreName(field); field = underscoreNameBase(field);
for (String key : from.keySet()) { for (String key : from.keySet()) {
if (underscoreName(key).equals(field)) { if (underscoreNameBase(key).equals(field)) {
return from.get(key); return from.get(key);
} }
} }
...@@ -167,7 +179,7 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -167,7 +179,7 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
String underscoredName = underscoreName(column); String underscoredName = underscoreName(column);
if (this.isMapping) { if (this.isMapping) {
Object value = JdbcUtils.getResultSetValue(rs, index); Object value = JdbcUtils.getResultSetValue(rs, index);
((Map) mappedObject).put(column, value); ((Map) mappedObject).put(getCamelCase(column), value);
} else if (!this.mappedIsFields.containsKey(underscoredName)) { } else if (!this.mappedIsFields.containsKey(underscoredName)) {
continue; continue;
} else if (!this.mappedIsFields.get(underscoredName)) { } else if (!this.mappedIsFields.get(underscoredName)) {
...@@ -224,6 +236,24 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -224,6 +236,24 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
return mappedObject; return mappedObject;
} }
/**
* 设置SameCase命名方式
*
* @param column
* @return
*/
private String getCamelCase(String column) {
switch (configDb.getRowNameType()) {
case DaoConst.RowNameTypeNoChange:
break;
case DaoConst.RowNameTypeCamelCase:
default:
column = StringHelper.getCamelCase(column);
break;
}
return column;
}
/** /**
* Initialize the given BeanWrapper to be used for row mapping. * Initialize the given BeanWrapper to be used for row mapping.
* To be called for each row. * To be called for each row.
......
...@@ -12,28 +12,35 @@ public class ConfigDb { ...@@ -12,28 +12,35 @@ public class ConfigDb {
* 打印映射日志 * 打印映射日志
*/ */
@Value("${yzg.PrintMapper:true}") @Value("${yzg.PrintMapper:true}")
private boolean PrintMapper; private boolean printMapper;
/** /**
* 打印SQL日志 * 打印SQL日志
*/ */
@Value("${yzg.PrintSql:true}") @Value("${yzg.PrintSql:true}")
private boolean PrintSql; private boolean printSql;
/** /**
* 判断是否匹配过滤 * 判断是否匹配过滤
*/ */
@Value("${yzg.PrintSqlFilter:}") @Value("${yzg.PrintSqlFilter:}")
private String PrintSqlFilter; private String printSqlFilter;
@Value("${yzg.row.name.type:0}")
private int rowNameType;
public boolean isPrintMapper() { public boolean isPrintMapper() {
return PrintMapper; return printMapper;
} }
public boolean isPrintSql() { public boolean isPrintSql() {
return PrintSql; return printSql;
} }
public String getPrintSqlFilter() { public String getPrintSqlFilter() {
return PrintSqlFilter; return printSqlFilter;
}
public int getRowNameType() {
return rowNameType;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment