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的映射文件,由 ...
随机推荐
- 【Python】str类方法说明
#capitalize():字符串首字符大写 string = 'this is a string.'new_str = string.capitalize()print(new_str)#输出:Th ...
- XML代码生成器——XMLFACTORY 简介(一)
XML代码生成器——XMLFACTORY 简介(一) 软件开发中经常要和第三方应用交互数据,特别是在银行.电信行业,这种需求更是必不可少,往往一个系统要和三五个其它系统交互数据,而数据交换的报文经常采 ...
- 模拟Post请求
此文摘自csdn青山的博客地址:http://blog.csdn.net/a497785609/article/details/6437154 本人随笔只为方便自己查阅,也为广大网友提供方便,不喜勿喷 ...
- 10 条有趣的 Linux 命令
在终端工作是一件很有趣的事情.今天,我们将会列举一些有趣得为你带来欢笑的Linux命令. 1. rev 创建一个文件,在文件里面输入几个单词,rev命令会将你写的东西反转输出到控制台. # rev & ...
- C语言文件处理
数据存储方式: 数据->变量->文件 数据 10个学生的信息: #define N 10 struct student { char stu_num[15]; char stu_name[ ...
- I.MX6 initramfs.cpio.gz.uboot unpack
/********************************************************************************* * I.MX6 initramfs ...
- maven - Eclipse构建maven项目
前面的博文已经介绍了如何安装maven,本文将记录如何在Eclipse下构建maven项目. 一.Eclipse maven插件安装 关于安装Eclipse maven插件,网上有很多方法,这里推荐一 ...
- C#中的Excel操作【1】——设置Excel单元格的内容,打开Excel文件的一种方式
前言 作为项目管理大队中的一员,在公司里面接触最多的就是Excel文件了,所以一开始就想从Excel入手,学习简单的二次开发,开始自己的编程之路! 程序界面 功能说明 打开文件按钮,可以由使用者指定要 ...
- 可能是Mac环境变量恢复的参考
因为要做物联网实验的缘故,于是在Mac上用Android Studio想导入SensorSimulator的demo项目. 根据SensorSimulator的相关说明,需要先将Sensor Simu ...
- JavaScript DOM 编程艺术·setInterval与setTimeout的动画实现解析
先贴上moveElement()函数的大纲,为了方便观看,删了部分代码,完整版粘到文章后面. function moveElement(elementID,final_x,final_y,interv ...