package test
import java.util.ArrayList
import java.util.List
public class Test {
public static void main(String[] args) {
//testing for data base column-key to java field
String[] dbKeys = {"id", "user_age", "user_addr_"}
Test t = new Test()
t.convertToJava(dbKeys)
System.out.println("-----------------------------------")
//testing for Java field to data base column-key
String javaFieldNames[] = {"id","userAge","userHomeAddr"}
t.getDBKey(javaFieldNames)
}
/*
* Java field to data base column-key
*/
private void getDBKey(String... javaFieldNames){
if(javaFieldNames != null && javaFieldNames.length > 0){
for(String name : javaFieldNames){
StringBuffer buffer = new StringBuffer()
char[] array = name.toCharArray()
List<Integer> insertIndexes = new ArrayList<>()
for(int i=0i<array.lengthi++){
Character c = array[i]
if(i != 0 && Character.isUpperCase(c)){
insertIndexes.add(i)
}
}
if(insertIndexes.size() > 0){
int flag = 0
for(int j=0j<insertIndexes.size()j++){
String word = toLowercase4FirstLetter(name.substring(flag, insertIndexes.get(j)))
buffer.append(word).append("_")
flag = insertIndexes.get(j)
}
String last = toLowercase4FirstLetter(name.substring(flag))
buffer.append(last)
System.out.println(buffer.toString())
} else {
System.out.println(name)
}
}
}
}
private String toLowercase4FirstLetter(String word){
if(word != null && word.length() > 0){
String firstLetter = word.substring(0,1)
String others = word.substring(1)
return firstLetter.toLowerCase() + others
}else{
return ""
}
}
/*
* data base column-key to java field
*/
public void convertToJava(String... dbKeys) {
if(dbKeys != null && dbKeys.length > 0){
for(String key : dbKeys){
String[] words = key.split("_")
String result = toUppercase4FirstLetter(words)
System.out.println(result)
}
}
}
private String toUppercase4FirstLetter(String... words){
StringBuffer buffer = new StringBuffer()
if(words != null && words.length > 0){
for(int i=0i<words.lengthi++){
String word = words[i]
String firstLetter = word.substring(0, 1)
String others = word.substring(1)
String upperLetter = null
if(i != 0){
upperLetter = firstLetter.toUpperCase()
} else {
upperLetter = firstLetter
}
buffer.append(upperLetter).append(others)
}
return buffer.toString()
}
return ""
}
}
map-underscore-to-camel-case: true
但是有些没有VO的 只是一两个字段的 直接返回map给前端的
就在配置文件
下加入
重启 返回的map数据也转为驼峰了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)