• 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;
        } 
      
    }  



  • 相关阅读:
    SpringBoot 项目瘦身
    对比两个文本的异同
    Spring 事务不起作用的场景
    Controller 层数据校验实现思路
    Notify 类的实现思路
    backup: 使用 vim 时一定会用到的设置 --for-myself
    exercise: 反射获取指定的属性值 --CSharp
    exercise: 序列化和反序列化Xml --CSharp
    前缀
    华罗庚的数学思想
  • 原文地址:https://www.cnblogs.com/yangzhi/p/3576559.html
Copyright © 2020-2023  润新知