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的映射文件,由 ...
随机推荐
- softwareTesting_work1
1.12306手机APP软件测评 首先是软件界面,iPhone版本和android版本长得是一模一样,虽然是注重功能的软件,但是一样样的界面让人完全感受不到软件设计者的诚意啊. 还有就是软件图片和图标 ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- sql2000不能远程注册服务器上sql2000的解决方法
1. 开始——cmd——telnet Ip 1433 看1433端口是否打开 2.在服务器上查询分析器中输入select @@version查看sql2000的版本,版本号在8.0.2039以下的都 ...
- HowTo系列之virtualenv
1. 简介 virtualenv是一个用于创建Python独立运行环境的命令行工具. 在我们使用python进行开发的时候,我们会依赖不同的Python版本和不同的库版本.当我们的项目需要依赖别的开发 ...
- word2vec + transE 知识表示模型
本文主要工作是将文本方法 (word2vec) 和知识库方法 (transE) 相融合作知识表示,即将外部知识库信息(三元组)加入word2vec语言模型,作为正则项指导词向量的学习,将得到的词向量用 ...
- android 中theme和style的语法相关
1.theme和style都是一组属性的集合,用于定义文本.颜色.大小等显示风格.他们都是资源,可以用android系统级别的一些默认的风格和主题资源,你也可以自定义你自己的主题和风格资源. 2.自定 ...
- 转载《Android-TabHost 选项卡功能用法详解》
一. TabHost介绍 TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计; 1. TabHost常用组件 TabWidget : 该组件就是TabHost标签页中上部 ...
- CentOS7 搭建python3 Django环境
yum install gcc yum install make yum install openssl-devel -y yum install sqlite-devel -y wget https ...
- html选择图片后预览,保存并上传
html代码:------------------添加-------------------------- accept="image/gif,image/jpeg,image/jpg,im ...
- Android 的 DatePicker、TimePicker或NumberPicker
布局文件加上这个就可以,去除日期选择器.时间选择器或数值选择器的可编辑状态. android:descendantFocusability="blocksDescendants" ...