Java教程

Mybatis学习04_SQL格式小工具

本文主要是介绍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;
    }
}
View Code

运行结果:

 

这篇关于Mybatis学习04_SQL格式小工具的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!