Hibernate反向工程生成DAO
通过Hibernate反向工程生成个DAO:
package dao; import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* A data access object (DAO) providing persistence and search support for
* Student entities. Transaction control of the save(), update() and delete()
* operations can directly support Spring container-managed transactions or they
* can be augmented to handle user-managed Spring transactions. Each of these
* methods provides additional information for how to configure it for the
* desired type of transaction control.
*
* @see dao.Student
* @author MyEclipse Persistence Tools
*/ public class StudentDAO extends BaseHibernateDAO {
private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
// property constants
public static final String SNAME = "sname";
public static final String EMAIL = "email"; public void save(Student transientInstance) {
log.debug("saving Student instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} public void delete(Student persistentInstance) {
log.debug("deleting Student instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} public Student findById(java.lang.Integer id) {
log.debug("getting Student instance with id: " + id);
try {
Student instance = (Student) getSession().get("dao.Student", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} public List findByExample(Student instance) {
log.debug("finding Student instance by example");
try {
List results = getSession().createCriteria("dao.Student")
.add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} public List findByProperty(String propertyName, Object value) {
log.debug("finding Student instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Student as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findBySname(Object sname) {
return findByProperty(SNAME, sname);
} public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
} public List findAll() {
log.debug("finding all Student instances");
try {
String queryString = "from Student";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} public Student merge(Student detachedInstance) {
log.debug("merging Student instance");
try {
Student result = (Student) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Student instance) {
log.debug("attaching dirty Student instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public void attachClean(Student instance) {
log.debug("attaching clean Student instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
以及相应的studentPO.java
package dao; /**
* Student entity. @author MyEclipse Persistence Tools
*/
public class Student extends AbstractStudent implements java.io.Serializable { // Constructors /** default constructor */
public Student() {
} /** full constructor */
public Student(String sname, String email) {
super(sname, email);
} }
package dao; /**
* AbstractStudent entity provides the base persistence definition of the
* Student entity. @author MyEclipse Persistence Tools
*/ public abstract class AbstractStudent implements java.io.Serializable { // Fields private Integer sid;
private String sname;
private String email; // Constructors /** default constructor */
public AbstractStudent() {
} /** full constructor */
public AbstractStudent(String sname, String email) {
this.sname = sname;
this.email = email;
} // Property accessors public Integer getSid() {
return this.sid;
} public void setSid(Integer sid) {
this.sid = sid;
} public String getSname() {
return this.sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getEmail() {
return this.email;
} public void setEmail(String email) {
this.email = email;
} }
package dao; import org.hibernate.Session; /**
* Data access interface for domain model
* @author MyEclipse Persistence Tools
*/
public interface IBaseHibernateDAO {
public Session getSession();
}
package dao; import hibernate.HibernateSessionFactory;
import org.hibernate.Session; /**
* Data access object (DAO) for domain model
* @author MyEclipse Persistence Tools
*/
public class BaseHibernateDAO implements IBaseHibernateDAO { public Session getSession() {
return HibernateSessionFactory.getSession();
} }
Hibernate反向工程生成DAO的更多相关文章
- 用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean)
用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean) 安装: 在help中eclise marksplace中查询JBo ...
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件. 1.相关文 ...
- hibernate反向工程 (eclipse和myeclipse)(转)
hibernate反向工程 (eclipse和myeclipse) 如何提取数据库的模式信息,想通过hibernate的反向工具尝试下. 一.myeclipse下hibernate反向工程: 1.选择 ...
- 小学生之使用Mybatis反向生成dao,entity,xml
本小学生刚进公司的时候,就一顿装逼,不管别人问我啥我都会说:"会"!毕竟在公司吗,什么都要装,不要别人看出你的底细.不过有一天,听说用Mybatis可以反向生成dao(第一次听说) ...
- 使用Mybatis-Generator自动生成Dao、Model、Mapping
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件. 1.相关文 ...
- hibernate反向工程 (eclipse和myeclipse)【转】
myeclipse下hibernate反向工程: 1.选择myeclipse hibernate视图 2.建立与后台数据库的连接 1).configure database driver: 2).添加 ...
- 【原创】Hibernate自动生成(1)
本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正. 开发环境: Eclipse Version: Photon ...
- 【原创】Hibernate自动生成(2)
本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正. 开发环境: Eclipse Version: Photon ...
- 10.使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
出处:http://www.cnblogs.com/lichenwei/p/4145696.html Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由 ...
随机推荐
- linux命令:df
1.命令介绍: df用来检测磁盘空间占用情况. 2.命令格式: df [选项] 文件 3.命令参数: 必要参数: -a 全部文件系统列表 -h 方便阅读方式显示 -H 等于“-h”,但是计算式,1K= ...
- Android自定义权限和使用权限
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 自定义权限,主要用于保护被赋予权限的组件.如无权限与有权限,正如public与private的对类保 ...
- Flash动画
Flash (交互式矢量图和Web动画标准) Flash是由macromedia公司推出的交互式矢量图和 Web 动画的标准,由Adobe公 司收购.做Flash动画的人被称之为闪客.网页设计者使用 ...
- python-tab还是space?
今天把windows下的python代码传到服务器上,结果莫名其妙的报了一堆indent的错误 网上建议说用: python -m tabnanny filename.py 查一下 然后就用space ...
- 安装go语言,配置环境及IDE,只需3步
安装go语言,配置环境及IDE,只需3步 ( 欢迎加入go语言群: 218160862 , 群内有实践) 第1.下载 go压缩包,解压 ,如果你是window系统,请选择go1.5.windows ...
- 第三个Sprint冲刺第九天
讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:做最后的工作
- Linux学习之路—Linux文件权限
内容来源于鸟哥私房菜 1.Linux文件属性 1)第一列为文件类型与权限 第一个字符表示文件的类型: [d]表示目录 [-]表示文件 [l]表示连接文件 [b]表示设备文件中可供存储的接口设备,例如硬 ...
- The Swift Programming Language 中文翻译版(个人翻新随时跟新)
The Swift Programming Language --lkvt 本人在2014年6月3日(北京时间)凌晨起来通过网络观看2014年WWDC 苹果公司的发布会有iOS8以及OS X 10.1 ...
- C++学习笔记27:异常处理机制
一.异常处理机制基础 异常的定义 程序中可以检测的运行不正常的情况 异常处理的基本流程 某段程序代码在执行操作时发生特殊情况,引发一个特定的异常 另一段程序代码捕获该异常并处理它 二.异常的引发 th ...
- Apache2 部署 Django
环境: debian8 apache2.4.10 #请注意自己的apache版本,不同版本配置文件结构差异很大 django1.10 python3.4 默认ap ...