[1].[代码] [Java]代码 跳至 [1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
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"
/>
包 如下 非常重要
|