SSM(Spring+SpringMVC+Mybatis)框架整合Demo+詳細講解
阿新 • • 發佈:2019-01-01
注意:將資料庫、使用者名稱、密碼修改成個人的資料庫。<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 配置資料來源,記得去掉myBatis-config.xml的資料來源相關配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="admin123" /> </bean> <!-- 配置session工廠 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:myBatis-config.xml" /> <!--配置掃描式載入SQL對映檔案,記得去掉mybatis-config配置--> <property name="mapperLocations" value="classpath:cn/mob/jekin/dao/*.xml"/> </bean> <!-- 配置事務管理器,管理資料來源事務處理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事務通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <!-- 預設只處理執行時異常,可加rollback-for="Exception/Throwable"等處理所有異常或包括錯誤 --> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- 配置切面織入的範圍,後邊要把事務邊界定在service層 --> <aop:config> <aop:advisor advice-ref="advice" pointcut="execution(* cn.mob.jekin.service.impl.*.*(..))" /> </aop:config> <!-- 配置SessionTemplate,已封裝了繁瑣的資料操作 --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 自動掃描元件,要把controller去除,他們是在spring-mvc.xml中配置,如果不去除會影響事務管理。 --> <context:component-scan base-package="cn.mob"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 配置 轉換器,對於在basePackage設定的包(包括子包)下的介面類, 如果介面類的全類名在Mapper.xml檔案中和定義過名稱空間一致, 將被轉換成spring的BEAN,在呼叫 的地方通過@Autowired方式將可以注入介面例項 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="basePackage" value="cn.mob.jekin.dao" /> </bean> </beans>
myBatis-config.xml