环境说明:spring4.0+hibernate3

数据库:oracle

连接池:c3p0

项目结构:

lib中的jar:

一、配置spring.xml

说明:这里采用的配置模式将hibernateTemplate注入至类NewsDaoImpl中。在本文的“附”中介绍的是在类NewsDaoImpl中自动装载hibernateTemplate。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 引入外部文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>${driverName}</value>
</property>
<property name="jdbcUrl">
<value>${url}</value>
</property>
<property name="user">
<value>${name}</value>
</property>
<property name="password">
<value>${pwd}</value>
</property>
<property name="maxPoolSize">
<value>40</value>
</property>
<property name="minPoolSize">
<value>10</value>
</property>
<property name="initialPoolSize">
<value>10</value>
</property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/chen/vo/News.hbm.xml</value>
</list>
</property>
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean id="newsDaoImpl" class="com.chen.dao.NewsDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> <!-- 开启注解模式 -->
<context:annotation-config/>
<!-- 扫包(使用“注解模式”需要此配置)。服务器在启动时会扫描base-package所指定的包,并将相应的bean注入ApplicationContext容器。 -->
<context:component-scan base-package="com.chen"></context:component-scan> </beans>

二、配置News.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 指定Hibernate映射文件的DTD信息 -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- hibernate-mapping是映射文件的根源素 -->
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="com.chen.vo.News" table="news_table">
<!-- id元素定义持久化类的标识属性 -->
<id name="id">
<generator class="sequence">
<param name="sequence">seq_news</param>
</generator>
</id>
<!-- property元素定义常规属性 -->
<property name="title"/>
<property name="content"/>
</class>
</hibernate-mapping>

三、创建vo

package com.chen.vo;

public class News
{
//消息类的标识属性
private Integer id;
//消息标题
private String title;
//消息内容
private String content;
//id属性的setter和getter方法
public void setId(Integer id)
{
this.id=id;
}
public Integer getId()
{
return this.id;
}
//title属性的setter和getter方法
public void setTitle(String title)
{
this.title=title;
}
public String getTitle()
{
return this.title;
}
//content 属性的setter和getter方法
public void setContent(String content)
{
this.content=content;
}
public String getContent()
{
return this.content;
} @Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", content=" + content + "]";
} }

四、创建模型层

package com.chen.dao;

import java.util.List;

import com.chen.vo.News;

public interface NewsDao {
public abstract List<News> listNews();
public abstract News findNewsById(int id);
public abstract News findNewsById2(int id);
public abstract void saveNews(News news);
public abstract void deteleNews(News news);
public abstract void updateNews(News news);
}
package com.chen.dao;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository; import com.chen.vo.News;
@Repository("newsDaoImpl")
public class NewsDaoImpl implements NewsDao{
// 设置hibernateTemplate属性
private HibernateTemplate hibernateTemplate;
// 必须设置set方法
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} @Override
public List<News> listNews() {
List<News> entities = hibernateTemplate.find("from News");
return entities;
} @Override
public News findNewsById(int id) {
List<News> entitise = hibernateTemplate.find("from News where id=" + id);
if (entitise.size() > 0) {
News entity = entitise.get(0);
return entity;
}
return null;
} @Override
public News findNewsById2(int id) {
return hibernateTemplate.load(News.class, id);
} @Override
public void saveNews(News news) {
hibernateTemplate.saveOrUpdate(news); } @Override
public void deteleNews(News news) {
hibernateTemplate.delete(news); } @Override
public void updateNews(News news) {
hibernateTemplate.saveOrUpdate(news); } }

五、测试

