<!DOCTYPE html>
<html>
<script>
window.onload=function(){
}
//执行转换
function transform(){
//判断源字符串是否为空
var sourceInput=document.getElementById("sourceInput").value.trim();
if(sourceInput==null || sourceInput==''){
window.alert("源字符串不能为空");
return;
}
//判断是哪种转换
var selectionVal=document.getElementById("selection").value.trim();
if("01"==selectionVal){
oracleFieldToHumping(sourceInput);
}else if("02"==selectionVal){
humpToOracleField(sourceInput);
}else if("03"==selectionVal){
jsonFormat(sourceInput);
}else if("04"==selectionVal){
oracleFieldConvertBo(sourceInput);
}else if("05"==selectionVal){
//显示输入业务表名标签
document.getElementById("tableId").style.display="";
var tableName=document.getElementById("tableId").value.trim();
oracleFieldConvertMyBatisInsertSQL(sourceInput,tableName);
}else if("06"==selectionVal){
//显示输入业务表名标签
document.getElementById("tableId").style.display="";
var tableName=document.getElementById("tableId").value.trim();
oracleFieldConvertMyBatisDeleteSQL(sourceInput,tableName);
}else if("07"==selectionVal){
//显示输入业务表名标签
document.getElementById("tableId").style.display="";
var tableName=document.getElementById("tableId").value.trim();
oracleFieldConvertMyBatisUpdateSQL(sourceInput,tableName);
}else if("08"==selectionVal){
//显示输入业务表名标签
document.getElementById("tableId").style.display="";
var tableName=document.getElementById("tableId").value.trim();
oracleFieldConvertMyBatisSelectSQL(sourceInput,tableName);
}
}
//PLSQL复制字段转MyBatis-select语句
function oracleFieldConvertMyBatisSelectSQL(sourceInput,tableName){
if(typeof(tableName) == undefined || tableName=="")
tableName="tableName";//默认值
var fieldArr=plsqlCopyInfoProcess(sourceInput);
var selectSql=[];
var whereSql=[];
fieldArr.forEach((item,index,array)=>{
var oracleFieldName=item.fieldName;
var fieldJdbcType=oracleTypeConvertJdbcType(item.fileType);
var javaField=fieldToHumpingMethod(item.fieldName);
var selectSqlItem="\t"+oracleFieldName+"\tas\t\""+javaField+"\"";
var whereSqlItem="\t < if test=\""+oracleFieldName+" !=null\">\n\t "
+"and\t"+oracleFieldName+" = "+"#{"+javaField+",jdbcType="+fieldJdbcType+"}\n"
+"\t </if>\n";
selectSql.push(selectSqlItem);
whereSql.push(whereSqlItem);
});
var selectResultSql="select\n"
+selectSql.join(",\n")
+"\nfrom\t"+tableName+"\n"
+"where 1=1\n"
+whereSql.join(" ");
document.getElementById("transformOutput").value=selectResultSql;
}
//PLSQL复制字段转MyBatis-update语句
/**
update k_id_staff_expert
<set>
<if test="orgNo != null">
ORG_NO = #{orgNo,jdbcType=VARCHAR},
</if>
<if test="staffNo != null">
STAFF_NO = #{staffNo,jdbcType=VARCHAR},
</if>
<if test="expertRange != null">
EXPERT_RANGE = #{expertRange,jdbcType=VARCHAR},
</if>
<if test="serviceTimes != null">
SERVICE_TIMES = #{serviceTimes,jdbcType=INTEGER},
</if>
</set>
where INFO_ID = #{infoId,jdbcType=BIGINT}
*/
function oracleFieldConvertMyBatisUpdateSQL(sourceInput,tableName){
if(typeof(tableName) == undefined || tableName=="")
tableName="tableName";//默认值
var fieldArr=plsqlCopyInfoProcess(sourceInput);
var updateFieldArr=new Array();//values 驼峰转化后具体的
fieldArr.forEach((item,index,array)=>{
var oracleFieldName=item.fieldName;
var fieldJdbcType=oracleTypeConvertJdbcType(item.fileType);
var javaField=fieldToHumpingMethod(item.fieldName);
var combVal="\t < if test=\""+oracleFieldName+" !=null\">\n\t "
+oracleFieldName+" = "+"#{"+javaField+",jdbcType="+fieldJdbcType+"},\n"
+"\t </if>\n";
updateFieldArr.push(combVal);
});
var primaryKeyName=fieldArr[0].fieldName;
var fieldJdbcType=oracleTypeConvertJdbcType(fieldArr[0].fileType);
var javaField=fieldToHumpingMethod(fieldArr[0].fieldName);
var combVal="#{"+javaField+",jdbcType="+fieldJdbcType+"}";
var updateSql="update "+tableName+"\n\t"
+"<set>"+"\n"
+updateFieldArr.join(" ")
+"\t</set>\n\t"
+"where "+primaryKeyName+" = "+combVal;
document.getElementById("transformOutput").value=updateSql;
}
//PLSQL复制字段转MyBatis-delete语句
/**
* delete from k_affair_notice
where notice_id = #{noticeId,jdbcType=BIGINT}
*/
function oracleFieldConvertMyBatisDeleteSQL(sourceInput,tableName){
if(typeof(tableName) == undefined || tableName=="")
tableName="tableName";//默认值
var fieldArr=plsqlCopyInfoProcess(sourceInput);
var primaryKeyName=fieldArr[0].fieldName;
var fieldJdbcType=oracleTypeConvertJdbcType(fieldArr[0].fileType);
var javaField=fieldToHumpingMethod(fieldArr[0].fieldName);
var combVal="#{"+javaField+",jdbcType="+fieldJdbcType+"}";
var deleteSql="delete from "+tableName+"\n"
+"where "+primaryKeyName+" = "+combVal;
document.getElementById("transformOutput").value=deleteSql;
}
//PLSQL复制字段转MyBatis-insert语句
/**
* insert into k_affair_notice (ORG_NO, NOTICE_NAME,
NOTICE_DESC, ATTRACH_ID, NOTICE_STATUS,
CONSULT_TIMES, CREATE_TIME, RELEASE_TIME
)
values (#{orgNo,jdbcType=VARCHAR}, #{noticeName,jdbcType=VARCHAR},
#{noticeDesc,jdbcType=VARCHAR}, #{attrachId,jdbcType=DECIMAL}, #{noticeStatus,jdbcType=VARCHAR},
#{consultTimes,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{releaseTime,jdbcType=TIMESTAMP}
)
*/
function oracleFieldConvertMyBatisInsertSQL(sourceInput,tableName){
if(typeof(tableName) == undefined || tableName=="")
tableName="tableName";//默认值
var fieldArr=plsqlCopyInfoProcess(sourceInput);
var oracleFieldArr=new Array();//insert 插入表具体的oracle字段名称
var jdbcFieldArr=new Array();//values 驼峰转化后具体的
fieldArr.forEach((item,index,array)=>{
oracleFieldArr.push(item.fieldName);
var fieldJdbcType=oracleTypeConvertJdbcType(item.fileType);
var javaField=fieldToHumpingMethod(item.fieldName);
var combVal="#{"+javaField+",jdbcType="+fieldJdbcType+"}";
jdbcFieldArr.push(combVal);
});
var insertSql="insert into "+tableName+"(\n\t"
+oracleFieldArr.join(",\n\t")
+"\n)"
+"values (\n\t"
+jdbcFieldArr.join(",\n\t")
+"\n)"
document.getElementById("transformOutput").value=insertSql;
}
//oracle 字段类型转jdbc类型
function oracleTypeConvertJdbcType(oracleType){
if(oracleType.toUpperCase().startsWith("CHAR")){
return "CHAR";
}else if(oracleType.toUpperCase().startsWith("VARCHAR2")){
return "VARCHAR";
}else if(oracleType.toUpperCase().startsWith("LONG")){
return "LONGVARCHAR";
}else if(oracleType.toUpperCase().startsWith("NUMBER")){
return "NUMERIC";
}else if(oracleType.toUpperCase().startsWith("RAW")){
return "BINARY";
}else if(oracleType.toUpperCase().startsWith("LONGRAW")){
return "LONGVARBINARY";
}else if(oracleType.toUpperCase().startsWith("DATE")){
return "DATE";
}else if(oracleType.toUpperCase().startsWith("TIMESTAMP")){
return "TIMESTAMP";
}else if(oracleType.toUpperCase().startsWith("BLOB")){
return "BLOB";
}else if(oracleType.toUpperCase().startsWith("CLOB")){
return "CLOB";
}else{
return oracleType;
}
}
//oracle 字段类型转java类型
function oracleTypeConvertJavaType(oracleType){
if(oracleType.toUpperCase().startsWith("CHAR")){
return "String";
}else if(oracleType.toUpperCase().startsWith("VARCHAR2")){
return "String";
}else if(oracleType.toUpperCase().startsWith("LONG")){
return "String";
}else if(oracleType.toUpperCase().startsWith("NUMBER")){
return "Integer";
}else if(oracleType.toUpperCase().startsWith("RAW")){
return "byte[]";
}else if(oracleType.toUpperCase().startsWith("LONGRAW")){
return "byte[]";
}else if(oracleType.toUpperCase().startsWith("DATE")){
return "Date";
}else if(oracleType.toUpperCase().startsWith("TIMESTAMP")){
return "Timestamp";
}else if(oracleType.toUpperCase().startsWith("BLOB")){
return "Blob";
}else if(oracleType.toUpperCase().startsWith("CLOB")){
return "Clob";
}else{
return oracleType;
}
}
//字段集合转标准格式化Bo
function oracleFieldConvertBo(sourceInput){
var fieldArr=plsqlCopyInfoProcess(sourceInput);
var javaFieldArr=new Array();
fieldArr.forEach((item,index,array)=>{
var fieldName=fieldToHumpingMethod(item.fieldName);
var fieldType=oracleTypeConvertJavaType(item.fileType);
var javaField="/**\n"
+"*\t "+item.fileMemo+"\n"
+"*/\n"
+"private "+fieldType+" "+fieldName+";\n";
javaFieldArr.push(javaField);
});
var javaAllFieldStr=javaFieldArr.join("\n");
document.getElementById("transformOutput").value=javaAllFieldStr;
}
//PLSQLcopy表信息解析
/**
* {
fieldName:
fileType:
fileMemo:
}
*/
function plsqlCopyInfoProcess(sourceInput){
var re=/([\w\W]+?)(?=(?:\n(?:(?:[a-zA-Z]+\d?)|(?:[a-zA-Z]+(?:[_][\w]+)+)))|(?=$))/g;
var fieldArr = [];//存储字段属性信息
var matchResult=re.exec(sourceInput);
while (matchResult) {
fieldArr.push(matchResult[0]);
matchResult = re.exec(sourceInput);
}
var sqlFieldArr=new Array();
//1 没有返回值
fieldArr.forEach((item,index,array)=>{
var re=/(?<=(?:^\n)|(?:^))(?=\w+)(\w+)\t+([\w\d\(\)\s]+?)\t+y?\t+([\w\W]+)/gm;
var matchResult=re.exec(item);
//将备注换行符转空格,调整排版
if(matchResult[3]!=null){
var rep=/(?=\n)(\n)/gm;
var formatStr=matchResult[3].replace(rep,function(keyWord){
return " ";
});
matchResult[3]=formatStr;
}
if(matchResult!=null){
//执行代码
sqlFieldArr.push({
"fieldName":matchResult[1],
"fileType":matchResult[2],
"fileMemo":matchResult[3]
});
}
});
return sqlFieldArr;
}
//oracle字段->驼峰格式
/**
* --字段转驼峰 (?=_)(\w{2})
* --驼峰转字段 ([A-Z])
* --表属性规则([a-z_],并且不以_开头、_结尾,不会出现连续的__)
* --驼峰规则([a-zA-Z],并且不以大写字母开头)
*/
function oracleFieldToHumping(sourceInput){
//单行模式,判断是否符合表属性规则
var re=/(?!.*[_]{2,})(?!.*^_)(?!.*[_]$)^[a-z_]+$/gm;
if(!re.test(sourceInput)){
window.alert("源字符串格式错误!");
return;
}
var result=fieldToHumpingMethod(sourceInput)
document.getElementById("transformOutput").value=result;
}
//格式化带下划线字段类型转驼峰
function fieldToHumpingMethod(sourceInput){
//不关注单行、多行模式,捕获符合要求的字符串,进行格式化
var re=/(?!.*[_]{2,})(?!.*^_)(?!.*[_]$)^([a-z_]+)$/gm;
var matchResult=re.exec(sourceInput);
var ary = [];//存储符合匹配模式的值
while (matchResult) {
ary.push(matchResult[0]);
matchResult = re.exec(sourceInput);
}
//匹配符合字符行并格式化返回
var result=sourceInput.replace(re, function(word){
var rep=/(_\w)/g;
var upperCasize=word.replace(rep,function(keyWord){
return keyWord[1].toUpperCase();
});
return upperCasize;
});
return result;
}
//驼峰转oracle字段格式
function humpToOracleField(sourceInput){
//单行模式,判断是否符合驼峰规则
var re=/(?!.*^[A-Z])^[a-zA-Z]+$/gm;
if(!re.test(sourceInput)){
window.alert("源字符串格式错误!");
return;
}
//不关注单行、多行模式,捕获符合要求的字符串,进行格式化
re=/(?!.*^[A-Z])^([a-zA-Z]+)$/gm;
var matchResult=re.exec(sourceInput);
var ary = [];//存储符合匹配模式的值
while (matchResult) {
ary.push(matchResult[0]);
matchResult = re.exec(sourceInput);
}
//匹配符合字符行并格式化返回
var result=sourceInput.replace(re, function(word){
var rep=/([A-Z])/g;
var lowerCasize=word.replace(rep,function(keyWord){
return "_"+keyWord[0].toLowerCase();
});
return lowerCasize;
});
document.getElementById("transformOutput").value=result;
}
/**
* json字符格式化显示
*
*/
function jsonFormat(sourceInput){
try{
var jsonObj=JSON.parse(sourceInput);
var jsonResult=JSON.stringify(jsonObj);
document.getElementById("transformOutput").value=jsonResult;
}catch(err){
window.alert("源字符串JSON格式错误!");
}
}
</script>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body onchange="">
<fieldset>
<legend>字符串格式化</legend>
<textarea id="sourceInput"name="sourceStr"
cols="50" rows="30" style="color:skyblue;"
placeholder="源字符串"></textarea>
<select id="selection" onchange="transform()">
<option value ="01">oracle字段(仅字段)->驼峰格式</option>
<option value ="02">驼峰字段->oracle字段格式</option>
<option value ="03">json格式化</option>
<option value ="04">PLSQL复制字段->Bo实体类</option>
<option value ="05">PLSQL复制字段->MyBatis-insert语句</option>
<option value ="06">PLSQL复制字段->MyBatis-delete语句</option>
<option value ="07">PLSQL复制字段->MyBatis-update语句</option>
<option value ="08">PLSQL复制字段->MyBatis-select语句</option>
</select>
<input id="tableId" type="text" style="width: 5%;color:red;display:none;border: 1px solid #FF0000;" placeholder="tableName"/>
<input type="button" value="transform" onclick="transform()"/>
<textarea id="transformOutput" name="outputStr"
cols="50"rows="30" style="color:red;"
placeholder="目标值"></textarea>
</fieldset>
</body>
</html>
正则表达式仿Mybatis生成代码
于 2022-05-23 09:38:13 首次发布
本文提供了一个工具,能将PL/SQL复制的字段快速转化为MyBatis的insert、update、delete和select语句,同时支持驼峰命名转Oracle字段格式和Bo实体类定义,简化开发过程。
摘要由CSDN通过智能技术生成