spring配置
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8&useAffectedRows=true"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<!---->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="classpath:com/sun/config/mybatis.xml"/>--><!--此种方式必须结合mybatis配置文件使用-->
<property name="mapperLocations" value="classpath:com/sun/mapper/User.xml" /><!--使用此种方式可以不用额外配置mybatis配置文件-->
</bean>
<!--事务管理器配置-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txadvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--aop切面配置-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.sun.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
</aop:config>
<!--关联sqlsessionfactory可以跳过sqlsessiontemplate但是必须继承SqlSessionDaoSupport-->
<bean id="userDaoImpl" class="com.sun.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="userServiceImpl" class="com.sun.service.impl.UserServiceImpl">
<property name="userDao" ref="userDaoImpl"/>
</bean>