Hibernate



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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值