Spring3 整合 Hibernate4实现数据库操作(1)

简介:

Hibernate知识学习:http://justsee.iteye.com/blog/1061576

v注意Hibernate4在开发当中的一些改变  :http://snake-hand.iteye.com/blog/1995592

1
//首先在web.xml中加入OpenSessionInViewFilter过滤器
1
2
3
4
5
6
7
8
9
<br>   <filter> 
       <filter-name>openSessionInViewFilter</filter-name> 
       <filter- class >org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter- class
     </filter>
     
     <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern> 
     </filter-mapping> 
1
在applicationContext.xml中的sessionFactory 中 添加< property  name="hibernate.current_session_context_class">thread</ property > 属性
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
< bean  id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         <!-- 依赖注入数据源,正式上下文定义的dataSource -->
         < property  name="dataSource" ref="dataSource" />
   
         < property  name="hibernateProperties">
             < value >
                 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                 hibernate.hbm2ddl.auto=update
                 hibernate.show_sql=true
                 hibernate.format_sql=false
                 hibernate.cache.use_second_level_cache=true
                 hibernate.cache.use_query_cache=false
                 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                 hibernate.current_session_context_class=thread
             </ value >
        </ property >
        
         <!--通过配置文件的方式获取数据源-->
         < property  name="mappingResources">
             < list >
                <!-- 以下用来列出所有的PO映射文件 -->
                < value >hibernate.cfg.xml</ value >
                < value >test.cfg.xml</ value >
             </ list >
         </ property >     
        
   </ bean >
1
在dao层部分的代码如下,通过sessionFactory可以获得当前的事务的session,通过session实现对数据库的操作
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
public class MessageDaoImpl implements MessageDao{
     
     private SessionFactory sessionFactory; 
      
     public Session getSession() { 
         return sessionFactory.getCurrentSession(); 
    
   
     public SessionFactory getSessionFactory() { 
         return sessionFactory; 
    
   
     public void setSessionFactory(SessionFactory sessionFactory) { 
         this.sessionFactory = sessionFactory; 
    
     
     @Override
     public void saveMessage(TestMessage tm) {
         Session session = this.getSession();
         Transaction tran = session.beginTransaction();
         session.save(tm);
         tran.commit();
     }
 
     @Override
     public String getMessage() {
         Session session = this.getSession();
         Transaction tran = session.beginTransaction();
         List< TestMessage > datas = session.createQuery("From TestMessage").list();
         tran.commit();
         return datas.get(7).getContent();
     }
}









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/4365578.html,如需转载请自行联系原作者
目录
相关文章
|
30天前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——TestStu.java
hibernate正向生成数据库表以及配置——TestStu.java
16 1
|
30天前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.java
hibernate正向生成数据库表以及配置——Teacher.java
11 0
|
1月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
12 0
|
1月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
12 0
|
1月前
|
Shell
sh(Spring+Spring mvc+hibernate)——IEmpDao.java
sh(Spring+Spring mvc+hibernate)——IEmpDao.java
11 0
|
1月前
|
Shell
sh(Spring+Spring mvc+hibernate)——IDeptDao.java
sh(Spring+Spring mvc+hibernate)——IDeptDao.java
13 0
|
2月前
|
安全 Java 数据库
后端进阶之路——万字总结Spring Security与数据库集成实践(五)
后端进阶之路——万字总结Spring Security与数据库集成实践(五)
|
4月前
|
SQL 监控 druid
p6spy【SpringBoot集成】使用p6spy-spring-boot-starter集成p6spy监控数据库(配置方法举例)
p6spy【SpringBoot集成】使用p6spy-spring-boot-starter集成p6spy监控数据库(配置方法举例)
189 0
|
1月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——Dept.java
ssh(Spring+Spring mvc+hibernate)——Dept.java
12 0
|
1月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——showDept.jsp
ssh(Spring+Spring mvc+hibernate)——showDept.jsp
8 0