Spring和mybatis整合,数据库由spring来进行管理,

spring.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:tx="http://www.springframework.org/schema/tx"
  xmlns:mvc="http://www.springframework.org/schema/mvc"

      xsi:schemaLocation="

  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
    
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  
  http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        
      ">
      <!-- 配置C3P0连接池,目的:管理数据库连接 -->
      <bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass" value="com.mysql.jdbc.Driver"/>
      <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/data"/>
      <property name="user" value="root"/>
      <property name="password" value="root"/>
      <!-- <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
      <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
      <property name="user" value="scott"/>
      <property name="password" value="tiger"/> -->
      </bean>
      
      <!-- 配置SqlSessionFactoryBean,目的:加载mybaits配置文件和映射文件,即替代原Mybatis工具类的作用 -->
      <bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="configLocation" value="classpath:mybatis.xml"/>
      <property name="dataSource" ref="comboPooledDataSourceID"/>
      </bean>
 
      <!-- 配置Mybatis的事务管理器,即因为Mybatis底层用的是JDBC事务管事器,所以在这里依然配置JDBC事务管理器 -->
      <bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="comboPooledDataSourceID"/>
      </bean>
      
      <!-- 配置事务通知,即让哪些方法需要事务支持 -->
      <tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">
      <tx:attributes>
      <tx:method name="*" propagation="REQUIRED"/>
      </tx:attributes>
      </tx:advice>
      
      <!-- 配置事务切面,即让哪些包下的类需要事务 --> <!-- 错误写法:execution(*cn.it.jzl.dao.*.*(..)) 把第一个*后的空格给省了-->
      <aop:config>
      <aop:pointcut id="pointcut" expression="execution(* cn.it.jzl.daoImpl.*.*(..))"/>
      <aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
      </aop:config>
  <bean id="answerDaoID" class="cn.it.jzl.daoImpl.AnswerDaoImpl">
  </bean>
  <bean id="answerServiceID" class="cn.it.jzl.serviceImpl.AnswerServiceImpl">
  <property name="answerDao" ref="answerDaoID"></property>
  </bean>
  <bean name="/hello.action" class="cn.it.jzl.actionImpl.AnswerActionImpl">
  <property name="answerService" ref="answerServiceID"></property>
  </bean>
</beans> 

猜你喜欢

转载自blog.csdn.net/jzl110/article/details/80852681
今日推荐