1.maven新建的web(war)工程的結構以下圖,每一個包下的都爲空web
2.spring.xml的配置代碼以下:spring
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 1.引入屬性文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 2.包掃描 --> <context:component-scan base-package="com.zhiyou.han"> <context:exclude-filter type="annotation" expression="com.zhiyou.han.controller"/> </context:component-scan> <!-- 3.配置數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> </bean> <!-- 4.配置SQLSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- <property name="configLocation" value="classpath:mybatis.xml"></property> <property name="mapperLocations" value="classpath:com/zhiyou/han/mapper/*.xml"></property> --> </bean> <!-- 5.配置mybatisDao接口掃描MapperScannerConfigurer --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zhiyou.han.dao"></property> </bean> <!-- 6.事務管理的配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean>
<!-- 7.切面通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="query*" read-only="true" /> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 8.切面 --> <aop:config> <aop:pointcut expression="execution(* com.zhiyou.han.service.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/> </aop:config> </beans>
3.SQLSessionFactory的配置注意事項sql
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- <property name="configLocation" value="classpath:mybatis.xml"></property> <property name="mapperLocations" value="classpath:com/zhiyou/han/mapper/*.xml"></property> --> </bean>
由於mybatis.xml沒有寫,也沒有相關的mapper,因此
express
4.測試代碼:mybatis
若是SQLSessionFactory和DataSource有輸出,怎配置沒有問題app