说到整合框架,其实也就是环境的搭建了,首先我们要导包,这里连接数据库我们用到了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. 百度echarts

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</t ...

  2. Python中的浅拷贝 深拷贝

    浅拷贝只拷贝父对象,子对象的地址空间不改变,包括下面三种: 1. copy 从下面的例子可以看出对象c从a拷贝,当对象a增加一个列表元素之后,c对象没有改变, 而当对象a中的子列表改变时,对象c的子列 ...

  3. java代码打包成jar以及转换为exe

    教你如何把java代码打包成jar文件以及转换为exe可执行文件 1.背景: 学习java时,教材中关于如题问题,只有一小节说明,而且要自己写麻烦的配置文件,最终结果却只能转换为jar文件.实在是心有 ...

  4. 使用Vue构建中(大)型应用

    init 首先要起一个项目,推荐用vue-cli安装 $ npm install -g vue-cli $ vue init webpack demo $ cd demo $ npm install ...

  5. C#树状图 初始默认选中节点

    效果图: <script type="text/javascript"> $(document).ready(function () { GetTree(); GetG ...

  6. jq 弹半透明遮罩层

    jquery制作点击按钮弹出遮罩半透明登陆窗口 // )[^>]*$|^#([\w-]+)$/,M=/^.[^:#\[\.,]*$/,ka=/\S/,$= /^(\s|\u00A0)+|(\s| ...

  7. 初学Objective-C语言需要了解的星星点点

             其实大多数开发初学者都有一些相同的特点,可以说是一种“职业病”.Most有其他平台开发基础的初学者,看到Xcode就想摩拳擦掌:看到Interface Builder就想跃跃欲试:而 ...

  8. MiniUI动态添加table表格

    本文将介绍一下,如何用Jquery MiniUi动态添加一行table表格 1.效果展示 ↓ 2.具体代码 <script type="text/javascript"> ...

  9. Upnp资料整理

    系统,软件和路由器都要打开upnp功能. windows7下,分别启动 function discovery resources publication; Upnp device host; SSDp ...

  10. Thinkphp更改子集

    //规格编辑 public function standard(){ //$id = $_GET['id'];//dump($id);die; $id = 388; //根据$id 到re_goods ...