/**
* 配置表名称,自动生成mybatis中sql语句
*/
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTemplteUtil {
protected final Log logger = LogFactory.getLog(this.getClass());
// oracle数据库url
private static String url = "jdbc:oracle:thin:@//127.0.0.1:1521/orcl";
// oracle数据库的用户名
private static String user = "xxx_test";
// oracle数据库的用户密码
private static String password = "xxx_test";
private static Connection conn;
private static PreparedStatement ps;
public static ResultSet rs;
public static Statement st;
// 生成查询、新增、修改的语句
public static void main(String[] args) throws Exception {
// 设置表名
getAutoCode("table_xxx".toUpperCase());
}
private static void getAutoCode(String tableName) throws Exception {
String sql = "select s.TABLE_NAME,s.COLUMN_NAME,"
+ " s.DATA_TYPE,c.COMMENTS,s.DATA_PRECISION,s.DATA_SCALE from user_tab_columns s left join user_col_comments c on s.COLUMN_NAME "
+ " = c.COLUMN_NAME and s.TABLE_NAME = c.TABLE_NAME"
+ " where s.Table_Name= '" + tableName + "' order by s.COLUMN_ID asc";
JDBCTemplteUtil basedao = new JDBCTemplteUtil();
Connection conn = basedao.getConnection();
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
String cloumns = "";
// 查询sql
StringBuffer selectSql = new StringBuffer();
StringBuffer addSql = new StringBuffer();
StringBuffer updateSql = new StringBuffer();
StringBuffer insertsql = new StringBuffer();
// 字段属性
StringBuffer properties = new StringBuffer();
selectSql.append("\t" + "<select id=" + "\"list" + "" + "\"" + " parameterType=\"\" resultType=\"\"> " + "\n")
.append("\t\t\t" + "select" + "\n");
addSql.append("\t" + "<insert id=" + "\"save" + "" + "\"" + " parameterType=\"\"> " + "\n")
.append("\t\t\t" + "insert into @{" + tableName + "}(\n");
updateSql.append("\t" + "<update id=" + "\"update" + "" + "\"" + " parameterType=\"\"> " + "\n")
.append("\t\t\t" + "update \t@{" + tableName + "} \n")
.append("\t\t\t" + "<set> \n");
StringBuffer columnList = new StringBuffer();
String columnName = "";
String comments = "";
String dataType = "";
int dataScale = 0;
while (rs.next()) {
// 字段类型
dataType = rs.getString("DATA_TYPE");
// 字段名称
columnName = rs.getString("COLUMN_NAME");
comments = rs.getString("COMMENTS");
dataScale = rs.getInt("DATA_SCALE");
if (dataType.equals("VARCHAR2")) {
dataType = "VARCHAR";
}
columnList.append("\t\t\t\t" + columnName + "," + (comments == null ? "" : "\t" + "<!--" + comments + "-->") + "\n");
insertsql.append("\t\t\t\t" + "#{" + columnName.toLowerCase() + "},\n");
updateSql.append("\t\t\t\t" + "<if test=\"" + columnName.toLowerCase() + " != null\"> " + columnName + " = #{" + columnName.toLowerCase() + "}, </if>\t" + (comments == null ? "" : "<!--" + comments + "-->") + "\n");
properties.append("\t" + "@Column" + "\n");
if (dataType.contains("VARCHAR")) {
properties.append("\t" + "private String " + columnName.toLowerCase() + "; " + (comments == null ? "" : "//" + comments) + "\n");
} else if (dataType.contains("NUMBER")) {
if (dataScale == 0) {
properties.append("\t" + "private Integer " + columnName.toLowerCase() + "; " + (comments == null ? "" : "//" + comments) + "\n");
} else {
properties.append("\t" + "private BigDecimal " + columnName.toLowerCase() + "; " + (comments == null ? "" : "//" + comments) + "\n");
}
} else if (dataType.contains("CLOB")) {
properties.append("\t" + "private String " + columnName.toLowerCase() + "; " + (comments == null ? "" : "//" + comments) + "\n");
} else {
properties.append("\t" + "private String " + columnName.toLowerCase() + "; " + (comments == null ? "" : "//" + comments) + "\n");
}
}
selectSql.append(columnList);
cloumns = selectSql.toString();
int index = cloumns.lastIndexOf(",");
String str = cloumns.substring(0, index) + cloumns.substring(index + 1);
String selectSqlStr = str + ("\t\t\t" + "from" + "\t@{" + tableName + "}\n\t" + "</select>");
String insertList = insertsql.toString();
index = insertList.lastIndexOf(",");
insertList = insertList.substring(0, index) + insertList.substring(index + 1);
String columnListStr = columnList.toString();
index = columnListStr.lastIndexOf(",");
columnListStr = columnListStr.substring(0, index) + columnListStr.substring(index + 1);
addSql.append(columnListStr + "\t\t\t" + ")values(" + "\n" + insertList + "\t\t\t)");
String updateSqlStr = updateSql.toString();
index = updateSqlStr.lastIndexOf(",");
updateSqlStr = updateSqlStr.substring(0, index) + updateSqlStr.substring(index + 1);
updateSqlStr = updateSqlStr + "\t\t\t</set>";
System.out.println(selectSqlStr + "\n");
System.out.println(addSql.toString() + "\n" + "\t" + "</insert>" + "\n");
System.out.println(updateSqlStr + "\n" + "\t" + "</update>" + "\n");
System.out.println(properties.toString());
conn.close();
}
/**
* @Description: 连接数据库的方法
*/
private Connection getConnection() {
try {
// 初始化oracle驱动包
Class.forName("oracle.jdbc.driver.OracleDriver");
// 根据数据库url、用户名、密码获取连接
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
logger.error("连接oracle异常", e);
}
return conn;
}
}
配置表名称,自动生成mybatis中sql语句
最新推荐文章于 2022-06-03 16:34:30 发布