动软生成的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的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...
随机推荐
- Java中Vector与ArrayList的差别具体解释
首先看这两类都实现List接口,而List接口一共同拥有三个实现类.各自是ArrayList.Vector和LinkedList.List用于存放多个元素,可以维护元素的次序,而且同意元素的反复. 3 ...
- hibernate学习系列-----(6)hibernate对集合属性的操作之Set集合篇
先说一段废话吧,本打算每天把所学的知识总结为博客的,但是昨天为什么没有写呢?没有学习吗?No,那是为什么?贪玩,对,这位同学说对了,老实说昨天感觉身体不怎么舒服,大家都知道,这其实就是为自己懒找借口, ...
- Win7如何开启管理员账户
打开运行对话框,在LUSRMGR.MSC里,左边点用户,在右边栏里右击Administrator选择属性,去掉账户已禁用这个选项前面的勾.我也问过这个问题,确实如版主说的这样可解决这个问题,但有个问题 ...
- 用C#封装的ServiceStack.redis操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- cocos2d-x OpenGL ES 坐标系总结
很多教程都说cocos2d-x OpenGL ES世界坐标系原点在左下角,但至于为什么在左下角却从来没有人提过,这导致大部分人觉得这是OpenGL ES的规定,事实上这是错的.OpenGL ES的坐标 ...
- mac下配置adb
博主近期搞了台macbook用,搞android开发爽多了.程序编译那个速度确实让我感到非常爽.尤其是在之前用windows时动辄启动eclipse几分钟,编译又花非常久的情况下,可是用了mac发现a ...
- Maven 命令行创建项目时 Could not find goal ‘create’ in plugin org.apache.maven.plugins:...
使用maven3.3.9 版本,进行命令行创建项目时输入以下命令创建失败 mvn archetype:create -DgroupId=com.zang.maven -DartifactId=sys ...
- iOS开发-使用代码退出应用程序,带动画。
有时候我们需要使用代码中断程序,如果直接调用exit方法,会使得程序就像是崩溃那样,因此我们应该加上一个动画效果. 例如: AppDelegate *app = [UIApplication shar ...
- 导入解析excel小结
导入解析excel小结 控制器例子:
- windows操作系统记事本保存操作时间、字符映射表的打开、步骤记录器使用
记事本自动记录修改时间 你有用记事本记账或写日记的习惯吗?其实在记事本的文档开头输入".LOG"(无引号,字母为大写),之后记录内容并保存,这样以后打开就会看到之前每次修改的时间了 ...