Velocity实例

vm文件,文件名为dao.vm

内容如下

 

package com.packagename.dao;

import java.io.Serializable;
import java.util.List;

import com.packagename.entity.$XxxxEntity;
import com.packagename.exception.SomeException;

import org.hibernate.Query;


/**
 * 
 */
public class $XxxxEntityDao extends AbstractDao {


    /**
     * Constructor
     */
    public $XxxxEntityDao() {
    }
   
    /**
     * 
     * @see com.packagename.dao.AbstractDao#get
     */
    @Override
    public Object get(Serializable id) throws SomeException {
        return super.get(id);
    }
   
    /**
     * 
     * @see com.packagename.dao.AbstractDao#delete
     */
    public void delete($XxxxEntity entity) throws SomeException {
        super.delete(entity);
    }
   
    /**
     * 
     * @see com.packagename.dao.AbstractDao#save
     */
    public void save($XxxxEntity entity) throws SomeException {
        super.save(entity);
    }
   
    /**
     * insert or update $XxxxEntity.
     * @param entity $XxxxEntity
     * @throws SomeException Database Exception
     */
    public void saveOrUpdate($XxxxEntity entity) throws SomeException {
        super.saveOrUpdate(entity);
    }
   
    /**
     * update $XxxxEntity
     * @param entity $XxxxEntity
     * @throws SomeException Database Exception
     */
    public void update($XxxxEntity entity) throws SomeException {
        super.update(entity);
    }

    /**
     * refresh
     * @param entity $XxxxEntity
     * @param lock lock or not
     * @throws SomeException Database Exception
     */
    public void refresh($XxxxEntity entity, boolean lock) throws SomeException {
        super.refresh(entity, lock);
    }
}

DAO生成器类DaoGenerator

代码如下

 

package com.test.velocity;

import java.io.File;
import java.io.FileWriter;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class DaoGenerator
{
    private static final String[] DAOS = { "User", "UserRole" };

    private static final String OUTPUT_DIR = "d:/dao/";
    private static final String EXT = ".java";


    public static void main(String[] args) throws Exception
    {
        // make dirs if not exists
        File dir = new File(OUTPUT_DIR);
        dir.mkdirs();
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        Properties p = new Properties();
        p.setProperty("resource.loader", "class");
        p
            .setProperty("class.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        p.setProperty("input.encoding", "UTF-8");
        //Velocity.init(p);
        ve.init(p);

        for (String entityName : DAOS)
        {
            FileWriter fw = new FileWriter(new File(dir, entityName + "Dao"
                    + EXT));

            // Set Velocity Context
            VelocityContext context = new VelocityContext();
            context.put("XxxxEntity", entityName);
            context.put("XxxxEntityDao", entityName + "Dao");

            Template template = ve.getTemplate("conf/dao.vm", "UTF-8");
            template.merge(context, fw);

            fw.flush();
            fw.close();
        }
    }

}

 

此为较为简单的代码生成工具类,需要velocity-1.6.3.jar,commons-collections-3.2.1.jar,commons-lang-2.4.jar,commons-logging-1.1.1.jar工具包

调试运行成功后将在d:/dao/下生成UserDao.java和UserRoleDao.java两个文件

 

 

你可能感兴趣的:(velocity)