public class HibernateUtil {
private static SessionFactory sf = null;
static{
//读取hibernate.cfg.xml这个代码要求hibernate.cfg.xml必须在src目录下
Configuration cfg = new Configuration().configure();
//创建sessionFactory
sf = cfg.buildSessionFactory();
}
public static Session getSession(){
return sf.openSession();
}
}
package cn.com.ambow.ects.dao.hibernate.impl;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.com.ambow.ects.dao.IUserDao;
import cn.com.ambow.ects.entity.User;
import cn.com.ambow.ects.exception.ManagerUserException;
import cn.com.ambow.hibernate.util.HibernateUtil;
public class UserDaoHibernateImpl implements IUserDao{
public void add(User user) throws ManagerUserException {
//获得session对象
Session session = null;
Transaction tran = null;
try{
session = HibernateUtil.getSession();
//开始事务
tran = session.beginTransaction();
//do some work
session.save(user);
tran.commit();
}catch(Exception e){
e.printStackTrace();
tran.rollback();
}finally{
if(session != null) session.close();
}
}
public User findById(Integer id) throws ManagerUserException {
Session session = null;
Transaction tran = null;
User user = null;
Query query = null;
try{
session = HibernateUtil.getSession();
tran = session.beginTransaction();
String hql = "from User user where user.id=?";
query = session.createQuery(hql);
query.setInteger(0, id);
user = (User)query.uniqueResult();
tran.commit();
}catch(Exception e){
e.printStackTrace();
tran.rollback();
}finally{
if(session != null) session.close();
}
return user;
}
public User login(String name, String passwd) throws ManagerUserException {
Session session = null;
Transaction tran = null;
User user = null;
Query query = null;
try{
session = HibernateUtil.getSession();
//开始事务
tran = session.beginTransaction();
//do some work
//hql sql select * from test_user
//sql select * from test_user where name=? and password=?
//hql select user from User user where user.name=? and user.password=?
String hql ="from User user where user.name=? and user.passwd=?";
query = session.createQuery(hql);
query.setString(0,name);
query.setString(1,passwd);
user = (User) query.uniqueResult();
tran.commit();
}catch(Exception e){
e.printStackTrace();
tran.rollback();
}finally{
if(session != null) session.close();
}
return user;
}
public void update(User user) throws ManagerUserException {
Session session = null;
Transaction tran = null;
Query query = null;
try{
session = HibernateUtil.getSession();
tran = session.beginTransaction();
session.update(user);
tran.commit();
}catch(Exception e){
e.printStackTrace();
tran.rollback();
}finally{
if(session != null) session.close();
}
}
public static void main(String[] args) {
IUserDao dao = new UserDaoHibernateImpl();
User user = null;
//user.setName("zhang");
//user.setPasswd("12345");
//dao.add(user);
// user = dao.login("zhang", "12345");
// System.out.println(user.getName());
// user = dao.findById(100);
// System.out.println(user.getName());
user = new User();
user.setId(100);
user.setName("chen");
user.setPasswd("12321");
dao.update(user);
}
}
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/ects</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- 配置数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">false</property>
<mapping resource="cn/com/ambow/ects/hbm/xml/User.hbm.xml"/>
<mapping resource="cn/com/ambow/ects/hbm/xml/Product.hbm.xml"/>
<mapping resource="cn/com/ambow/ects/hbm/xml/Order.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?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="cn.com.ambow.ects.entity">
<class name="User" table="user">
<!-- name代表的属性名 column对应表列名 -->
<id name="id" column="user_id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="passwd" column="passwd"></property>
</class>
</hibernate-mapping>