利用Velocity自动生成自定义代码_java版_源码下载

项目结构:

运行效果:你可能会看到项目中有报错,不用着急,这个不会影响到你的项目运行。

把其中的User类拿出来,给大家分享一下:

自动生成UserDao.java效果:

自动生成UserDaoImpl.java效果:

自动生成UserService.java效果:

自动生成UserServiceImpl.java效果:

我想大家看到这里,是不是有亲自动手试试的兴奋感觉....

=======================================================

代码部分:这个是对“自己写的一个代码自动生成工具_java版_源码下载”的改进

=======================================================

/AutomaticCodeGeneration/src/com/b510/base/bean/User.java

 1 /**
 2  * 
 3  */
 4 package com.b510.base.bean;
 5 
 6 /**
 7  * @author hongten<br>
 8  * @date 2013-3-10
 9  */
10 public class User {
11 
12 }

/AutomaticCodeGeneration/src/com/b510/base/bean/install/Annotation.java

 1 /**
 2  * 
 3  */
 4 package com.b510.base.bean.install;
 5 
 6 /**
 7  * 注释
 8  * @author hongten<br>
 9  * @date 2013-3-10
10  */
11 public class Annotation {
12 
13     /**
14      * 作者名称
15      */
16     private String authorName;
17     /**
18      * 作者邮箱
19      */
20     private String authorMail;
21     /**
22      * 日期
23      */
24     private String date;
25     /**
26      * 版本
27      */
28     private String version;
29 
30     public String getAuthorName() {
31         return authorName;
32     }
33 
34     public void setAuthorName(String authorName) {
35         this.authorName = authorName;
36     }
37 
38     public String getAuthorMail() {
39         return authorMail;
40     }
41 
42     public void setAuthorMail(String authorMail) {
43         this.authorMail = authorMail;
44     }
45 
46     public String getDate() {
47         return date;
48     }
49 
50     public void setDate(String date) {
51         this.date = date;
52     }
53 
54     public String getVersion() {
55         return version;
56     }
57 
58     public void setVersion(String version) {
59         this.version = version;
60     }
61     
62     
63 }

/AutomaticCodeGeneration/src/com/b510/base/bean/install/Bean.java

 1 /**
 2  * 
 3  */
 4 package com.b510.base.bean.install;
 5 
 6 /**
 7  * bean类
 8  * 
 9  * @author hongten<br>
10  * @date 2013-3-10
11  */
12 public class Bean {
13 
14     /** bean 名称 */
15     private String name;
16     /** bean 首字母小写名称 */
17     private String lowerName;
18     /** bean 路径 */
19     private String beanUrl;
20     /** dao 路径 */
21     private String beanDaoUrl;
22     /** dao 实现路径 */
23     private String beanDaoImplUrl;
24     /** service 路径 */
25     private String beanServiceUrl;
26     /** service 实现路径 */
27     private String beanServiceImplUrl;
28 
29     public String getName() {
30         return name;
31     }
32 
33     public void setName(String name) {
34         this.name = name;
35     }
36 
37     public String getLowerName() {
38         return lowerName;
39     }
40 
41     public void setLowerName(String lowerName) {
42         this.lowerName = lowerName;
43     }
44 
45     public String getBeanUrl() {
46         return beanUrl;
47     }
48 
49     public void setBeanUrl(String beanUrl) {
50         this.beanUrl = beanUrl;
51     }
52 
53     public String getBeanDaoUrl() {
54         return beanDaoUrl;
55     }
56 
57     public void setBeanDaoUrl(String beanDaoUrl) {
58         this.beanDaoUrl = beanDaoUrl;
59     }
60 
61     public String getBeanDaoImplUrl() {
62         return beanDaoImplUrl;
63     }
64 
65     public void setBeanDaoImplUrl(String beanDaoImplUrl) {
66         this.beanDaoImplUrl = beanDaoImplUrl;
67     }
68 
69     public String getBeanServiceUrl() {
70         return beanServiceUrl;
71     }
72 
73     public void setBeanServiceUrl(String beanServiceUrl) {
74         this.beanServiceUrl = beanServiceUrl;
75     }
76 
77     public String getBeanServiceImplUrl() {
78         return beanServiceImplUrl;
79     }
80 
81     public void setBeanServiceImplUrl(String beanServiceImplUrl) {
82         this.beanServiceImplUrl = beanServiceImplUrl;
83     }
84 
85 }

