动软生成的WCP DAO层模板(不使用接口)

本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正。 

开发环境: Eclipse Version: Photon Milestone 6

WCP:http://www.wcpdoc.com/home/Pubindex.html

 

目录:

Hibernate自动生成(1)

Hibernate自动生成(2)

动软生成的WCP DAO层模板(不使用接口)

 

 

<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
     TableHost host = (TableHost)(Host);         
     string s=host.GetModelClass(host.TableName);
     string ClassName =s.Substring(0,1).ToUpper()+s.Substring(1).ToLower(); //根据表名首字母大写,其他小写
     string EntityName=ClassName.ToLower(); // 小写
     string sessionfactory="sessionFactorymssql"; //请检查此处的sessionFactory名字
     string packagename="com.farm.member.domain.Member"; //请检查此处的领域模型domain的包名 example:com.farm.doc.domain.Doc  
#>
package com.farm.<#= EntityName #>.Dao;

import java.math.BigInteger;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.core.sql.utils.HibernateSQLTools; 
 
import <#= packagename #>;  //注意这里的包名


/**
 * @author LuHui
 * 本Dao类由动软生成
 */
 
 @Repository 
 public class <#= ClassName #>Dao extends HibernateSQLTools<<#= ClassName #>>  {
    @Resource(name = "<#= sessionfactory #>")//此处的资源名称请核对
    private SessionFactory sessionFactory;
   
    //
    public <#= ClassName #> insertEntity(<#= ClassName #> <#= EntityName #>) {
        Session session = getSession();
        session.save(<#= EntityName #>);
        return <#= EntityName #>;
    }
    
    //
    public void deleteEntity(<#= ClassName #> <#= EntityName #>) {
        Session session = getSession();
        session.delete(<#= EntityName #>);
    }
    
    //
    public void editEntity(<#= ClassName #> <#= EntityName #>) {
        Session session = getSession();
        session.update(<#= EntityName #>);
    }
    
    //查:返回一共几条记录,不带条件
    public int getAllListNum() {
        Session session = getSession();
        SQLQuery sqlquery = session.createSQLQuery("select count(*) from <#= ClassName #>");
        BigInteger num = (BigInteger) sqlquery.list().get(0);
        return num.intValue();
    }
    
    
  
    
    //查:根据主键id查询返回一个实体,注意:这里的id名字无所谓的,只是一个形参
    //请注意这里的id的类型,必须是和你领域层模型的@id 类型一致
    public <#= ClassName #> getEntity(int id) {
        Session session = getSession();
        return (<#= ClassName #>) session.get(<#= ClassName #>.class, id);
    }
    
    
    //查:根据DataQuery查询
    public DataResult runSqlQuery(DataQuery query) {
    
        try {
            return query.search(getSession());
        } catch (Exception e) {
            return null;
        }
    }
    
    //下面是带条件的查询,返回数量,请自定义条件
    /*
    public Integer get<#= ClassName #>sNum() {
        Session session = getSession();
        SQLQuery sqlquery = session.createSQLQuery("select count(*) from <#= ClassName #> where STATE=1");
        BigInteger num = (BigInteger) sqlquery.list().get(0);
        return num.intValue();
    }
    */

    //----------以下几个根据DBRule实现增删改查---------------------------
    
    //删:根据DBRules删除
    public void deleteEntitys(List<DBRule> rules) {
        
        deleteSqlFromFunction(getSession(), rules);
    }
        
    //查询:根据DBRules查询
    public List<<#= ClassName #>> selectEntitys(List<DBRule> rules) {
        
        return selectSqlFromFunction(getSession(), rules);
    }

    //更新:根据DBRules更新
    public void updataEntitys(Map<String, Object> values, List<DBRule> rules) {
        
        updataSqlFromFunction(getSession(), values, rules);
    }

    //查:返回一共几条记录,不带条件
    public int countEntitys(List<DBRule> rules) {
        
        return countSqlFromFunction(getSession(), rules);
    }
   
    //----------以上几个根据DBRule实现增删改查-------------------------------------------------------

   
   
    
    //---SessionFactory   Getter  Setter----
  protected SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    // Current Session 
    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    
    //获得类型
    protected Class<<#= ClassName #>> getTypeClass() {
        return <#= ClassName #>.class;
    }
 
 
  
    
}

 

 生成的代码如下

package com.farm.member.Dao;

import java.math.BigInteger;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.core.sql.utils.HibernateSQLTools; 
 
import com.farm.member.domain.Member;  //注意这里的包名


/**
 * @author LuHui
 * 本Dao类由动软生成
 */
 
 @Repository 
 public class MemberDao extends HibernateSQLTools<Member>  {
    @Resource(name = "sessionFactorymssql")//此处的资源名称请核对
    private SessionFactory sessionFactory;
   
    //
    public Member insertEntity(Member member) {
        Session session = getSession();
        session.save(member);
        return member;
    }
    
    //
    public void deleteEntity(Member member) {
        Session session = getSession();
        session.delete(member);
    }
    
    //
    public void editEntity(Member member) {
        Session session = getSession();
        session.update(member);
    }
    
    //查:返回一共几条记录,不带条件
    public int getAllListNum() {
        Session session = getSession();
        SQLQuery sqlquery = session.createSQLQuery("select count(*) from Member");
        BigInteger num = (BigInteger) sqlquery.list().get(0);
        return num.intValue();
    }
    
    
  
    
    //查:根据主键id查询返回一个实体,注意:这里的id名字无所谓的,只是一个形参
    //请注意这里的id的类型,必须是和你领域层模型的@id 类型一致
    public Member getEntity(int id) {
        Session session = getSession();
        return (Member) session.get(Member.class, id);
    }
    
    
    //查:根据DataQuery查询
    public DataResult runSqlQuery(DataQuery query) {
    
        try {
            return query.search(getSession());
        } catch (Exception e) {
            return null;
        }
    }
    
    //下面是带条件的查询,返回数量,请自定义条件
    /*
    public Integer getMembersNum() {
        Session session = getSession();
        SQLQuery sqlquery = session.createSQLQuery("select count(*) from Member where STATE=1");
        BigInteger num = (BigInteger) sqlquery.list().get(0);
        return num.intValue();
    }
    */

    //----------以下几个根据DBRule实现增删改查---------------------------
    
    //删:根据DBRules删除
    public void deleteEntitys(List<DBRule> rules) {
        
        deleteSqlFromFunction(getSession(), rules);
    }
        
    //查询:根据DBRules查询
    public List<Member> selectEntitys(List<DBRule> rules) {
        
        return selectSqlFromFunction(getSession(), rules);
    }

    //更新:根据DBRules更新
    public void updataEntitys(Map<String, Object> values, List<DBRule> rules) {
        
        updataSqlFromFunction(getSession(), values, rules);
    }

    //查:返回一共几条记录,不带条件
    public int countEntitys(List<DBRule> rules) {
        
        return countSqlFromFunction(getSession(), rules);
    }
   
    //----------以上几个根据DBRule实现增删改查-------------------------------------------------------

   
   
    
    //---SessionFactory   Getter  Setter----
    protected SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    // Current Session 
    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    
    //获得类型
    protected Class<Member> getTypeClass() {
        return Member.class;
    }
 
 
  
    
}

 

 

 

posted @ 2018-05-11 15:16  Daniel_Lu  阅读(237)  评论(0编辑  收藏  举报