一、注意小点
idea的resources下的文件不会及时清理编译,需要在pom文件中加入配置
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
</resource>
db.properties的user和密码起名注意冲突加上前缀
配置数据工厂时如果不加入扫描xml文件需要给dao层的接口和xml文件名称一致。会默认去找并加载名称一致的xml。
配置事务的propagation时配置方法是否需要事务配置,required需要事务,isolation是配置事务的隔离级别,4个级别序列化、可重复读、读已提交、读未提交。rollback-for回滚的设置。timeout设置超时时间默认时间-1,永不超时。
二、Application.xml
<!--配置扫描service层-->
<context:component-scan base-package="com.qianfeng.service"/>
<!--配置读取properties-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置连接池-->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pass}"/>
</bean>
<!--jdbc的事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置mybatis的工厂交给spring ,该类是实现了spring给的接口-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.qianfeng.entity"/>
<property name="configLocation" value="classpath:Mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/qianfeng/dao/*Mapper.xml"/>
</bean>
<!--配置扫描代理接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qianfeng.dao"/>
</bean>
<!--配置事务的方法-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!--配置切面-->
<aop:config proxy-target-class="true">
<aop:pointcut id="pt" expression="execution(* com.qianfeng.service.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="pt"/>
</aop:config>