/AutomaticCodeGeneration/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.io.StringWriter;
  9 import java.text.SimpleDateFormat;
 10 import java.util.Date;
 11 
 12 import org.apache.velocity.Template;
 13 import org.apache.velocity.VelocityContext;
 14 import org.apache.velocity.app.VelocityEngine;
 15 
 16 
 17 /**
 18  * @author hongten<br>
 19  * @date 2013-3-10
 20  */
 21 public class BeanUtils {
 22     
 23     public static final String BEAN_DAO_TEMPLATE_VM_PATH = "src/vms/beanDao.vm";
 24     public static final String BEAN_DAO_IMPL_TEMPLATE_VM_PATH = "src/vms/beanDaoImpl.vm";
 25     public static final String BEAN_SERVICE_TEMPLATE_VM_PATH = "src/vms/beanService.vm";
 26     public static final String BEAN_SERVICE_IMPL_TEMPLATE_VM_PATH = "src/vms/beanServiceImpl.vm";
 27 
 28      //文件 地址
 29     //private static final String BEAN_PATH = "com/b510/base/bean";
 30     private static final String DAO_PATH = "com/b510/base/dao";
 31     private static final String DAO_IMPL_PATH = "com/b510/base/dao/impl";
 32     private static final String SERVICE_PATH = "com/b510/base/service";
 33     private static final String SERVICE_IMPL_PATH = "com/b510/base/service/impl";
 34 
 35     
 36     
 37     //包名
 38     private static final String BEAN_URL = "com.b510.base.bean";
 39     private static final String DAO_URL = "com.b510.base.dao";
 40     private static final String DAO_IMPL_URL = "com.b510.base.dao.impl";
 41     private static final String SERVICE_URL = "com.b510.base.service";
 42     private static final String SERVICE_IMPL_URL = "com.b510.base.service.impl";
 43 
 44     
 45     private static Bean bean = new Bean();
 46     private static Annotation annotation = new Annotation();
 47     
 48     /**
 49      * 初始化bean和注解
 50      * @param c
 51      */
 52     public void init(Class c){
 53         if(c != null){
 54             String cName = c.getName();
 55             bean.setName(getLastChar(cName));
 56             bean.setBeanUrl(cName);
 57             bean.setLowerName(getLowercaseChar(getLastChar(cName)));
 58 
 59             annotation.setAuthorName("hongten");
 60             annotation.setAuthorMail("hongtenzone@foxmail.com");
 61             SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
 62                     "yyyy-MM-dd");
 63             annotation.setDate(simpleDateFormat.format(new Date()));
 64             annotation.setVersion("1.0");
 65         }
 66     }
 67     
 68     /**
 69      * 创建bean的Dao<br>
 70      * 
 71      * @param c
 72      * @throws Exception
 73      */
 74     public void createBeanDao(Class c) throws Exception {
 75         String cName = c.getName();
 76         String path = System.getProperty("user.dir") + "/src/" + DAO_PATH
 77                 + "/";
 78         File filePath = new File(path);
 79         createFilePath(filePath);
 80         
 81         String fileName = path + getLastChar(cName) + "Dao.java";
 82         File file = new File(fileName);
 83         FileWriter fw = new FileWriter(file);
 84         
 85         bean.setBeanDaoUrl(DAO_URL);
 86         
 87         fw.write(createCode(BEAN_DAO_TEMPLATE_VM_PATH,bean,annotation));
 88         fw.flush();
 89         fw.close();
 90         showInfo(fileName);
 91     }
 92     
 93     /**
 94      * 创建bean的Dao的实现<br>
 95      * 
 96      * @param c
 97      * @throws Exception
 98      */
 99     public void createBeanDaoImpl(Class c) throws Exception {
100         String cName = c.getName();
101         String path = System.getProperty("user.dir") + "/src/" + DAO_IMPL_PATH
102                 + "/";
103         File filePath = new File(path);
104         createFilePath(filePath);
105         
106         String fileName = path + getLastChar(cName) + "DaoImpl.java";
107         File file = new File(fileName);
108         FileWriter fw = new FileWriter(file);
109         
110         bean.setBeanDaoUrl(DAO_URL);
111         bean.setBeanDaoImplUrl(DAO_IMPL_URL);
112         
113         fw.write(createCode(BEAN_DAO_IMPL_TEMPLATE_VM_PATH,bean,annotation));
114         fw.flush();
115         fw.close();
116         showInfo(fileName);
117     }
118     
119     
120     /**
121      * 创建bean的Service<br>
122      * 
123      * @param c
124      * @throws Exception
125      */
126     public void createBeanService(Class c) throws Exception {
127         String cName = c.getName();
128         String path = System.getProperty("user.dir") + "/src/" + SERVICE_PATH
129                 + "/";
130         File filePath = new File(path);
131         createFilePath(filePath);
132         
133         String fileName = path + getLastChar(cName) + "Service.java";
134         File file = new File(fileName);
135         FileWriter fw = new FileWriter(file);
136         
137         bean.setBeanServiceUrl(SERVICE_URL);
138         
139         fw.write(createCode(BEAN_SERVICE_TEMPLATE_VM_PATH,bean,annotation));
140         fw.flush();
141         fw.close();
142         showInfo(fileName);
143     }
144     
145     /**
146      * 创建bean的Service的实现<br>
147      * 
148      * @param c
149      * @throws Exception
150      */
151     public void createBeanServiceImpl(Class c) throws Exception {
152         String cName = c.getName();
153         String path = System.getProperty("user.dir") + "/src/" + SERVICE_IMPL_PATH
154                 + "/";
155         File filePath = new File(path);
156         createFilePath(filePath);
157         
158         String fileName = path + getLastChar(cName) + "ServiceImpl.java";
159         File file = new File(fileName);
160         FileWriter fw = new FileWriter(file);
161         
162         bean.setBeanDaoUrl(DAO_URL);
163         bean.setBeanServiceUrl(SERVICE_URL);
164         bean.setBeanServiceImplUrl(SERVICE_IMPL_URL);
165         
166         fw.write(createCode(BEAN_SERVICE_IMPL_TEMPLATE_VM_PATH,bean,annotation));
167         fw.flush();
168         fw.close();
169         showInfo(fileName);
170     }
171     
172     
173     
174     
175     
176     
177     
178     
179     
180     
181     
182     /**
183      * 根据模板生成代码
184      * @param fileVMPath 模板路径 
185      * @param bean 目标bean
186      * @param annotation 注释
187      * @return
188      * @throws Exception
189      */
190     public String createCode(String fileVMPath,Bean bean,Annotation annotation) throws Exception{
191         VelocityEngine velocityEngine = new VelocityEngine();
192         velocityEngine.setProperty("input.encoding", "UTF-8");
193         velocityEngine.setProperty("output.encoding", "UTF-8");
194         velocityEngine.init();
195         Template template = velocityEngine.getTemplate(fileVMPath);
196         VelocityContext velocityContext = new VelocityContext();
197         velocityContext.put("bean", bean);
198         velocityContext.put("annotation", annotation);
199         StringWriter stringWriter = new StringWriter();
200         template.merge(velocityContext, stringWriter);
201         return stringWriter.toString();
202     }
203     
204      /**
205      * 创建文件
206      * @param file
207      */
208     public void createFilePath(File file){
209         if(!file.exists()){
210             System.out.println("创建["+file.getAbsolutePath()+"]情况:"+file.mkdirs());
211         }else{
212             System.out.println("存在目录:"+file.getAbsolutePath());
213         }
214     }
215     
216  
217     
218     
219     /**
220      * 获取路径的最后面字符串<br>
221      * 如:<br>
222      *     <code>str = "com.b510.base.bean.User"</code><br>
223      *     <code> return "User";<code>
224      * @param str
225      * @return
226      */
227     public String getLastChar(String str) {
228         if ((str != null) && (str.length() > 0)) {
229             int dot = str.lastIndexOf('.');
230             if ((dot > -1) && (dot < (str.length() - 1))) {
231                 return str.substring(dot + 1);
232             }
233         }
234         return str;
235     }
236     
237     /**
238      * 把第一个字母变为小写<br>
239      * 如:<br>
240      *     <code>str = "UserDao";</code><br>
241      *     <code>return "userDao";</code>
242      * @param str
243      * @return
244      */
245     public String getLowercaseChar(String str){
246         return str.substring(0,1).toLowerCase()+str.substring(1);
247     }
248 
249     /**
250      * 显示信息
251      * @param info
252      */
253     public void showInfo(String info){
254         System.out.println("创建文件:"+ info+ "成功!");
255     }
256     
257     /**
258      * 获取系统时间
259      * @return
260      */
261     public static String getDate(){
262         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
263         return simpleDateFormat.format(new Date());
264     }
265 }

