Eclipse3M7+Hibernate2 运行环境测试

package springWebwork2;

import net.sf.hibernate.*;
import org.apache.commons.logging.*;
import java.util.*;
import java.io.*;
import net.sf.hibernate.cfg.Configuration;

[i][color=darkblue]import showHelper.block.*;[/color][/i]

public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
Configuration conf= new Configuration().configure(new File("hibernate.cfg.xml"));
sessionFactory = conf.buildSessionFactory();
} catch (Throwable ex) {
log.error("创建SessionFactory失败:", ex);
throw new ExceptionInInitializerError(ex);
}
}

//使用ThreadLocal实现线程安全
public static final ThreadLocal LocalSession = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) LocalSession.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
LocalSession.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) LocalSession.get();
LocalSession.set(null);
if (s != null)
s.close();
}
//测试代码
public static void main(String[] args) throws Exception {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
//创建数据对象并插入表
Block B = new Block();
//B.setBlockname("浑南新区");
//B.setShoworder(0);
//相当于insert
//session.save(B);

//用list方法查询数据
String Sql="from Block as block1 order by block1.Showorder";
Query q = session.createQuery(Sql); //用对象类名,而非表名
List l = q.list();//返回一个List接口,用来遍历结果集

for (int i = 0; i < l.size(); i++) {
[i][color=darkblue]B = (Block) l.get(i);[/color][/i]
[i][color=darkblue]System.out.println(B.getId()+" "+B.getBlockname());[/color][/i] }
//更新数据
//Query qq = session.createQuery("from SimpleUser");
//B = (Block) session.load(Block.class, new Integer(1));
//B.setBlockname("其它");
//tx.commit();
//关闭
HibernateUtil.closeSession();
}
}
----------------------------------------------------------------------------------
配置文件 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">


<hibernate-configuration>
<session-factory>
<!-- local connection properties -->
<property name="hibernate.connection.url">
jdbc:postgresql://192.168.1.3/datebasename </property>
<property name="hibernate.connection.driver_class">
org.postgresql.Driver
</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">123456</property>
<!-- property name="hibernate.connection.pool_size"></property -->
<!-- dialect for PostgreSQL -->
<property name="dialect">
net.sf.hibernate.dialect.PostgreSQLDialect
</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<mapping resource="showHelper/Block.hbm" />
</session-factory>
</hibernate-configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值