Mybatis学习04_SQL格式小工具
在映射配置文件中编写添加/修改SQL语句时,需要添加#{}格式的属性,分享小工具
<insert id="insertUser">
insert into sysuser(uname, upwd) values (#{uname},#{upwd})
</insert>
sql.properties
key=uname, upwd, uemail, phone, create_time, update_time, deleted, rid
SqlUtil_1
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class SqlUtil_1 { public static void main(String[] args) { String sqlValues = getSqlValues(); System.out.println(sqlValues); } public static String getSqlValues() { String string = ""; InputStream is = SqlUtil_1.class.getClassLoader().getResourceAsStream("sql.properties"); Properties pros = new Properties(); try { pros.load(is); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } string+=(String.valueOf(pros.values())); String[] strings = string.split(","); StringBuffer sb = new StringBuffer(); sb.append("#{"); String tempString=""; for (int i = 0 ;i< strings.length;i++){ tempString =strings[i]; if(tempString.contains("_")){ String wordCase = (tempString.charAt(tempString.indexOf("_")+ 1)+""); String newString = tempString.replaceAll(("_" + wordCase), wordCase.toUpperCase()); sb.append(newString); }else { sb.append(tempString); } if(i!=strings.length-1){ sb.append(","); } } String str = sb.toString(); String substring = str.replaceAll("\\[",""); str = substring.replaceAll(",",",#{").replaceAll("]","").replaceAll(",","},"); str+="}"; return str; } }
运行结果: