zoukankan      html  css  js  c++  java
  • freemark告别无休止的增删改查

    首先建立一个模版文件。

    当然freemark建议的是ftl结尾。其实这个是可以自己设定的。

    这里需要根据需求添加的元素就用${}包含起来.然后在java文件中去添加就可以了。

    如下:

    /**
     * @title 			${model_name}.java 
     * @description 	
     * @create 			${date?string("yyyy-MM-dd HH:mm:ss")}  By ${author}
     * @package 		${package_name}
     * @copyright 		Copyright (c) 2011-2012 ${link} . All right reserved
     * @version        $Id$
     * XYRJ-Java-Project
     */
     
     
    package com.freemark.model;  
      
    import java.util.List;  
     
    import com.freemark.pojo.${model_pojo};  
      
    /**
     * 
     * @description	
     * 
     * @classname 	${model_name_cn} 
     * @author 		${author}
     * @date 		${date?string("yyyy-MM-dd HH:mm:ss")} 
     * @version 	1.0
     */
    public class ${model_name}
    {  
    
    	/**
    	 * 
         * 根据${model_name_cn}查找${model_name_cn}信息 
    	 *  
    	 * @description
    	 * 
    	 * @title			find${model_pojo}ById
    	 * @author			yangzhi<helloyangzhi@foxmail.com>
    	 * @param			Long ${instant}Id
    	 * @return			${model_name}
    	 * @throws
    	 */
        public ${model_name} find${model_pojo}ById(Long ${instant}Id)
        {
        	return null;
        } 
          
        /** 
         * 批量物理删除${model_name_cn}(不可恢复) 
         * @param ${instant}Ids  ${model_name_cn}编号 
         * @throws DAOException 
         */  
        public void delete${model_name_list}(Long[] ${instant}Ids)
        {
        
        }  
          
        /** 
         * 物理删除${model_name_cn}(不可恢复) 
         * @param ${instant}Id  ${model_name_cn}编号 
         * @throws DAOException 
         */  
        public void delete${model_pojo}(Long ${instant}Id)
        {
        
        } 
          
        /** 
         * 保存${model_name_cn} 
         * @param ${instant} 
         * @throws DAOException 
         */  
        public void save${model_pojo}(${model_pojo} ${instant})
        {
        
        }  
          
        /** 
         * 更新${model_name_cn} 
         * @param ${instant} 
         * @throws DAOException 
         */  
        public void update${model_pojo}(${model_pojo} ${instant})
        {
        
        }  
          
        /** 
         * 利用hql语句查询${model_name_cn}信息
         * @param hsql 
         * @throws DAOException 
         */  
        public List<${model_pojo}> find${model_name_list}(String hsql)
        {
    	    return null;
        }  
          
        /** 
         * 利用hql语句查询${model_name_cn}信息 
         * @param hsql 
         * @throws DAOException 
         */  
        public List<${model_pojo}> find${model_name_list}(String hsql,Object[] params)
        {
    	    return null;
        } 
      
    }  

     这里需要引扩freemark.jar.然后在去写一个工具类,如下:

    /**
     * @title 			Client.java 
     * @description 	
     * @create 			2013-4-7 ����03:22:56 By yangzhi
     * @package 		com.freemark
     * @copyright 		Copyright (c) 2011-2012 http://xyrj.hhtc.edu.cn.com. All right reserved
     * @version         $Id$
     * XYRJ-Java-Project
     */
    
    
    package com.freemark;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.utility.Constants;
    
    /**
     * 
     * @description	
     * 
     * @classname 	Client 
     * @author 		yangzhi<helloyangzhi@foxmail.com>
     * @date 		2013-4-7 下午04:37:38 
     * @version 	1.0
     */
    public class Client {
    	
    	private Configuration cfg;
    	
    	public void init() throws Exception{
    		
    		cfg = new Configuration();
    		//模版文件所在的目录
    		cfg.setDirectoryForTemplateLoading(new File("template//"));
    	}
    	
    	/**
    	 * 建立模版
    	 *  
    	 * @description
    	 * 
    	 * @title			buildTemplate
    	 * @author			yangzhi<helloyangzhi@foxmail.com>
    	 * @param			@throws Exception
    	 * @return			void
    	 * @throws
    	 */
    	public void buildTemplate() throws Exception {  
    		//设置root属性值
    		Map<String,Object> root = this.setRootVal();
    		//创建文件
            this.createFile(root);  
        }
    	
    	/**
    	 * 设置root的值
    	 *  
    	 * @description
    	 * 
    	 * @title			setRootVal
    	 * @author			yangzhi<helloyangzhi@foxmail.com>
    	 * @param			@return
    	 * @return			Map<String,Object>
    	 * @throws
    	 */
    	@SuppressWarnings("serial")
    	public Map<String,Object> setRootVal(){
    		Map<String,Object> root = new HashMap<String,Object>(){
    			{
    				put("module","User");
    				put("model_name","UserModel");
    				put("model_pojo","UserPojo");
    				put("package_name","com.freemark.model");
    				put("model_name_list","Users");
    				put("instant","user");
    				put("model_name_cn","用户");
    				put("author","yangzhi");
    				put("link","www.helloyangzhi.com");
    				put("date",new Date());
    			}
    		};
    		
    		return root;
    	}
    	 
    	/**
    	 * 生成代码文件
    	 *  
    	 * @description
    	 * 
    	 * @title			createFile
    	 * @author			yangzhi<helloyangzhi@foxmail.com>
    	 * @param			@param root
    	 * @return			void
    	 * @throws
    	 */
    	public void createFile(Map root) 
    	{  
    		try {  
    			//工程路径
    			//如果是web请求
    			//request.getRequestURI()
    			//这里就用了一个绝对路径
    	        String projectPath		= "D://java//workspace//freemarkdemo//";  
    	        //文件名
    	        String fileName			= root.get("model_name")+ ".java";  
    	        //保存路径
    	        String savePath			= "src//com//freemark//model//";  
    	        //模版文件,后缀随意
    	        Template template		= cfg.getTemplate("HelloAction.temp"); 
    	        
    	        String realFileName = projectPath + savePath + fileName;  
    	        String realSavePath = projectPath + "/" + savePath;  
    	        File newsDir = new File(realSavePath);  
    	        if (!newsDir.exists()) {  
    	            newsDir.mkdirs();  
    	        } 
                Writer out = new OutputStreamWriter(
    		            	new FileOutputStream(realFileName),
    	            		"UTF-8");
                template.process(root, out);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
    	}  
    	  
    	/**
    	 * 
    	 *  
    	 * @description
    	 * 
    	 * @title			main
    	 * @author			yangzhi<helloyangzhi@foxmail.com>
    	 * @param			@param args
    	 * @param			@throws Exception
    	 * @return			void
    	 * @throws
    	 */
        public static void main(String[] args) throws Exception {  
            Client client = new Client();  
            client.init();  
            client.buildTemplate();  
        }  
    }
    

    运行之后就可以看到:

    /**
     * @title 			UserModel.java 
     * @description 	
     * @create 			2013-04-07 21:31:12  By yangzhi 
     * @package 		com.freemark.model
     * @copyright 		Copyright (c) 2011-2012 www.helloyangzhi.com . All right reserved
     * @version        $Id$
     * XYRJ-Java-Project
     */
     
     
    package com.freemark.model;  
      
    import java.util.List;  
     
    import com.freemark.pojo.UserPojo;  
      
    /**
     * 
     * @description	
     * 
     * @classname 	用户 
     * @author 		yangzhi
     * @date 		2013-04-07 21:31:12 
     * @version 	1.0
     */
    public class UserModel
    {  
    
    	/**
    	 * 
         * 根据用户查找用户信息 
    	 *  
    	 * @description
    	 * 
    	 * @title			findUserPojoById
    	 * @author			yangzhi<helloyangzhi@foxmail.com>
    	 * @param			Long userId
    	 * @return			UserModel
    	 * @throws
    	 */
        public UserModel findUserPojoById(Long userId)
        {
        	return null;
        } 
          
        /** 
         * 批量物理删除用户(不可恢复) 
         * @param userIds  用户编号 
         * @throws DAOException 
         */  
        public void deleteUsers(Long[] userIds)
        {
        
        }  
          
        /** 
         * 物理删除用户(不可恢复) 
         * @param userId  用户编号 
         * @throws DAOException 
         */  
        public void deleteUserPojo(Long userId)
        {
        
        } 
          
        /** 
         * 保存用户 
         * @param user 
         * @throws DAOException 
         */  
        public void saveUserPojo(UserPojo user)
        {
        
        }  
          
        /** 
         * 更新用户 
         * @param user 
         * @throws DAOException 
         */  
        public void updateUserPojo(UserPojo user)
        {
        
        }  
          
        /** 
         * 利用hql语句查询用户信息
         * @param hsql 
         * @throws DAOException 
         */  
        public List<UserPojo> findUsers(String hsql)
        {
    	    return null;
        }  
          
        /** 
         * 利用hql语句查询用户信息 
         * @param hsql 
         * @throws DAOException 
         */  
        public List<UserPojo> findUsers(String hsql,Object[] params)
        {
    	    return null;
        } 
      
    }  



  • 相关阅读:
    Java多线程学习
    JAVA类的加载机制
    这四款也许是电脑录屏软件中免费、无广告且最实用的,程序员必备
    手机端没有好的录屏软件?地表最强移动端录屏软件了解一下?
    手把手教你 在Pytorch框架上部署和测试 关键点人脸检测项目DBFace,成功实现人脸检测效果
    520是秀恩爱吃狗粮,521才是真正的告白日,- Python告白神器用起来 !
    截图还在使用QQ的Ctrl + Alt + A 截图?还不会网页长截图?
    卷积神经网络之卷积操作,使用卷积运算实现图片边缘特征检测
    小白也能弄懂的卷积神经网络(Convolutional Neural Networks )
    官网安装Python包太慢?教你三种下载安装方式-PiP、conda、轮子,教你三种Pytorch的下载安装方式,保证你再也不用出现Error
  • 原文地址:https://www.cnblogs.com/yangzhi/p/3576559.html
Copyright © 2011-2022 走看看