Eclipse3M7+Hibernate2 运行环境测试 博客分类: JEE开发 HibernatePostgreSQL.netSQLQQ
程序员文章站
2024-02-13 10:20:34
...
package springWebwork2;
import net.sf.hibernate.*;
import org.apache.commons.logging.*;
import java.util.*;
import java.io.*;
import net.sf.hibernate.cfg.Configuration;
import showHelper.block.*;
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++) {
B = (Block) l.get(i);
System.out.println(B.getId()+" "+B.getBlockname()); }
//更新数据
//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>
import net.sf.hibernate.*;
import org.apache.commons.logging.*;
import java.util.*;
import java.io.*;
import net.sf.hibernate.cfg.Configuration;
import showHelper.block.*;
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++) {
B = (Block) l.get(i);
System.out.println(B.getId()+" "+B.getBlockname()); }
//更新数据
//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>