⾃动⽣成⼩⼯具(⼆):根据建表sql⾃动⽣成增删改查sql语
句⽂件
Mybatis⾃动⽣成插件虽然功能强⼤,但是也略显笨重。我⾃⼰开发了⼀个⾃动⽣成的⼩⼯具,更加简单,更加轻量级。
⼀共只有⼏百⾏代码,想改的话,直接修改即可。根据⾃⼰的实际情况,可以进⾏灵活的⼆次开发。
Talk is cheap,show me the code.
⾃⼰写的,可以直接跑。如有问题,请联系,谢谢。
⼀,程序⼊⼝核⼼类:GenSqlXml.java
package cn.sephora.product.elasticsearch.service.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
/**
* 功能:根据mysql建表语句⽂件(XXX.sql),⾃动⽣成mapper⽂件(l)
* @date 2019/12/13
* @address shanghai
*
*/
public class GenSqlXml {
/**
* 源⽂件路径
*/
private static final String SOURCE_FILE_PATH = "D:\\sourcefile\\";
/**
* 源⽂件名称
*/
private static final String SOURCE_FILE_NAME = "table_create";
/**
* 源⽂件后缀
*/
private static final String SOURCE_FILE_SUFFIX = "sql";
/**
* ⽬标⽂件路径
*/
private static final String TARGET_FILE_PATH = "D:\\targetfile\\";
/**
* ⽬标⽂件名称
*/
private static final String TARGET_FILE_NAME = "SqlMapper";
/**
* ⽬标⽂件后缀
*/
private static final String TARGET_FILE_SUFFIX = "xml";
/**
* 换⾏符
*/
private static final String LINE_BREAK = "\r\n";
/**
* sqlxml⽂件的namespace
*/
private static final String NAMESPACE = "dao.StoreActivityDao";
public static void main(String[] args) {
File fileObj = new File(SOURCE_FILE_PATH + SOURCE_FILE_NAME + "." + SOURCE_FILE_SUFFIX);
ParseSqlFile parseSqlFile = new ParseSqlFile();
TableInfo tableInfo = FieldInfo(fileObj);
GenSqlXml genSqlXml = new GenSqlXml();
}
public void genSqlXmlFile(TableInfo tableInfo) {
genTargetSqlXmlFile(tableInfo);
}
private void genTargetSqlXmlFile(TableInfo tableInfo) {
FileOutputStream fos = null;
try {
File f1 = new File(TARGET_FILE_PATH + TARGET_FILE_NAME + "." + TARGET_FILE_SUFFIX); if (!f1.exists()){
}
fos = new FileOutputStream(f1);
// ⽣成⽂件头
fos.write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_BREAK).getBytes());
fos.write(("<!DOCTYPE mapper" + LINE_BREAK).getBytes());
fos.write((" PUBLIC \"-////DTD Mapper 3.0//EN\"" + LINE_BREAK).getBytes());
fos.write((" \"/dtd/mybatis-3-mapper.dtd\">" + LINE_BREAK).getBytes());
fos.write(("<mapper namespace=\"" + NAMESPACE + "\">" + LINE_BREAK).getBytes());
fos.write(Bytes());
// ⽣成select语句
genSelet(fos, tableInfo);
/
/ ⽣成insert语句
genInsert(fos, tableInfo);
// ⽣成update语句
genUpdate(fos, tableInfo);
// ⽣成delete语句
genDelete(fos, tableInfo);
// ⽣成⽂件尾部
fos.write(("</mapper>" + LINE_BREAK).getBytes());
} catch (IOException e) {
System.out.println("catch " + e);
} finally {
try {
fos.close();
} catch (IOException e) {
System.out.println("finally " + e);
}
}
}
/**
* ⽣成select语句
* @param fos
* @param tableInfo
*/
private void genSelet(FileOutputStream fos, TableInfo tableInfo){
List<FieldInfo> list = FieldList();
String resultMapId = "BaseResultMap";
try {
// <resultMap>
fos.write((" <resultMap id=" + resultMapId + " type=\"del.MktResourceModel\">" + LINE_BREAK).getBytes());
fos.write((" <id column=\"" + PrimaryKeyDB() + "\" property=\"" + PrimaryKeyJava() + "\" jdbcType=\"" + Primar for (FieldInfo fieldInfo : list) {
fos.write((" <result column=\"" + FieldNameDB() + "\" property=\"" + FieldNameJava() + "\" jdbcType=\"" + Dat }
fos.write((" </resultMap>" + LINE_BREAK).getBytes());
fos.write(Bytes());
// <sql>
StringBuffer fieldListDBStr = new StringBuffer();
fos.write((" <sql id=\"Base_Column_List\">" + LINE_BREAK).getBytes());
for (FieldInfo fieldInfo : list) {
fieldListDBStr.FieldNameDB()).append(",");
}
fos.write((" " + fieldListDBStr.substring(0, fieldListDBStr.lastIndexOf(",")).toString() + LINE_BREAK).getBytes());
fos.write((" </sql>" + LINE_BREAK).getBytes());
fos.write(Bytes());
// <select>
fos.write((" <select id=\"selectByPrimaryKey\" resultMap=\"BaseResultMap\" parameterType=\"java.lang.String\">" + LINE_BREAK).getBytes());
fos.write((" select <include refid=\"" + resultMapId + "\" />" + LINE_BREAK).getBytes());
fos.write((" from " + TableName() + LINE_BREAK).getBytes());
fos.write((" where " + PrimaryKeyDB() + " = #{" + PrimaryKeyJava() + ",jdbcType=" + PrimaryKeyDataType().toU fos.write((" </sql>" + LINE_BREAK).getBytes());
fos.write(Bytes());
} catch (IOException e) {
System.out.println("genSelect IOException " + e);
}
}
/**
* ⽣成insert语句
* @param fos
* @param tableInfo
*/
private void genInsert(FileOutputStream fos, TableInfo tableInfo){
String tableName = TableName();
String primaryKeyDB = PrimaryKeyDB();
List<FieldInfo> list = FieldList();
StringBuffer fieldListDBStr = new StringBuffer();
StringBuffer fieldListJavaStr = new StringBuffer();
for (FieldInfo fieldInfo : list) {
fieldListDBStr.FieldNameDB()).append(",");
fieldListJavaStr.append("#{").FieldNameJava()).append("},");
}
String fieldListDBStrFinal = String().substring(0, String().length() - 1);
String fieldListJavaStrFinal = String().substring(0, String().length() - 1);
try {
fos.write((" <insert id=\"insert\" parameterType=\"del.StoreModel\">" + LINE_BREAK).getBytes());
fos.write((" insert into " + tableName + " (" + fieldListDBStrFinal + ")" + LINE_BREAK).getBytes())
;
fos.write((" select " + " " + fieldListJavaStrFinal + LINE_BREAK).getBytes());
fos.write((" from dual" + LINE_BREAK).getBytes());
fos.write((" where not exists(select " + primaryKeyDB + " from " + tableName + " where " + primaryKeyDB + " = #{" + PrimaryKeyJava() + fos.write(("\t</insert>" + LINE_BREAK).getBytes());
fos.write(Bytes());
} catch (IOException e) {
System.out.println("genInsert IOException " + e);
}
}
/**
* ⽣成update语句
* 注意:update语句,需要执⾏jdbcType
* @param fos
* @param tableInfo
*/
private void genUpdate(FileOutputStream fos, TableInfo tableInfo){
String tableName = TableName();
String primaryKeyDB = PrimaryKeyDB();
List<FieldInfo> list = FieldList();
try {
fos.write((" <update id=\"updateByPrimaryKey\" parameterType=\"del.StoreActivityModel\">" + LINE_BREAK).getBytes());
fos.write((" update " + tableName + LINE_BREAK).getBytes());
fos.write((" <set>" + LINE_BREAK).getBytes());
for (FieldInfo fieldInfo : list) {
fos.write((" <if test=\"" + FieldNameJava() + " != null\">" + LINE_BREAK).getBytes());
fos.write((" " + FieldNameDB() + " = #{" + FieldNameJava() + ",jdbcType=" + DataTypeMysql().toUpperCas fos.write((" </if>" + LINE_BREAK).getBytes());
}
fos.write((" </set>" + LINE_BREAK).getBytes());
fos.write((" where " + primaryKeyDB + " = #{" + PrimaryKeyJava() + ",jdbcType=" + PrimaryKeyDataType().toUpperCase() + " fos.write((" </update>" + LINE_BREAK).getBytes());
fos.write(Bytes());
} catch (IOException e) {
System.out.println("genUpdate IOException " + e);
}
}
/**
* ⽣成delete语句
* @param fos
* @param tableInfo
*/
private void genDelete(FileOutputStream fos, TableInfo tableInfo){
try {
fos.write((" <delete id=\"deleteByPrimaryKey\" parameterType=\"String\">" + LINE_BREAK).getBytes());
fos.write((" delete from " + TableName() + " where " + PrimaryKeyDB() + " = #{" + PrimaryKeyJava() + "}" + LINE
fos.write((" delete from " + TableName() + " where " + PrimaryKeyDB() + " = #{" + PrimaryKeyJava() + "}" fos.write((" </delete>" + LINE_BREAK).getBytes());
fos.write(Bytes());
} catch (IOException e) {mysql中delete语句
System.out.println("genDelete IOException " + e);
}
}
}
⼆,建表语句⽂件解析类:ParseSqlFile.java
package cn.sephora.product.elasticsearch.service.impl;
import org.apachemons.lang.StringUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @description 解析建表sql⽂件,⽣成对应的封装对象TableInfo
* @date 2019/12/13
* @address shanghai
*
*/
public class ParseSqlFile {
/**
* Mysql建表语句关键字
*/
private final static Map<String, String> MYSQL_KEY_WORD_MAP = new HashMap();
static {
MYSQL_KEY_WORD_MAP.put("ENGINE", "ENGINE");
MYSQL_KEY_WORD_MAP.put("CHARACTER", "CHARACTER");
MYSQL_KEY_WORD_MAP.put("COLLATE", "COLLATE");
}
/**
* 解析sql建表语句⽂件,得到FieldInfo集合
* @param fileObj
* @return
*/
public TableInfo getFieldInfo(File fileObj) {
BufferedReader reader = null;
InputStream in = null;
TableInfo tableInfo = new TableInfo();
List<FieldInfo> list = new ArrayList<>();
List<FieldInfo> listParsed = null;
try {
reader = new BufferedReader(new FileReader(fileObj));
in = new FileInputStream(fileObj);
String line = null;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论