package com.chen.test;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.chen.dao.NewsDao;
import com.chen.vo.News; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Spring_HibernateTest { //自动装载
@Resource
NewsDao newsDaoImpl; @Test
public void getHibernateTemplate(){
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:spring.xml");
System.out.println(applicationContext.getBean("hibernateTemplate"));
//org.springframework.orm.hibernate3.HibernateTemplate@e79f7a
}
@Test
public void listNews(){
List<News> list = newsDaoImpl.listNews();
System.out.println(list);
}
@Test
public void otherListNews(){
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:spring.xml");
HibernateTemplate hibernateTemplate = (HibernateTemplate) applicationContext.getBean("hibernateTemplate");
List<News> entities = hibernateTemplate.find("from News");
System.out.println(entities);
}
@Test
public void findNewsById(){
News n = newsDaoImpl.findNewsById(2);
System.out.println(n);
}
//org.hibernate.LazyInitializationException: could not initialize proxy - no Session
@Test
public void findNewById2(){
News n = newsDaoImpl.findNewsById2(2);
System.out.println(n);
}
@Test
public void saveNews(){
News n = new News();
n.setTitle("t_Title");
n.setContent("t_content");
newsDaoImpl.saveNews(n);
}
@Test
public void deteleNews(){
News n = new News();
n.setId(6);
newsDaoImpl.deteleNews(n);
}
@Test
public void updateNews(){
News n = newsDaoImpl.findNewsById(2);
n.setTitle("a");
newsDaoImpl.updateNews(n);
}
}

小结:这里采用findNewById2查询bean信息会报异常:org.hibernate.LazyInitializationException: could not initialize proxy - no Session。具体原因及解决方法见“”。


附:在NewsDaoImpl 使用@Resource--自动装载hibernateTemplate

1.修改spring.xml——即删除原来的“将hibernateTemplate 注入到类NewsDaoImpl 中的配置代码”

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 引入外部文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>${driverName}</value>
</property>
<property name="jdbcUrl">
<value>${url}</value>
</property>
<property name="user">
<value>${name}</value>
</property>
<property name="password">
<value>${pwd}</value>
</property>
<property name="maxPoolSize">
<value>40</value>
</property>
<property name="minPoolSize">
<value>10</value>
</property>
<property name="initialPoolSize">
<value>10</value>
</property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/chen/vo/News.hbm.xml</value>
</list>
</property>
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 开启注解模式 -->
<context:annotation-config/>
<!-- 扫包(使用“注解模式”需要此配置)。服务器在启动时会扫描base-package所指定的包,并将相应的bean注入ApplicationContext容器。 -->
<context:component-scan base-package="com.chen"></context:component-scan> </beans>

2.修改NewsDaoImpl

package com.chen.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository; import com.chen.vo.News;
@Repository("newsDaoImpl")
public class NewsDaoImpl implements NewsDao{
@Resource
private HibernateTemplate hibernateTemplate; @Override
public List<News> listNews() {
List<News> entities = hibernateTemplate.find("from News");
return entities;
} @Override
public News findNewsById(int id) {
List<News> entitise = hibernateTemplate.find("from News where id=" + id);
if (entitise.size() > 0) {
News entity = entitise.get(0);
return entity;
}
return null;
} @Override
public News findNewsById2(int id) {
return hibernateTemplate.load(News.class, id);
} @Override
public void saveNews(News news) {
hibernateTemplate.saveOrUpdate(news); } @Override
public void deteleNews(News news) {
hibernateTemplate.delete(news); } @Override
public void updateNews(News news) {
hibernateTemplate.saveOrUpdate(news); } }

重启服务器,在初始化ApplicationContext时会将hibernateTemplate自动装载到类NewsDaoImpl 中。

