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的映射文件,由 ...
随机推荐
- BackTrack5-r3汉化
进入BT系统图形模式,将语言包1和2拖进BT图形桌面. 所需文件包地址:http://pan.baidu.com/s/1i3ouc9v(64位更新包)将语言包1里的全部文件复制粘贴到:/var/cac ...
- PHP 爬虫
1.爬虫的本质简单来说,就是读取页面源代码,然后用正则匹配得到想要的数据. 示例如下: private function spider_jiuyou_list($listname,$url) { ...
- JS原生效果瀑布流布局的实现(一)
JS原生效果 实现: HTML页面布局: <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...
- 【JS】falsy与truthy
1.Falsy值,当进行逻辑判断时均为false(如!!false==false).六个Falsy值:false.undefined.null.正负0.NaN."". 2.其余所有 ...
- 《剑指offer》面试题11: 数值的整数次方
面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...
- 【转】oracle in和exists、not in和not exists原理和性能探究
转自http://www.2cto.com/database/201310/251176.html 对于in和exists.not in和not exists还是有很多的人有疑惑,更有甚者禁用not ...
- Java实验三
20145113 20145102实验三 实验步骤 编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性 ...
- CSS hack方式一览【转】
做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...
- 用eclipse碰到的一些错误,然后自己去网上找的解决办法
错误一: [Please check logcat output for more details.Launch canceled! 解决办法:在配置文件:AndroidManifest.xml加入如 ...
- 计算机启动boot
原创博文:转载请标明出处:http://www.cnblogs.com/zxouxuewei 零.boot的含义 先问一个问题,"启动"用英语怎么说? 回答是boot.可是,boo ...