/AutomaticCodeGeneration/src/com/b510/base/bean/install/BeanUtilsTest.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 /**
12  * @author hongten<br>
13  * @date 2013-3-10
14  */
15 public class BeanUtilsTest{
16     public static void main(String[] args) throws Exception{
17         BeanUtilsTest beanUtilTest = new BeanUtilsTest();
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.init(c);
33         beanUtils.createBeanDao(c);
34         beanUtils.createBeanDaoImpl(c);
35         beanUtils.createBeanService(c);
36         beanUtils.createBeanServiceImpl(c);
37     }
38 }

/AutomaticCodeGeneration/src/vms/beanDao.vm

 1 ##create bean dao,this is a interface
 2 package $!{bean.beanDaoUrl};
 3 
 4 import com.blackants.core.dao.BaseDao;
 5 import $!{bean.beanUrl};
 6 
 7 /**
 8  * @author <a href="mailto:$!{annotation.authorMail}">$!{annotation.authorName}</a>
 9  * @date $!{annotation.date}
10  *
11  * @version $!{annotation.version}
12  */
13 public interface $!{bean.name}Dao extends BaseDao<$!{bean.name}> {
14 
15 }

/AutomaticCodeGeneration/src/vms/beanDaoImpl.vm

 1 ##create bean daoImpl,this is a class
 2 package $!{bean.beanDaoImplUrl};
 3 
 4 import com.blackants.core.dao.impl.AbstractBaseDaoImpl;
 5 import $!{bean.beanDaoUrl}.$!{bean.name}Dao;
 6 import $!{bean.beanUrl};
 7 
 8 /**
 9  * @author <a href="mailto:$!{annotation.authorMail}">$!{annotation.authorName}</a>
10  * @date $!{annotation.date}
11  *
12  * @version $!{annotation.version}
13  */
14 public class $!{bean.name}DaoImpl extends AbstractBaseDaoImpl<$!{bean.name}> implements $!{bean.name}Dao{
15 
16 }

