zoukankan      html  css  js  c++  java
  • 利用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 }

    源码下载https://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,然后加入就可以啦
     
  • 相关阅读:
    Python学习资料
    异常
    I/O
    Python3+迭代器与生成器
    python标准数据类型
    人工智能、机器学习和深度学习
    原地排序和复制排序
    序列化和Json
    登陆加密小程序
    hashlib模块加密用法
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_velocity_automatic_code_generation.html
Copyright © 2011-2022 走看看