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
@Override
public 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 class="org.savior.Person" />
</session-factory>
</hibernate-configuration>



一个简单的Hibernate应用就完成了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值