框架整合----------Hibernate、spring整合

时间:2023-03-08 22:10:23

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

框架整合----------Hibernate、spring整合

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

框架整合----------Hibernate、spring整合

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

框架整合----------Hibernate、spring整合

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

  1首先是dao层

框架整合----------Hibernate、spring整合

  2其次就是service层

框架整合----------Hibernate、spring整合

框架整合----------Hibernate、spring整合

  3.事务管理器 和sessionFactory关联

框架整合----------Hibernate、spring整合

  4.事务通知

框架整合----------Hibernate、spring整合

  5定义切点

框架整合----------Hibernate、spring整合

现在基本的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>