说到整合框架,其实也就是环境的搭建了,首先我们要导包,这里连接数据库我们用到了spring容器,我们用连接池来进行数据库的连接,我们需要导入c3p0和jdbc的jar包,其余的就是spring和Hibernate的jar包

之后我们在src下新建一系列的包和类,dao层我们新建一个接口,在建一个接口的实现类,同样service层也是新建一个接口和实现类,再写一个测试的main方法,当然实体类肯定不能少

接下来新建一个spring的xml(app.xml),在这个xml中我们获得sessionfactory,并加载配置文件和映射关系

接下来就要在xml中配置bean了

  1首先是dao层

  2其次就是service层

  3.事务管理器 和sessionFactory关联

  4.事务通知

  5定义切点

现在基本的xml配置就完成了

到这里框架就已经搭建好了,接下来就是main方法中该注意的事项了

下面是main方法代码

package com.hanqi.test;

import java.math.BigDecimal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hanqi.entity.AccountBank;
import com.hanqi.service.AccountBankServiceInterface; public class TestMain { public static void main(String[] args) {
// TODO 自动生成的方法存根 ApplicationContext ac =
new ClassPathXmlApplicationContext("app.xml") ; //service层接口实例化
AccountBankServiceInterface abs = (AccountBankServiceInterface)ac.getBean("kahao1") ; System.out.println(abs.getBankCard("123123")); AccountBank ab = new AccountBank("123","刘四","9999",new BigDecimal(777),"333") ; abs.addCard(ab); } }

在上面,我们看到了spring容器的调用,也就是下面这行代码

 //service层接口实例化
AccountBankServiceInterface abs = (AccountBankServiceInterface)ac.getBean("kahao1") ;

这里是接口的实例化而不是实现类的实例化,这里要特别注意,否则会发生下面的异常

Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy9 cannot be cast to com.hanqi.service.impl.AccountBankService
at com.hanqi.test.TestMain.main(TestMain.java:24)

下次遇到这样的异常也会解决了吧,

接下来放上所有的代码供参考

dao接口

package com.hanqi.dao;

import com.hanqi.entity.AccountBank;

public interface AccountBankDAOInterface {

    AccountBank getCard(String kahao) ;

    void insert(AccountBank ab) ;

    void update(AccountBank ab) ;

    void delete(AccountBank ab) ;
}

dao实现类

package com.hanqi.impl;

import org.hibernate.SessionFactory;

import com.hanqi.dao.AccountBankDAOInterface;
import com.hanqi.entity.AccountBank; public class AccountBankDAO implements AccountBankDAOInterface { // 注入SessionFactory
private SessionFactory sessionFactory ; public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} @Override
public AccountBank getCard(String kahao) { AccountBank ab = (AccountBank)sessionFactory
.getCurrentSession() //得到当前session
.get(AccountBank.class, kahao) ; return ab;
} @Override
public void insert(AccountBank ab) { sessionFactory.getCurrentSession().save(ab) ; } @Override
public void update(AccountBank ab) { sessionFactory.getCurrentSession().update(ab); } @Override
public void delete(AccountBank ab) { sessionFactory.getCurrentSession().delete(ab); } }

service接口

package com.hanqi.service;

import com.hanqi.entity.AccountBank;

public interface AccountBankServiceInterface {

    AccountBank getBankCard(String kahao) ;

    void addCard(AccountBank ab) ;

    void updateCard(AccountBank ab) ;

    void deleteCard(AccountBank ab) ;
}

service实现类

package com.hanqi.service.impl;

import com.hanqi.entity.AccountBank;
import com.hanqi.impl.AccountBankDAO;
import com.hanqi.service.AccountBankServiceInterface; public class AccountBankService implements AccountBankServiceInterface { //注入DAO
private AccountBankDAO accountBankDAO ; public void setAccountBankServiceInterface(AccountBankDAO accountBankDAO) {
this.accountBankDAO = accountBankDAO;
} @Override
public AccountBank getBankCard(String kahao) { return accountBankDAO.getCard(kahao);
} @Override
public void addCard(AccountBank ab) { accountBankDAO.insert(ab);
} @Override
public void updateCard(AccountBank ab) { accountBankDAO.update(ab);
} @Override
public void deleteCard(AccountBank ab) { accountBankDAO.delete(ab);
} }

db.properties文件

jdbc.driver_class=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=test0816
jdbc.password=934617699
jdbc.initPoolSize=1
jdbc.maxPoolSize=10

xml配置代码

