在做ssh项目多表查询时,每次都要写vo类的mapping.xml文件(po的mapping文件可以使用反向工程来实现),为了更方便书写代码,所以使用js写了生成mapping文件的方法。
先看下效果图:
使用方式:在红色框中写入(直接复制写好的vo就好了),需要生成的字段
代码部分:
<body>
<div style="overflow:hidden;">
<h1 style="text-align:center">自动生成hbm.xml文件源码</h1>
<div style="margin:0px auto;width:1500px;">
@* 左 *@
<div style="width:650px;height:650px;display:inline-block;border:1px #ff0000 solid ">
<textarea id="count" style="width:640px;height:640px;overflow:hidden;"></textarea>
</div>
@*中*@
<div style="width:100px;height:650px;display:inline-block;overflow:hidden;">
<button style="width:98px;"onclick="ooo()">生成源码</button>
<span>数据库名称</span><input value="dfzx" id="Database" style="width:90px;margin-top:10px;"/>
<span>类名</span><input id="className" value="com.wyc.vo" style="width:90px;margin-top:10px;"/>
</div>
@*右*@
<div style="width:650px;height:650px;display:inline-block;border:1px #ffd800 solid;overflow:hidden;"><textarea id="hbmxml" style="width:640px;height:640px;color:#000000"></textarea></div>
</div>
</div>
<script src="~/Content/bootstrap-3.3.7-dist/js/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
//首字母大写
var reg = /\b(\w)|\s(\w)/g;
function replaceReg(reg, str) {
//str = str.toLowerCase();
return str.replace(reg, function (m) { return m.toUpperCase() })
}
function ooo() {
var tab2 = "\t" + "\t";
var tab3 = "\t" + "\t" + "\t";
//更换数据类型
var oos = dateType()
//分割每个字段
var kkk = oos.split(";");
//获取前半段语句
var sql = first();
//拼接语句
for(var i=0 ;i<kkk.length-1;i++){
//分割
var property = kkk[i].split(" ");
//首字母大写
var initial = replaceReg(reg, property[2]);
if (i == 0) {
sql += tab2 + "<id name=\"" + property[2] + "\" type=\"" + property[1] + "\">" + "\n";
sql += tab3 + "<column name=\"" + initial + "\" />" + "\n";
sql += tab3 + "<generator class=\"native\"/>" + "\n"
sql += tab2 + "</id>" + "\n";
} else {
sql += tab2 + "<property name=\"" + property[2] + "\" type=\"" + property[1] + "\">" + "\n";
sql += tab3 + "<column name=\"" + initial + "\" />" + "\n";
sql += tab2 + " </property>" + "\n";
}
}
sql += "\t" + " </class>"+"\n";
sql += "</hibernate-mapping>";
//给文本框赋值
$("#hbmxml").val(sql);
}
//前半部分
function first() {
var sql = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "\n" +
"<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"" + "\n" +
"\"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">" + "\n" +
" <!-- " + "\n" +
"\t" +"Mapping file autogenerated by MyEclipse Persistence Tools" + "\n" +
" -->" + "\n" +
" <hibernate-mapping>" +"\n"
+ "\t" + " <class name=\"" + $("#className").val()+"\" catalog=\"" + $("#Database").val() + "\">" + "\n";
return sql;
}
//数据类型转换
function dateType() {
var zzz = $("#count").val();
//获取数据类型
var int = new RegExp("int", "g");
var Integer = new RegExp("Integer", "g");
var Short = new RegExp("Short", "g");
var long = new RegExp("long", "g");
var double = new RegExp("double", "g");
var Timestamp = new RegExp("Timestamp", "g");
var boolean = new RegExp("boolean", "g");
var string = new RegExp("String", "g");
var date = new RegExp("Date", "g");
//替换数据类型
var oos = zzz.replace(Integer, "java.lang.Integer");
oos = oos.replace(int, "java.lang.Integer");
oos = oos.replace(long, "java.lang.Long");
oos = oos.replace(string, "java.lang.String");
oos = oos.replace(Short, "java.lang.Short");
oos = oos.replace(double, "java.lang.Double");
oos = oos.replace(Timestamp, "java.sql.Timestamp");
oos = oos.replace(boolean, "java.lang.Boolean");
oos = oos.replace(date, "java.util.Date");
return oos
}
</script>
</body>