/**
*
*/
package hibernate.test;
import java.util.Date;
/**
* @author huang
*
Oct 18, 2013
3:01:08 PM
User.java
*/
public class UserTest {
private Integer id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
package hibernate.test;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
query(10);
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
// SchemaExport export = new SchemaExport(cfg);
// export.create(true, true);
//建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//取得session
Session session = null;
try {
session = factory.openSession();
//开启事务
session.beginTransaction();
UserTest user = new UserTest();
user.setName("张三121");
user.setPassword("121123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存User对象
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}
}
static void query(int i){
Session s=null;
try{
s = HibernateUtil.currentSession();
//from后面是对象,不是表名
String hql="from UserTest as ut where ut.id=:id";//使用命名参数,推荐使用,易读。
Query query=s.createQuery(hql);
query.setInteger("id", i);
List<UserTest> list=query.list();
for(UserTest ut:list){
System.out.println(ut.getName());
}
}finally{
if(s!=null)
s.close();
}
}
}
<?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>
<class name="hibernate.test.UserTest">
<id name="id" column="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">SEQ_SO</param>
</generator>
</id>
<property name="name" />
<property name="password" />
<property name="createTime" />
<property name="expireTime" />
</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="show_sql">true</property> <property name="hibernate.connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="hibernate.connection.url"> jdbc:oracle:thin:@10.101.2.3:1521:develope </property> <property name="hibernate.connection.username">dpcds_newnew</property> <property name="hibernate.connection.password">dpcds_newnew</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <mapping resource="test.hbm.xml" /> </session-factory> </hibernate-configuration>
操作数据库太方便了,记录一下,目前只有单表。
package hibernate.test;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.io.File;
/**
*
* <p>
* Title: WMDS
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2005
* </p>
* <p>
* Company: ECLINK
* </p>
*
* @author
* @version 1.0
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
// System.out.println("-----------------------------");
// File f = new File(
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
// log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
public static void save(Object object) throws Exception {
// 开始 获得hibernate环境 sessioin
Session session = HibernateUtil.currentSession();
// 开始 获得hibernate环境 transaction 事物
Transaction tx = session.beginTransaction();
try {
session.save(object);
// 事物提交
tx.commit();
// 关闭环境
HibernateUtil.closeSession();
} catch (Exception ex) {
tx.rollback();
HibernateUtil.closeSession();
throw ex;
}
}
public static Object load(Class class0, String id) throws Exception {
// 开始 获得hibernate环境 sessioin
Session session = HibernateUtil.currentSession();
// 开始 获得hibernate环境 transaction 事物
Transaction tx = session.beginTransaction();
try {
Object o = session.load(class0, id);
// 事物提交
tx.commit();
// 关闭环境
HibernateUtil.closeSession();
return o;
} catch (Exception ex) {
tx.rollback();
HibernateUtil.closeSession();
throw ex;
}
}
public static void delete(Object object) throws Exception {
// 开始 获得hibernate环境 sessioin
Session session = HibernateUtil.currentSession();
// 开始 获得hibernate环境 transaction 事物
Transaction tx = session.beginTransaction();
try {
session.delete(object);
// 事物提交
tx.commit();
// 关闭环境
HibernateUtil.closeSession();
} catch (Exception ex) {
tx.rollback();
HibernateUtil.closeSession();
throw ex;
}
}
public static void update(Object object) throws Exception {
// 开始 获得hibernate环境 sessioin
Session session = HibernateUtil.currentSession();
// 开始 获得hibernate环境 transaction 事物
Transaction tx = session.beginTransaction();
try {
session.update(object);
// 事物提交
tx.commit();
// 关闭环境
HibernateUtil.closeSession();
} catch (Exception ex) {
tx.rollback();
HibernateUtil.closeSession();
throw ex;
}
}
public static void saveOrUpdate(Object object) throws Exception {
// 开始 获得hibernate环境 sessioin
Session session = HibernateUtil.currentSession();
// 开始 获得hibernate环境 transaction 事物
Transaction tx = session.beginTransaction();
try {
session.saveOrUpdate(object);
// 事物提交
tx.commit();
// 关闭环境
HibernateUtil.closeSession();
} catch (Exception ex) {
tx.rollback();
HibernateUtil.closeSession();
throw ex;
}
}
/**
* 调用例子
*
* @param arg
* String[]
*/
public static void main(String[] arg) {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文介绍了一个基于Hibernate实现的用户管理系统的实例,包括User类的设计、客户端操作及Hibernate配置等。展示了如何通过Hibernate进行用户信息的增删改查。
2943

被折叠的 条评论
为什么被折叠?



