public class StringUtils {
/**
*
* @Title: modifyName
* @Description: 将表名和列名的命名改为驼峰式
* @param name
* @param type
* 1:表;其他:列
* @return
* @date 2017年10月20日 下午11:27:30
* @author Problem terminator
*/
public static String modifyName(String name, int type) {
String[] arrayOfName = name.split("_");
String result = null;
if (arrayOfName.length == 1) {
result = isFirstToUpperCase(arrayOfName, type);
} else {
result = isFirstToUpperCase(arrayOfName, type);
for (int i = 1; i < arrayOfName.length; i++) {
result = result + arrayOfName[i].substring(0, 1).toUpperCase() + arrayOfName[i].substring(1);
}
}
return result;
}
/**
*
* @Title: isFirstToUpperCase
* @Description: 判断首字母是否要大写
* @param arrayOfName
* @param type
* @return
* @date 2017年10月20日 下午11:28:34
* @author Problem terminator
*/
public static String isFirstToUpperCase(String[] arrayOfName, int type) {
String result = null;
if (1 == type) {
result = arrayOfName[0].substring(0, 1).toUpperCase() + arrayOfName[0].substring(1);
} else {
result = arrayOfName[0];
}
return result;
}
/**
* 列名转换 #{productId} 形式
* @Title: joinColumnNameForValuesOfMybatis
* @param columns 列名
* @return #{deviceId},#{mac},#{productId}
* @date 2018年11月7日 上午11:07:23
* @author yz
*/
public static String joinColumnNameForValuesOfMybatis(String prefix,String columns) {
String[] split = columns.split(",");
StringBuilder properties = new StringBuilder();
for (String string : split) {
properties.append("#").append("{").append((prefix != null ? prefix + "." : "") + StringUtils.modifyName(string, 0)).append("}").append(",");
}
return properties.deleteCharAt(properties.length() - 1).toString();
}
/**
* 列名转换user_id=#{userId}的形式
* @Title: joinPropertiesForValuesOfMybatis
* @param columns 列字符串,以逗号隔开:device_id,user_id
* @return device_id=#{deviceId},user_id=#{userId}
* @date 2018年11月7日 上午11:08:29
* @author yz
*/
public static String joinPropertiesForValuesOfMybatis(String columns) {
String[] split = columns.split(",");
List<String> list = new ArrayList<String>();
for (String string : split) {
String str = "#" + "{" + modifyName(string, 0) + "}";
list.add(str);
}
StringBuilder columnProperties = new StringBuilder();
for (int i = 0; i < split.length; i++) {
columnProperties.append(split[i]).append("=").append(list.get(i)).append(",");
}
return columnProperties.deleteCharAt(columnProperties.length() - 1).toString();
}
}
表、java、mybatis的转换
最新推荐文章于 2025-06-12 09:57:53 发布