代码自动生成工具_java版
项目结构:
这里要实现的功能是,当我们给出了bean,如:Admin,User,People等实体类后,
我想用代码自动生成我想要的代码,最后生成的效果:
也就是说为每一个bean都生成相应的Dao,DaoImpl,Service,ServiceImpl等类。
后台运行效果:
下面是列出自动生成User的相关文件:
UseDao
、
UserDaoImpl
UserService
UserServiceImpl
=================================================
代码部分:
=================================================
/UUUU_Web_Test/src/com/b510/base/bean/install/BeanUtils.java
1 /**
2 *
3 */
4 package com.b510.base.bean.install;
5
6 import java.io.File;
7 import java.io.FileWriter;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10
11
12 /**
13 * @author hongten(hongtenzone@foxmail.com)
14 * @date 2013-2-24
15 */
16 @SuppressWarnings("unchecked")
17 public class BeanUtils {
18
19
20
21 //公共部分
22 private static final String RT_1 = "\r\n";
23 private static final String RT_2 = RT_1+RT_1;
24 private static final String BLANK_1 =" ";
25 private static final String BLANK_4 =" ";
26 private static final String BLANK_8 =BLANK_4 + BLANK_4;
27
28
29
30 //注释部分
31 private static final String ANNOTATION_AUTHOR_PARAMTER = "@author ";
32 private static final String ANNOTATION_AUTHOR_NAME = "hongten(hongtenzone@foxmail.com)";
33 private static final String ANNOTATION_AUTHOR = ANNOTATION_AUTHOR_PARAMTER + ANNOTATION_AUTHOR_NAME;
34 private static final String ANNOTATION_DATE = "@date ";
35 private static final String ANNOTATION = "/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR +RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1;
36
37
38 //文件 地址
39 //private static final String BEAN_PATH = "com/b510/base/bean";
40 private static final String DAO_PATH = "com/b510/base/dao";
41 private static final String DAO_IMPL_PATH = "com/b510/base/dao/impl";
42 private static final String SERVICE_PATH = "com/b510/base/service";
43 private static final String SERVICE_IMPL_PATH = "com/b510/base/service/impl";
44
45
46
47 //包名
48 private static final String BEAN_URL = "com.b510.base.bean";
49 private static final String DAO_URL = "com.b510.base.dao";
50 private static final String DAO_IMPL_URL = "com.b510.base.dao.impl";
51 private static final String SERVICE_URL = "com.b510.base.service";
52 private static final String SERVICE_IMPL_URL = "com.b510.base.service.impl";
53
54 //基本类名称
55 private static final String BASE_DAO_NAME = DAO_URL + ".BaseDao";
56 private static final String ABSTRACT_BASE_DAO_IMPL_NAME = DAO_IMPL_URL + ".AbstractBaseDaoImpl";
57 private static final String BASE_SERVICE_NAME = SERVICE_URL + ".BaseService";
58 private static final String ABSTRACT_BASE_SERVICE_IMPL_NAME = SERVICE_IMPL_URL + ".AbstractBaseServiceImpl";
59
60
61 /**
62 * 创建bean的Dao<br>
63 *
64 * @param c
65 * @throws Exception
66 */
67 public void createBeanDao(Class c) throws Exception {
68 String cName = c.getName();
69 String fileName = System.getProperty("user.dir") + "/src/" + DAO_PATH
70 + "/" + getLastChar(cName) + "Dao.java";
71 File f = new File(fileName);
72 FileWriter fw = new FileWriter(f);
73 fw.write("package "+DAO_URL+";"+RT_2+ANNOTATION+"public interface " +
74 getLastChar(cName) + "Dao extends "+BASE_DAO_NAME+" <" + cName + "> {"+RT_2+"}");
75 fw.flush();
76 fw.close();
77 showInfo(fileName);
78 }
79
80 /**
81 * 创建bean的Dao的实现类
82 * @param c
83 * @throws Exception
84 */
85 public void createBeanDaoImpl(Class c) throws Exception{
86 String cName = c.getName();
87 String fileName = System.getProperty("user.dir") + "/src/" + DAO_IMPL_PATH
88 + "/" + getLastChar(cName) + "DaoImpl.java";
89 File f = new File(fileName);
90 FileWriter fw = new FileWriter(f);
91 fw.write("package "+DAO_IMPL_URL+";"+RT_2+ANNOTATION+"public class " +
92 getLastChar(cName) + "DaoImpl extends "+ABSTRACT_BASE_DAO_IMPL_NAME+"<" +
93 cName + "> implements "+DAO_URL+"."+getLastChar(cName)+"Dao{"+RT_2+"}");
94 fw.flush();
95 fw.close();
96 showInfo(fileName);
97 }
98
99
100
101 /**
102 * 创建bean的service
103 * @param c
104 * @throws Exception
105 */
106 public void createBeanService(Class c) throws Exception{
107 String cName = c.getName();
108 String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_PATH
109 + "/" + getLastChar(cName) + "Service.java";
110 File f = new File(fileName);
111 FileWriter fw = new FileWriter(f);
112 fw.write("package "+SERVICE_URL+";"+RT_2+ANNOTATION+"public interface " +
113 getLastChar(cName) + "Service extends "+BASE_SERVICE_NAME+"<"+ cName +">{"+RT_2+"}");
114 fw.flush();
115 fw.close();
116 showInfo(fileName);
117 }
118
119 /**
120 * 创建bean的service的实现类
121 * @param c
122 * @throws Exception
123 */
124 public void createBeanServiceImpl(Class c) throws Exception{
125 String cName = c.getName();
126 String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_IMPL_PATH
127 + "/" +getLastChar(cName)+"ServiceImpl.java";
128 File f = new File(fileName);
129 FileWriter fw = new FileWriter(f);
130 fw.write("package "+SERVICE_IMPL_URL+";"+RT_2+ANNOTATION+"public class "
131 + getLastChar(cName) + "ServiceImpl extends "+ABSTRACT_BASE_SERVICE_IMPL_NAME+"<"+ cName
132 + "> implements "+SERVICE_URL+"."+getLastChar(cName)+"Service{"+RT_2+BLANK_4
133 +"private "+DAO_URL+"."+getLastChar(cName)+"Dao "+getLowercaseChar(getLastChar(cName))
134 +"Dao;"+RT_2+BLANK_4+"public void set"+getLastChar(cName)+"Dao("+DAO_URL+"."+getLastChar(cName)+"Dao "
135 +getLowercaseChar(getLastChar(cName))+"Dao){"+RT_1+BLANK_8+"this."+getLowercaseChar(getLastChar(cName))+"Dao = "
136 +getLowercaseChar(getLastChar(cName))+"Dao;"+RT_1+BLANK_4+"}"+RT_2+BLANK_4+"@Override"+RT_1+BLANK_4
137 +"public "+DAO_URL+"."+"BaseDao<"+BEAN_URL+"."+getLastChar(cName)+"> getBaseDao(){"+RT_1+BLANK_8
138 +"return "+getLowercaseChar(getLastChar(cName))+"Dao;"+RT_1+BLANK_4+"}"+RT_2+"}");
139 fw.flush();
140 fw.close();
141 showInfo(fileName);
142 }
143
144
145 /**
146 * 获取路径的最后面字符串<br>
147 * 如:<br>
148 * <code>str = "com.b510.base.bean.User"</code><br>
149 * <code> return "User";<code>
150 * @param str
151 * @return
152 */
153 public String getLastChar(String str) {
154 if ((str != null) && (str.length() > 0)) {
155 int dot = str.lastIndexOf('.');
156 if ((dot > -1) && (dot < (str.length() - 1))) {
157 return str.substring(dot + 1);
158 }
159 }
160 return str;
161 }
162
163 /**
164 * 把第一个字母变为小写<br>
165 * 如:<br>
166 * <code>str = "UserDao";</code><br>
167 * <code>return "userDao";</code>
168 * @param str
169 * @return
170 */
171 public String getLowercaseChar(String str){
172 return str.substring(0,1).toLowerCase()+str.substring(1);
173 }
174
175 /**
176 * 显示信息
177 * @param info
178 */
179 public void showInfo(String info){
180 System.out.println("创建文件:"+ info+ "成功!");
181 }
182
183 /**
184 * 获取系统时间
185 * @return
186 */
187 public static String getDate(){
188 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
189 return simpleDateFormat.format(new Date());
190 }
191 }/UUUU_Web_Test/src/com/b510/base/bean/install/BeanUtilTest.java
1 /**
2 *
3 */
4 package com.b510.base.bean.install;
5
6 import com.b510.base.bean.Admin;
7 import com.b510.base.bean.People;
8 import com.b510.base.bean.User;
9
10 /**
11 * @author hongten(hongtenzone@foxmail.com)
12 * @date 2013-2-24
13 */
14 public class BeanUtilTest {
15
16 public static void main(String[] args) throws Exception{
17 BeanUtilTest beanUtilTest = new BeanUtilTest();
18 BeanUtils beanUtils = new BeanUtils();
19 beanUtilTest.beanTool(beanUtils, User.class);
20 beanUtilTest.beanTool(beanUtils, People.class);
21 beanUtilTest.beanTool(beanUtils, Admin.class);
22 }
23
24 /**
25 * 根据bean生成相应的文件
26 * @param beanUtils
27 * @param c
28 * @throws Exception
29 */
30 @SuppressWarnings("unchecked")
31 public void beanTool(BeanUtils beanUtils ,Class c)throws Exception{
32 beanUtils.createBeanDao(c);
33 beanUtils.createBeanDaoImpl(c);
34 beanUtils.createBeanService(c);
35 beanUtils.createBeanServiceImpl(c);
36 }
37 }
源码下载 http://files.cnblogs.com/hongten/UUUU_Web_Test.zip
代码自动生成工具_java版的更多相关文章
- 基于数据库的代码自动生成工具,生成JavaBean、生成数据库文档、生成前后端代码等(v6.0.0版)
TableGo v6.0.0 版震撼发布,此次版本更新如下: 1.UI界面大改版,组件大调整,提升界面功能的可扩展性. 2.新增BeautyEye主题,界面更加清新美观,也可以通过配置切换到原生Jav ...
- 代码自动生成工具MyGeneration之一(程序员必备工具)
代码自动生成工具MyGeneration之一(程序员必备工具) 转 分类: C#2008-08-06 18:12 16064人阅读 评论(12) 收藏 举报 工具数据库相关数据库stringbrows ...
- Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展
Asp.net mvc 5 CRUD代码自动生成工具 -Visual Studio.net2013 Saffolding功能扩展 上次做过一个<Asp.net webform scaffoldi ...
- C# 代码自动生成工具
开源:C# 代码自动生成工具,支持站点前后台 前言 写这个项目有很长一段时间了,期间也修修改改,写到最后,自己也没咋用(研究方向变化了). 正文 具体项目开源了:https://github.co ...
- iBatis 代码自动生成工具 iBator 及 Example 使用
iBator的下载和安装 官方下载地址:http://people.apache.org/builds/ibatis/ibator/ 安装:见<Eclipse 插件安装> 安装完成后,“F ...
- 代码自动生成工具MyGeneration之一
前段时间用C#做网站,用到了大量数据库相关的东西.网站采用3层结构,即数据访问层(Data Access Layer),业务逻辑层(Business Logic Layer),页面表现层().做了一段 ...
- mybatis-generator 代码自动生成工具
今天来介绍下怎么用mybatis-gennerator插件自动生成mybatis所需要的dao.bean.mapper xml文件,这样我们可以节省一部分精力,把精力放在业务逻辑上. 之前看过很多文章 ...
- mybatis-generator 代码自动生成工具(maven方式)
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,mybatis-gennerator插件帮我们自动生成mybatis所需要的 ...
- Py福利,基于uiautomatorviewer 的Python 自动化代码自动生成工具分享(jar已发布GitHub,欢迎Star)
前言做UI自动化无论你用SDK自带的uiautomatorviewer还是Macaca还是Appium自动的inspector,代码最多的就是那些繁琐重复的找元素后点击,输入,长按.....等.现在偷 ...
随机推荐
- R简易入门(二)
本文内容来源:https://www.dataquest.io/mission/128/working-with-data-frames 本文摘要:简单介绍一下用R处理数据 原始数据展示(这是一份 ...
- Golang学习笔记
一.基础 1. Hello World程序 demo: package main import "fmt" // 注释 //注释 func main() { fmt.Printf( ...
- STM32 ucosii 串口接收数据 遇到的问题及解决思路
写一个程序,用到了ucos ii ,串口在中断中接收数据(一包数据 8个字节 包含: 1byte包头 5byte数据 1byte校验和 1byte 包尾 ) ,数据由上位机每隔500ms发送一次,在串 ...
- Win8.1 IIS6 SQL SERVER 2012 执行 SqlServices.InstallSessionState 出错
新装了WIN8.1,感觉很不错. 新建了第一个站点是,在执行 SqlServices.InstallSessionState("localhost", null, SessionS ...
- mtu
通信术语 最大传输单元(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位).最大传输单元这个参数通常与通信接口有关(网络接 ...
- C#TCPClient应用-一个简单的消息发送和接收
TcpSend窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题. 源代码: 发送消息的窗口代码 using S ...
- 结构体,公用体,枚举类型的sizeof
1)枚举类enum型空间计算 enum只是定义了一个常量集合,里面没有“元素”,而枚举类型是当做int来存储的,所以枚举类型的sizeof值都为4 enum color(red,pink,white, ...
- phpcms v9 企业黄页系统发布没有表单出现的解决方案
第一种解决方案: 第一步:把yp_UTF8压缩文件解压得到:api.caches.phpcms.statics四个文件夹. 第二步:把这四个文件夹分别覆盖已安装好的phpcms系统根目录下的文件夹.这 ...
- div+css登陆界面案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- javascript_22_for_js性感的v
<script type="text/javascript"> window.onload=function(){ var aDiv=document.getEleme ...