动软生成的WCP DAO层模板(不使用接口)
本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正。
开发环境: Eclipse Version: Photon Milestone 6
WCP:http://www.wcpdoc.com/home/Pubindex.html
目录:
Hibernate自动生成(1)
Hibernate自动生成(2)
<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
TableHost host = (TableHost)(Host);
string s=host.GetModelClass(host.TableName);
string ClassName =s.Substring(,).ToUpper()+s.Substring().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();
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;
} }
动软生成的WCP DAO层模板(不使用接口)的更多相关文章
- 动软 生成 linq相关DAO
第一步:新建自定义模板 <#@ template language="c#" HostSpecific="True" #> <#@ outpu ...
- springboot自动生成mysql的DAO层代码
springboot提供了强大丰富的整合内容,但是每次要写一堆dao层的xml或者数据库相关的配置代码的时候,还是挺繁琐又容易出错的. 可以用mybatis-generator自动生成代码: 只需要加 ...
- mybatis-generator生成model和dao层代码
.建立文件夹myibatisGen 2.下载mybatis-generator-core-1.3.1.jar或者其它版本的jar包,到myibatisGen文件夹下 3.为生成代码建立配置文件“gen ...
- MybatisGenerator生成SSM的dao层
官网下载 mybatis generator 下载generator的release版本mybatis-generator-core-1.4.0-bundle.zip https://github.c ...
- OracleHelper 动软生成
using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi ...
- 动软生成Model(dapper.common)
<#@ template language="c#" HostSpecific="True" #><#@ output extension= ...
- SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis
一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...
- asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(三)—— 简单登录页面+sqlite+动软代码生成器的使用
上一节讲到利用easyui的layout.tree.tab和datagrid创建用户管理的页面,注意利用到easyui的页面一定要按顺序添加jQuery和easyUI的.js和.css样式,灵活查看e ...
- DAO层,Service层,Controller层、View层 的分工合作
DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...
随机推荐
- Managed Media Aggregation using Rtsp and Rtp
his article was written almost 2 years ago, it's content may not reflect the latest state of the cod ...
- 补番计划 (长沙理工大学第十一届程序设计竞赛)(双端队列+set容器+string)
补番计划 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submissi ...
- activiti入门3排他网关,并行网管,包括网关,事件网关
网关用来控制流程的流向 网关能够消费也能够生成token. 网关显示成菱形图形,内部有有一个小图标. 图标表示网关的类型. 基本分支 首先 利用 流程变量 写个带有分支的一个基本流程 流程图: wa ...
- <转>windows下编译lua源码
因为之前一直使用 lua for windows 来搭建lua的使用环境,但是最新的 lua for windows 还没有lua5.2,我又想用这个版本的lua,所以被逼无奈只能自己编一下lua源码 ...
- iOS定位服务CoreLocation
欢迎訪问我的新博客: 开发人员说 基于LBS的应用开发是当今移动开发中的一大热门, 当中主要涉及到地图和定位两个方面. iOS开发中, 定位服务依赖于CoreLocation框架, CLLocatio ...
- Redis之SkipList数据结构
0.前言 Redis中有序集合zset需要使用skiplist作为存储数据结构, 关于skiplist数据结构描述可以查询wiki, 本文主要介绍Redis实现的skiplist的细节. 1.数据结构 ...
- <LeetCode OJ> 26 / 264 / 313 Ugly Number (I / II / III)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- RGB格式等比例缩放
原理为:将原始图像的每个像素通过一个比例关系式映射到相应的位置. /* lrgb: input 24bits rgb buffer srgb: output 24bits rgb buffer wid ...
- MySQL的索引(中)
连着发了几天文章,从我收到的反映来说,大家觉着还不错,可以很清晰的看到知识的脉络,但是这个还不错是针对传统的文章的无聊.不明确.完全不考虑考虑用户体验的角度上对比出来的.掌握一门知识还是不容易的,有的 ...
- MVC-Model
用模型取代字典理由: **使用字典的坏处 一般情况下,存入数据和取出数据都使用“字典类型的key”,编写这些key时,编译时不会有任何的友善提示,需要手敲,容易出错. dict[@“name”] = ...