spring4+hibernate3的更多相关文章

  1. JEECMS v8 发布,java 开源 CMS 系统

    JEECMSv8 是国内java开源CMS行业知名度最高.用户量最大的站群管理系统,支持栏目模型.内容模型交叉自定义.以及具备支付和财务结算的内容电商为一体:  对于不懂技术的用户来说,只要通过后台的 ...

  2. 搭建SSH框架心得

    <Struts2+Spring4+hibernate3> 工程结构 导入相关jar包:自行网上百度即可,根据环境需要 写dao类 以及实现 package com.icommon.dao; ...

  3. spring3.0.6+hibernate3升级spring4.1.6+hibernate4.3遇到的问题

    1.下面这三个包,在hibernate4中已经废弃了,因为都直接用session来进行很好的事务管理 import org.springframework.orm.hibernate3.Hiberna ...

  4. SSH整合(struts2.3.24+hibernate3.6.10+spring4.3.2+mysql5.5+myeclipse8.5+tomcat6+jdk1.6)

    终于开始了ssh的整合,虽然现在比较推崇的是,ssm(springmvc+spring+mybatis)这种框架搭配确实比ssh有吸引力,因为一方面springmvc本身就是遵循spring标准,所以 ...

  5. Spring4.0整合Hibernate3 .6

    转载自:http://greatwqs.iteye.com/blog/1044271 6.5  Spring整合Hibernate 时至今日,可能极少有J2EE应用会直接以JDBC方式进行持久层访问. ...

  6. 【j2ee spring】27、巴巴荆楚网-整合hibernate4+spring4(2)

    巴巴荆楚网-整合hibernate4+spring4(2) 1.图文项目 2.首先我们引入对应的jar包 这里用的是oracle 11g,所以我们使用的数据库连接jar包是ojdbc6, 的区别就是支 ...

  7. 【j2ee spring】30、巴巴荆楚网-综合hibernate4+spring4(5)分页

    巴巴荆楚网-综合hibernate4+spring4(5)分页 1.图文项目 2.首先我们引入对应的jar包 3.我们配置一下数据库中对应的实体对象 ProductType.java /** * 功能 ...

  8. spring2.5与hibernate3升级后的bug

    手头有一个项目,使用的是struts2 hibernate3 spring2.5 是之前的老项目了,spring与hibernate的版本都比较低 自己看了最新的spring4与hibernate4, ...

  9. Spring4+Hibernate4事务小记

    学习Spring+Hibernate,非常强大的框架,为了追新,就直接从最高版本开始学习了,这要冒很大的风险,因为网上可查到的资料大多是针对旧版本的,比如Spring3,Hibernate3. 根据我 ...

随机推荐

  1. 在jQuery环境下制作轻巧遮罩层

    遮罩层的好处就是可以屏蔽用户对遮罩层下方元素的操作. 制作原理很简单:1设置遮罩层触发按钮 2设置遮罩层内容 3设置遮罩层背景(重点是捕获内容div的大小位置)4设置点击触发按钮遮罩层背景内容同时显示 ...

  2. ORACLE 小写金额转大写金额

    Create Or Replace Function Money2Chinese(Money In Number) Return Varchar2 Is strYuan Varchar2(); str ...

  3. SQL server 2008 安装问题解决

    安装sqlserver2008 出现的一些问题解决方法 1,安装sqlserver的时候出现如下图所示,解决办法是:开始→运行→输入“regedit”→找到“HKEY_LOCAL_MACHINE\SY ...

  4. UIView中常见的方法总结

    addSubview: 添加一个子视图到接收者并让它在最上面显示出来.- (void)addSubview:(UIView *)view[讨论]这方法同样设置了接收者为下一个视图响应对象.接收者保留视 ...

  5. css 等高补偿法

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  6. pgpool常用命令

    启动pgpool pgpool -n -d > /tmp/pgpool.log >& & 停止pgpool pgpool stop pgbench的测试命令 pgbench ...

  7. WinForm窗体间如何传值的几种方法

    (转) 窗体间传递数据,无论是父窗体操作子窗体,还是子窗体操作符窗体,有以下几种方式: 公共静态变量: 使用共有属性: 使用委托与事件: 通过构造函数把主窗体传递到从窗体中: 一.通过静态变量 特点: ...

  8. MoneyUtil

    public class MoneyUtil {       private final static String[] CN_Digits = { "零", "壹&qu ...

  9. linux关闭服务的方法

    本文介绍下,在linux下关闭服务的方法,主要学习chkconfig的用法,有需要的朋友参考下. 先来看一个在linux关闭服务的例子,例如,要关闭sendmail服务,则可以按如下操作. 例1, 复 ...

  10. 《疯狂的android讲义第3版》读书笔记

    第一章.开始启程,你的第一行android代码 1.android系统架构: 1)linux内核层:为底层硬件提供驱动,如显示驱动.音频驱动.照相机驱动.蓝牙驱动.Wifi驱动.电源管理等 2)系统运 ...