<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
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.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 基于c3p0连接池的数据源 -->
<!-- 加载外部配置文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver_class}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean> <!-- Hibernate 的 SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 加载Hibernate的配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 映射文件, 可以使用*作为通配符-->
<property name="mappingLocations" value="classpath:com/hanqi/entity/*.hbm.xml" ></property> </bean> <!-- bean -->
<!-- DAO -->
<bean id="accountBankDAO" class="com.hanqi.impl.AccountBankDAO"
p:sessionFactory-ref="sessionFactory"></bean> <!-- Service -->
<bean id="kahao1" class="com.hanqi.service.impl.AccountBankService"
p:accountBankServiceInterface-ref="accountBankDAO"></bean> <!-- 声明式事务 -->
<!-- 1.事务管理器 和sessionFactory关联 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 事务通知 -->
<tx:advice id="adv" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/><!-- 使用事务的方法 -->
<tx:method name="get" read-only="true"/> <!-- get开头的只读方法,不使用事务 -->
</tx:attributes>
</tx:advice> <!-- 事务切点 -->
<aop:config>
<aop:advisor advice-ref="adv" pointcut="execution(* com.hanqi.service.*.*(..))"/> <!-- 接口 -->
</aop:config> </beans>

框架整合----------Hibernate、spring整合的更多相关文章

  1. Struts 2 + Hibernate + Spring 整合要点

    Struts 2 和 Spring 的功能有重合,因此有必要说明下,整合中分别使用了两种框架的哪些技术. Struts 2 使用功能点: 1.拦截器.一处是对非登录用户购物进行拦截,一处是对文件上传的 ...

  2. Struts2+Hibernate+Spring 整合示例

    转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...

  3. Hibernate+Spring整合开发步骤

    Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升. 整合开发步骤如下: 第一步:导入架包: 1.Hibernate ...

  4. 【Saas-export项目】--项目整合(spring整合MVC)

    转: [Saas-export项目]--项目整合(spring整合MVC) 文章目录 Spring整合SpringMVC(export_web_manager子工程) (1)log4j.propert ...

  5. Struts2+Hibernate+Spring 整合示例[转]

    原文 http://blog.csdn.net/tkd03072010/article/details/7468769 Spring整合Struts2.Hibernate原理概述: 从用户角度来看,用 ...

  6. SSH整合之spring整合hibernate

    SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...

  7. [置顶] struts2+hibernate+spring整合(annotation版)

    本博文使用struts2,hibernate,spring技术整合Web项目,同时分层封装代码,包含model层,DAO层,Service层,Action层. 在整合hibernate时使用annot ...

  8. 二 SSH整合:Spring整合Hibernate,无障碍整合&无核心配置整合,Hibernate模版常用方法,

    重建SSH项目 java项目可以直接复制,但是web项目除了改名字还要该配置,如下: 方式一:无障碍整合:带Hibernate配置文件 <?xml version="1.0" ...

  9. J2EE框架(Struts&Hibernate&Spring)的理解

    SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层)Struts:Struts是一个表示层框架,主要作用是界面展示,接收请求,分发请求.在MVC框架中,Struts属于 ...

  10. 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action

    SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib  开发基本包 Struts2有一 ...

随机推荐

  1. lintcode bugfree and good codestyle note

    2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写py ...

  2. Android--我的Butterknife黄油刀怎么找不到控件了!!!

    1,首先说一下Butterknife这个插件真的挺好用的,不过最近几天在写demo的时候发现总是出现绑定的view是空的,当时着急写代码,也没有深究一下,直接手工findViewbyid了,今天下午写 ...

  3. Overview of the Oppia codebase(Oppia代码库总览)

    Oppia is built with Google App Engine. Its backend is written in Python, and its frontend is written ...

  4. android 流量 压缩

    引用:http://my.eoe.cn/blue_rain/archive/340.html 对于目前的状况来说,移动终端的网络状况没有PC网络状况那么理想.在一个Android应用中,如果需要接收来 ...

  5. python 在最后一行追加

    2.文本文件的写入 import fileinput file = open("D:\\test.txt", encoding="utf-8",mode=&qu ...

  6. WebSQL 查询工具

    最近在写 WebSQL ,每次都在浏览器控制台执行 SQL 太费劲了,并且脑子不好使,总是忘记上次初始化的数据库是什么,所以写了一个特别简单的 WebSQL 可视化工具,说工具有点大了,就是为了方便, ...

  7. WebConfig错误页配置

    在system.web节下配置<customErrors mode="On" defaultRedirect="/ErrorPage/MyErrorPage.htm ...

  8. Oracle 12c In Memory Option初探

    前情提要: Oracle OpenWorld 2013中Larry Ellison爆料的Oracle新特性:Oracle In Memory Database Option 1. 这个新特性将随着12 ...

  9. VMWare虚拟机NAT上网方法 亲测可用

    首先虚拟机的网卡要选择NAT 然后 在Virtual Network Editor中一定选上DHCP功能. 还要主机的服务必须开启.主机的VMnet8对应NAT模式,这个VMnet8的配置保持默认不要 ...

  10. UILabel内容模糊

    在非retina的ipad mini的屏幕上,一个UIlabel的frame的origin值如果有小数位数(例如0.5),就会造成显示模糊,所以最好使用整数的值作为origin坐标.