实验步骤:
1.设计domain对象User。
2.设计UserDao接口。
3.加入hibernate.jar和其依赖的包。
4.编写User.hbm.xml映射文件,可以基于hibernate/eg目录下的org/hibernate/auction/User.hbm.xml修改。
5.编写hibernate.cfg.xml配置文件,可以基于hibernate/etc/hibernate.cfg.xml修改;必须提供的几个参数:
connection.driver_class、connection.url、connection.username、connection.password、dialect、hbm2ddl.auto。
6.编写HibernateUtils类,主要用来完成Hibnerate初始化和提供一个获得Session的方法;这步可选。
7.实现UserDao接口。
package com.eric.hibernate.Domain;
import java.util.Date;
public class User {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
private int id;
private String name;
private Date birthday;
}
package com.eric.hibernate.Dao;
import com.eric.hibernate.Domain.User;
public interface UserDAO {
public void saveUser(User user);
public User findUserByName(String name);
public User findUserById(int id);
public void updateUser(User user);
public void removeUser(User user);
}
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.eric.hibernate.Domain"> <class name="User" table="user"> <id name="id"> <generator class="native" /> </id> <property name="name" unique="true"/> <property name="birthday"/> </class> </hibernate-mapping>
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate</property> <property name="connection.username">root</property> <property name="connection.password">789+</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 自动建表 --> <property name="hbm2ddl.auto">create</property> <!-- 控制台打印出SQL语句 --> <property name="show_sql">true</property> <!-- <class-cache class="com.eric.hibernate.Domain.User" usage="read-only"/> --> <mapping resource="com/eric/hibernate/Domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
package com.eric.Utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public final class hibernateUtil {
private static SessionFactory sessionFactory;
private hibernateUtil(){
}
static{
// 寻找配置文件,进行初始化
Configuration cfg = new Configuration();
// 可以在此指定配置文件的位置
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
// 获取sessionFactory的方法
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
// 获取session的方法
public static Session getSession(){
return sessionFactory.openSession();
}
}
package com.eric.hibernate.Dao.Impl;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.eric.Utils.hibernateUtil;
import com.eric.hibernate.Dao.UserDAO;
import com.eric.hibernate.Domain.User;
public class UserDAOImpl implements UserDAO {
// 保存用户
public void saveUser(User user) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.save(user);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
// 根据name查找用户
public User findUserByName(String name) {
Session s = null ;
try{
s = hibernateUtil.getSession();
String hql = "from User as user where user.name=:name";
Query query = s.createQuery(hql);
query.setString("name", name);
User user = (User) query.uniqueResult();
return user;
}finally{
if(s != null){
s.close();
}
}
}
// 根据id查找用户
public User findUserById(int id) {
Session s = null ;
try{
s = hibernateUtil.getSession();
User user = (User) s.get(User.class, id);
return user;
}finally{
if(s != null)
{
s.close();
}
}
}
// 更新用户
public void updateUser(User user) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.update(user);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
// 删除用户
public void removeUser(User user) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(user);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
}
附加:log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appender1"
class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logfile08.html" />
<param name="MaxFileSize" value="1MB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.HTMLLayout">
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="appender1" />
</root>
</log4j:configuration>