/AutomaticCodeGeneration/src/vms/beanService.vm

 1 ##create bean service,this is a interface
 2 package $!{bean.beanServiceUrl};
 3 
 4 import com.blackants.core.service.BaseService;
 5 import $!{bean.beanUrl};
 6 
 7 /**
 8  * @author <a href="mailto:$!{annotation.authorMail}">$!{annotation.authorName}</a>
 9  * @date $!{annotation.date}
10  *
11  * @version $!{annotation.version}
12  */
13 public interface $!{bean.name}Service extends BaseService<$!{bean.name}> {
14 
15 }

/AutomaticCodeGeneration/src/vms/beanServiceImpl.vm

 1 ##create bean serviceImpl,this is a class
 2 package $!{bean.beanServiceImplUrl};
 3 
 4 import com.blackants.core.service.impl.AbstractBaseServiceImpl;
 5 import com.blackants.core.dao.BaseDao;
 6 import $!{bean.beanDaoUrl}.$!{bean.name}Dao;
 7 import $!{bean.beanServiceUrl}.$!{bean.name}Service;
 8 import $!{bean.beanUrl};
 9 
10 /**
11  * @author <a href="mailto:$!{annotation.authorMail}">$!{annotation.authorName}</a>
12  * @date $!{annotation.date}
13  *
14  * @version $!{annotation.version}
15  */
16 public class $!{bean.name}ServiceImpl extends AbstractBaseServiceImpl<$!{bean.name}> implements $!{bean.name}Service{
17 
18     /**
19      * $!{bean.lowerName} Dao
20      */
21     private $!{bean.name}Dao $!{bean.lowerName}Dao;
22     
23     @Override
24     public BaseDao<$!{bean.name}> getBaseDao() {
25         return $!{bean.lowerName}Dao;
26     }
27 
28     // =================setter================= //
29     
30     public void setActivationCodeDao($!{bean.name}Dao $!{bean.lowerName}Dao) {
31         this.$!{bean.lowerName}Dao = $!{bean.lowerName}Dao;
32     }
33     
34 }

