java如何根据实体类图生成sql脚本?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java如何根据实体类图生成sql脚本?相关的知识,希望对你有一定的参考价值。
就是数据库建模工具中的导出sql脚本这个功能是怎么实现的
自动生成C#和Java实体类,自动生连接符为“&”和“+”的INSERT,UPDATE,DELETE,SELECT语句,支持简单的数据查询修改。 参考技术A 你说的是?mybatis generator?如何利用SQL中数据使用FreeMarker生成JAVA实体bean代码
参考技术A 利用freemarker生成JAVA BEANFreemarker模板代码如下:
package $packageName;
/**
* <#if author == "adams"> @author adams </#if>
*/
pulic class $className
<#list attrs as a>
private $a.type $a.field;
</#list>
<#list attrs as a>
public void set$a.field?cap_first($a.type $a.field)
this.$a.field = $a.field;
public $a.type get$a.field?cap_first()
return this.$a.field;
</#list>
Java代码如下
package com.my.learn.freemarker;
public class Attr
public String field;
public String type;
public Attr(String field, String type)
this.field = field;
this.type = type;
public String getField()
return this.field;
public String getType()
return this.type;
public void setField(String field)
this.field = field;
public void setType(String type)
this.type = type;
package com.my.learn.freemarker;
import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;
public class FmAppUseage
public static void main(String[] args)
List<Object> list = new ArrayList<Object>();
list.add(new Attr("username", "String"));
list.add(new Attr("password", "String"));
list.add(new Attr("age", "int"));
list.add(new Attr("hobby", "String"));
Map<String,Object> root = new HashMap<String, Object>();
root.put("packageName", "com.my.learn.freemarker");
root.put("className", "User");
root.put("attrs", list);
root.put("author", "adams");
Configuration cfg = new Configuration();
String path = FmAppUseage.class.getResource("/").getPath()+"template";
try
cfg.setDirectoryForTemplateLoading(new File(path));
Template template = cfg.getTemplate("/demo.ftl");
StringWriter out = new StringWriter();
template.process(root, out);
System.out.println(out.toString());
catch (IOException e)
System.out.println("Cause==>" + e.getCause());
catch (TemplateException e)
System.out.println("Cause==>" + e.getCause());
输出结果如下:
package com.my.learn.freemarker;
/**
* @author adams
*/
pulic class User
private String username;
private String password;
private int age;
private String hobby;
public void setUsername(String username)
this.username = username;
public String getUsername()
return this.username;
public void setPassword(String password)
this.password = password;
public String getPassword()
return this.password;
public void setAge(int age)
this.age = age;
public int getAge()
return this.age;
public void setHobby(String hobby)
this.hobby = hobby;
public String getHobby()
return this.hobby;
当在笔者刚做测试时,将Attr的类定义在了FmAppUseage类的内部,导致不能正常运行,只能将其移除单独成一个类时,便能正常运行了。 转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦本回答被提问者和网友采纳
以上是关于java如何根据实体类图生成sql脚本?的主要内容,如果未能解决你的问题,请参考以下文章
CodeSmith生成SQL Server视图的实体类脚本/对应的生成模板