自动代码生成

/**


 * 


 */


package com.liuyonggang.component.utils;




import java.io.File;


import java.io.FileWriter;


import java.text.SimpleDateFormat;


import java.util.Date;








/**


 * @author liuyonggang


 * @date 2014-11-12


 */


@SuppressWarnings("unchecked")


public class BeanUtils {


 




 


 //公共部分


 private static final String RT_1 = "\r\n";


 private static final String RT_2 = RT_1+RT_1;


 private static final String BLANK_1 =" ";


 private static final String BLANK_4 ="    ";


 private static final String BLANK_8 =BLANK_4 + BLANK_4;


 


 


 


 //注释部分


 private static final String ANNOTATION_AUTHOR_PARAMTER = "@author ";


 private static final String ANNOTATION_AUTHOR_NAME = "liuyonggang";


 private static final String ANNOTATION_AUTHOR = ANNOTATION_AUTHOR_PARAMTER + ANNOTATION_AUTHOR_NAME;


 private static final String ANNOTATION_DATE = "@date ";


 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;


 


 


 //文件 地址


 private static final String DAO_PATH = "com/liuyonggang/component/repository/gps";


 private static final String SERVICE_PATH = "com/liuyonggang/component/sevice/gps";




 


 //包名


 private static final String DAO_URL = "com.liuyonggang.component.repository.gps";


 private static final String SERVICE_URL = "com.liuyonggang.component.sevice.gps";


 


 /**


  * 创建bean的Dao<br>


  * 


  * @param c


  * @throws Exception


  */


 public void createBeanDao(Class c) throws Exception {


 String cName = c.getName();


 String fileName = System.getProperty("user.dir") + "/src/main/java/" + DAO_PATH+ "/" + getLastChar(cName) + "Repository.java";


 


 File f = new File(fileName);


 FileWriter fw = new FileWriter(f);


 


 fw.write("package "+DAO_URL+";"+RT_2+


 


  "import com.liuyonggang.component.core.DefaultEntityRepository;"+RT_2+


  "import "+cName+";"+RT_2


  


  +ANNOTATION+


  "public interface " + getLastChar(cName) + "Repository extends DefaultEntityRepository <" + c.getSimpleName() + ",Integer> {"+RT_2+"}");


 


 fw.flush();


 fw.close();


 showInfo(fileName);


 }


   


    /**


     * 创建bean的service


     * @param c


     * @throws Exception


     */


    public void createBeanService(Class c) throws Exception{


     String cName = c.getName();


 String fileName = System.getProperty("user.dir") + "/src/main/java/" + SERVICE_PATH+ "/" + getLastChar(cName) + "Service.java";


 


 File f = new File(fileName);


 FileWriter fw = new FileWriter(f);


 


 fw.write("package "+SERVICE_URL+";"+RT_2+


 


  "import javax.annotation.Resource;"+RT_1+


  "import javax.transaction.Transactional;"+RT_2+


  


  "import org.springframework.stereotype.Service;"+RT_2+


  


  "import com.liuyonggang.component.core.DefaultEntityRepository;"+RT_1+


  "import com.liuyonggang.component.core.DefaultEntityService;"+RT_1+


  "import "+cName+";"+RT_1+


  "import "+DAO_URL+"."+c.getSimpleName()+"Repository;"+RT_2


  


  +ANNOTATION+


  "@Service"+RT_1+


  "@Transactional"+RT_1+


  "public class " + getLastChar(cName) + "Service extends DefaultEntityService <" + c.getSimpleName() + ",Integer> {"+RT_2+


  


   BLANK_8+"@Resource"+RT_1+


 BLANK_8+c.getSimpleName()+"Repository "+getLowercaseChar(c.getSimpleName()+"Repository; ")+RT_2+


 


 BLANK_8+"@Override"+RT_1+


 BLANK_8+"protected DefaultEntityRepository<"+c.getSimpleName()+", Integer> getDao() {"+RT_1+


 BLANK_8+BLANK_4+"return "+getLowercaseChar(c.getSimpleName()+"Repository; ")+RT_1+


 BLANK_8+"}"+RT_1+


 


  "}");


 


 


 fw.flush();


 fw.close();


 showInfo(fileName);


    }




    




 /**


  * 获取路径的最后面字符串<br>


  * 如:<br>


  *     <code>str = "com.liuyonggang.base.bean.User"</code><br>


  *     <code> return "User";<code>


  * @param str


  * @return


  */


 public String getLastChar(String str) {


 if ((str != null) && (str.length() > 0)) {


 int dot = str.lastIndexOf('.');


 if ((dot > -1) && (dot < (str.length() - 1))) {


 return str.substring(dot + 1);


 }


 }


 return str;


 }


 


 /**


  * 把第一个字母变为小写<br>


  * 如:<br>


  *     <code>str = "UserDao";</code><br>


  *     <code>return "userDao";</code>


  * @param str


  * @return


  */


 public String getLowercaseChar(String str){


 return str.substring(0,1).toLowerCase()+str.substring(1);


 }




 /**


  * 显示信息


  * @param info


  */


 public void showInfo(String info){


 System.out.println("创建文件:"+ info+ "成功!");


 }


 


 /**


  * 获取系统时间


  * @return


  */


 public static String getDate(){


 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");


 return simpleDateFormat.format(new Date());


 }


 


}








/**


 * 


 */


package com.liuyonggang.component.utils;







/**


 * @author liuyonggang


 * @date 2014-11-12


 */




public class BeanUtilTest {




 public static void main(String[] args) throws Exception{


 BeanUtilTest beanUtilTest = new BeanUtilTest();


 BeanUtils beanUtils = new BeanUtils();


 Object [] array =new Object[]{


 User.class,


 Admin.class


 };


 


 for (int i = 0; i < array.length; i++) {


 beanUtilTest.beanTool(beanUtils, (Class) array[i]);


 }


 


 


 }


 


 /**


  * 根据bean生成相应的文件


  * @param beanUtils


  * @param c


  * @throws Exception


  */


 @SuppressWarnings("unchecked")


 public void beanTool(BeanUtils beanUtils ,Class c)throws Exception{


 beanUtils.createBeanDao(c);


 beanUtils.createBeanService(c);


 }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值