
正文
java代码生成器多表 java 代码生成器 开源
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java代码生成器怎么用
zip包,然后自动下载下来
1.预先定义好模板
2.界面输入相关参数
3.解析模板生成代码并下载
最后放出源代码:
package com.et.controller.system.createcode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.et.controller.base.BaseController;
import com.et.util.DelAllFile;
import com.et.util.FileDownload;
import com.et.util.FileZip;
import com.et.util.Freemarker;
import com.et.util.PageData;
import com.et.util.PathUtil;
/**
* 类名称:FreemarkerController
* 创建人:Harries
* 创建时间:2015年1月12日
* @version
*/
@Controller
@RequestMapping(value=”/createCode”)
public class CreateCodeController extends BaseController {
/**
* 生成代码
*/
@RequestMapping(value=”/proCode”)
public void proCode(HttpServletResponse response) throws Exception{
PageData pd = new PageData();
pd = this.getPageData();
/* ============================================================================================= */
String packageName = pd.getString(“packageName”); //包名 ========1
String objectName = pd.getString(“objectName”); //类名 ========2
String tabletop = pd.getString(“tabletop”); //表前缀 ========3
tabletop = null == tabletop?””:tabletop.toUpperCase(); //表前缀转大写
String zindext = pd.getString(“zindex”); //属性总数
int zindex = 0;
if(null != zindext !””.equals(zindext)){
zindex = Integer.parseInt(zindext);
}
ListString[] fieldList = new ArrayListString[](); //属性集合 ========4
for(int i=0; i zindex; i++){
fieldList.add(pd.getString(“field”+i).split(“,fh,”)); //属性放到集合里面
}
MapString,Object root = new HashMapString,Object(); //创建数据模型
root.put(“fieldList”, fieldList);
root.put(“packageName”, packageName); //包名
root.put(“objectName”, objectName); //类名
root.put(“objectNameLower”, objectName.toLowerCase()); //类名(全小写)
root.put(“objectNameUpper”, objectName.toUpperCase()); //类名(全大写)
root.put(“tabletop”, tabletop); //表前缀
root.put(“nowDate”, new Date()); //当前日期
DelAllFile.delFolder(PathUtil.getClasspath()+”admin/ftl”); //生成代码前,先清空之前生成的代码
/* ============================================================================================= */
String filePath = “admin/ftl/code/”; //存放路径
String ftlPath = “createCode”; //ftl路径
/*生成controller*/
Freemarker.printFile(“controllerTemplate.ftl”, root, “controller/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Controller.java”, filePath, ftlPath);
/*生成service*/
Freemarker.printFile(“serviceTemplate.ftl”, root, “service/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Service.java”, filePath, ftlPath);
/*生成mybatis xml*/
Freemarker.printFile(“mapperMysqlTemplate.ftl”, root, “mybatis_mysql/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);
Freemarker.printFile(“mapperOracleTemplate.ftl”, root, “mybatis_oracle/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);
/*生成SQL脚本*/
Freemarker.printFile(“mysql_SQL_Template.ftl”, root, “mysql数据库脚本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);
Freemarker.printFile(“oracle_SQL_Template.ftl”, root, “oracle数据库脚本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);
/*生成jsp页面*/
Freemarker.printFile(“jsp_list_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_list.jsp”, filePath, ftlPath);
Freemarker.printFile(“jsp_edit_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_edit.jsp”, filePath, ftlPath);
/*生成说明文档*/
Freemarker.printFile(“docTemplate.ftl”, root, “说明.doc”, filePath, ftlPath);
//this.print(“oracle_SQL_Template.ftl”, root); 控制台打印
/*生成的全部代码压缩成zip文件*/
FileZip.zip(PathUtil.getClasspath()+”admin/ftl/code”, PathUtil.getClasspath()+”admin/ftl/code.zip”);
/*下载代码*/
FileDownload.fileDownload(response, PathUtil.getClasspath()+”admin/ftl/code.zip”, “code.zip”);
}
}
相关问答
Q1: java项目开发在多表情况下的DAO设计问题
package com.xxx.web.dao.base;
import java.io.Serializable;
import java.util.LinkedHashMap;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.xxx.bean.QueryResult;
import com.xxx.utils.GenericsUtils;
/**
* Dao公共类
* @author Administrator
* 本类主要是各个dao实现类的父类,以后写dao实现类时只要继承此类即可
*/
@SuppressWarnings("unchecked")
@Transactional
public abstract class DaoSupportT implements DaoT{
protected ClassT entityClass = GenericsUtils.getGenericsType(this.getClass());
@Resource protected SessionFactory sessionFactory;//注入SessionFactory实例
/**
* 插入一个实体
* @param o
*/
public void createObject(Object o){
sessionFactory.getCurrentSession().save(o);
}
/**
* 删除一个实体
* @param o
*/
public void deleteObject(Object o){
sessionFactory.getCurrentSession().delete(o);
}
/**
* 更新一个实体
* @param o
*/
public void updateObject(Object o){
sessionFactory.getCurrentSession().update(o);
}
/**
* 查询一个实体
* @param o
*/
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public T searchObject(Serializable id){
if(id == null) throw new RuntimeException(this.entityClass.getName()+ ":传入的实体id不能为空");
return (T)sessionFactory.getCurrentSession().get(this.entityClass, id);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public long getCount() {
return (Long)sessionFactory.getCurrentSession().createQuery("select count(o) from "+ (this.entityClass.getSimpleName())+ " o").uniqueResult();
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResultT getScrollData(int firstindex, int maxresult, LinkedHashMapString, String orderby) {
return getScrollData(firstindex,maxresult,null,null,orderby);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResultT getScrollData(int firstindex, long maxresult, String wherejpql, Object[] queryParams) {
return getScrollData(firstindex,(int) maxresult,wherejpql,queryParams,null);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResultT getScrollData(int firstindex, int maxresult) {
return getScrollData(firstindex,maxresult,null,null,null);
}
/**
* 查询实体,并返回一个结果集
* @param firstIndex 前置索引
* @param maxNum 要查询的数量
* @param wherehql hql语句
* @param params 参数
* @return QueryResultT结果集
*
*
* 如果想取出前20条,并以倒序,则hibernate以最后一个为1,
* 比如,数据库中有20条数据,则你取出后10条,并以倒序显示出来,则它的逻辑是20,19,18,.....11,懂了吧,
* 相应的查询语句为
* getScrollData(int firstIndex, int maxNum,orderby)
*
*
*
*/
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResultT getScrollData(int firstIndex, int maxNum, String wherehql, Object[] params, LinkedHashMapString, String orderby ){
QueryResultT qr = new QueryResultT();
Session session = sessionFactory.openSession();
Query query = session.createQuery(" select o from "+this.entityClass.getSimpleName()+" o " +(wherehql==null ||"".equals(wherehql.trim())?" ":" where "+wherehql)+ buildOrderby(orderby));
if(firstIndex!=-1 maxNum!=-1)
{
query.setFirstResult(firstIndex-1).setMaxResults(maxNum);
}
setParams(query, params);
qr.setResultList(query.list());
query = session.createQuery(" select count(o) from "+this.entityClass.getSimpleName()+" o " +(wherehql==null ||"".equals(wherehql.trim())?" ":" where "+wherehql));
setParams(query, params);
qr.setTotalRecord((Long)query.uniqueResult());
session.close();
return qr;
}
给分,你的分太少了
Q2: java代码生成器用途
主要功能: 你只要设计好数据库 就可以生成java vo
java DAO jsp
servlet
struts-config配置信息
oracle 建表语句 查询语句 等
可生成java struts 架构的完整的源码 包括 增加 删除 修改 查询等功能的源码
但
1.不同的架构,需要不同的生成器
2.生成器一般需要模板技术,如freeMarker、velocity等
3.生成器也是Java项目,可以自己修改、设计、开发
4.生成器能节省一定的工作量
学这个? 网上都有现成的软件,会用就行,使用很简单。 如果是想学开发一个代码生成器的话,我觉得没必要啊。
你把JAVA 语言学会了,真正在开发的时候自然会遇见这个软件,自然就会了
Q3: 怎样用java代码动态生成数据库表
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("数据库url","帐号","密码");
state=conn.createStatement();
state.executeUpdate("create 建表语句");
state.executeUpdate("insert 插入数据")------插入java代码生成器多表的值由页面获得java代码生成器多表,注意字符串拼接。
然后就是关闭连接java代码生成器多表,state.close();conn.close();
核心代码就是这些,具体应用你可以多写几个方法(增删改查),都是类似的,注意异常的处理,关闭连接最好在finally中进行。
Q4: java中如何实现多表查询?
多表查询是属于数据库的知识java代码生成器多表, 按照你说的使用java进行多表查询那就要使用Hibernatejava代码生成器多表,此ORM框架将数据库的关系映射成了java代码的形式。
通过配置映射文件(*.hbm.xml) 设置好关联关系就可以了。也不知道你具体的表结构是什么样子的。
另外java代码生成器多表:比较简单的 你直接用sql代码的左右连接也可以实现多表查询java代码生成器多表, 甚至如果你基础差点,可以分成几句sql语句, 逐步完成查找。 也不清楚你的表结构,具体代码就不给出了。
java代码生成器多表的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 代码生成器 开源、java代码生成器多表的信息别忘了在本站进行查找喔。