源码下载http://files.cnblogs.com/hongten/AutomaticCodeGeneration.rar

下载说明:

如果你下载了此压缩包,并且在你的本地导入eclipse,运行程序你会发现系统找不到:

com.blackants.core.dao.BaseDao;(interface)
com.blackants.core.dao.impl.AbstractBaseDaoImpl;(class)
com.blackants.core.service.BaseService;(interface)
com.blackants.core.service.impl.AbstractBaseServiceImpl;(class)
上面所列出的文件,这个不用着急,你可以在项目中新建这些package,然后加入就可以啦

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Hongten

自己写的一个代码自动生成工具_java版_源码下载

这里要实现的功能是,当我们给出了bean,如:Admin,User,People等实体类后,

1.7K2
来自专栏fixzd

excel导出工具

通常使用的是无参构造函数。另外两个都需要自己实例化workbook对象,有三个参数的构造函数,需要传入workbook、标题行样式对象、数据行样式对象。关于样式...

1011
来自专栏iOS122-移动混合开发研究院

【自问自答】关于 Swift 的几个疑问

感觉自己给自己释疑,也是一个极为有趣的过程。这次,我还新增了“猜想”一栏,来尝试回答一些暂时没有足够资料支撑的问题。 Swift 版本是:4.0.3。不同版本的...

3246
来自专栏码匠的流水账

聊聊eureka server的response cache

eureka-core-1.8.8-sources.jar!/com/netflix/eureka/resources/ApplicationResource....

1143
来自专栏程序猿DD

Spring框架中的设计模式(三)

在之前的两篇文章中,我们看到了一些在Spring框架中实现的设计模式。这一次我们会发现这个流行框架使用的3种新模式。 本文将从描述两个创意设计模式开始:原型和...

4267
来自专栏分布式系统进阶

Kafka中Message存储相关类大揭密Kafka源码分析-汇总

2121
来自专栏源码之家

word如何自动分割成多个文档

4335
来自专栏后端沉思录

SpringMVC启动加载、请求分析

DispatcherServlet其实是一个Servlet,用于初始化各个功能的实现类,比如异常处理、视图处理、请求映射等;且继承了FrameworkServl...

1183
来自专栏偏前端工程师的驿站

Java魔法堂:JUnit4使用详解

目录                                                                              ...

1875
来自专栏Android知识点总结

SpringBoot-08-之统一化json输出与自定义异常捕获

2361

扫码关注云+社区

领取腾讯云代金券