[java]代码库
hibernate.cfg.xml配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databaseName=newsDB
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="show_sql">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="c3p0.max_size">30</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<mapping resource="com/entity/NewsInfo.hbm.xml" />
<mapping resource="com/entity/NewsTypeInfo.hbm.xml" />
<mapping resource="com/entity/UserInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>
hibernate 的工厂获取方式 ---》更3不同
package com.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateSessionFactory {
private static Session session;
private static SessionFactory sessionFatory =null;
static{
Configuration cfg = new Configuration().configure("/hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFatory = cfg.buildSessionFactory(serviceRegistry);
}
public static Session getSession() {
return sessionFatory.openSession();
}
public static SessionFactory getSessionFatory() {
return sessionFatory;
}
public static void setSessionFatory(SessionFactory sessionFatory) {
HibernateSessionFactory.sessionFatory = sessionFatory;
}
}
在实体 里面放入xml配置
<cache usage="read-write"/>
包 如下 非常重要
//源代码片段来自云代码http://yuncode.net