首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibernate学习札记(简单的Hibernate环境搭建)

2012-09-03 
Hibernate学习笔记(简单的Hibernate环境搭建)导入Hibernate3.3环境所需的架包.建立Person类(使用注解方式)

Hibernate学习笔记(简单的Hibernate环境搭建)
导入Hibernate3.3环境所需的架包.
建立Person类(使用注解方式):

public class Person {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)@Column(name="ID")private Integer id;@Column(name="NAME",length=100,nullable=false)private String name;@Column(name="DATE",nullable=false)private Date date;        public Person(Integer id, String name, Date date) {this.id = id;this.name = name;this.date = date;}        //get/set@Overridepublic String toString() {return id + "," + name + "," + date;}}


实现类(接口不贴了):
package org.savior;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;public class PersonDaoImpl implements PersonDao {private SessionFactory sessionFactory;public PersonDaoImpl() {Configuration cg = new AnnotationConfiguration().configure();sessionFactory = cg.buildSessionFactory();}public void delete(Person person) {Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();try {tx.begin();session.delete(person);tx.commit();} catch (RuntimeException e) {tx.rollback();throw e;} finally {session.close();}}@SuppressWarnings("unchecked")public List<Person> findAll() {Session session = sessionFactory.openSession();try {Query q = session.createQuery("from Person");return q.list();} finally {session.close();}}public Person findById(int personId) {Session session = sessionFactory.openSession();return (Person) session.get(Person.class, personId);}public void save(Person person) {Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();try {tx.begin();session.saveOrUpdate(person);tx.commit();} catch (RuntimeException e) {tx.rollback();throw e;} finally {session.close();}}public void update(Person person) {Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();try {tx.begin();session.update(person);tx.commit();} catch (RuntimeException e) {tx.rollback();throw e;} finally {session.close();}}}


测试:
package org.savior;import java.util.GregorianCalendar;import java.util.Iterator;import java.util.List;public class textHibernate {/** * @param args */public static void main(String[] args) {PersonDao personDao = new PersonDaoImpl();Person person = new Person();person.setName("savior");person.setDate(new GregorianCalendar(2011, 1, 1).getTime());personDao.save(person);List<Person> persons = personDao.findAll();for(Iterator<Person> it=persons.iterator();it.hasNext();){person=(Person)it.next();System.out.println(person);}Integer id=persons.get(0).getId();person=personDao.findById(id);System.out.println(person);//personDao.delete(person);person.setName("puppy");personDao.update(person);System.out.println(person);}}

hibernate.cgf.xml
<?xml version='1.0' encoding='UTF-8'?>  <!DOCTYPE hibernate-configuration PUBLIC             "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  <hibernate-configuration>      <session-factory>          <property name="connection.driver_class">com.mysql.jdbc.Driver</property>          <property name="connection.url">jdbc:mysql://localhost:3307/test</property>          <property name="connection.username">root</property>          <property name="connection.password">123</property>          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>          <property name="show_sql">true</property>          <property name="hbm2ddl.auto">update</property>          <mapping />      </session-factory>  </hibernate-configuration>  


一个简单的Hibernate应用就完成了

热点排行