image.png
    springapplication.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xmlns:tx="http://www.springframework.org/schema/tx"
    7. xsi:schemaLocation="http://www.springframework.org/schema/beans
    8. http://www.springframework.org/schema/beans/spring-beans.xsd
    9. http://www.springframework.org/schema/context
    10. https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    11. <!--Spring组件扫描-->
    12. <context:component-scan base-package="com.wzy">
    13. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><!--不扫描Conntroller-->
    14. </context:component-scan>
    15. <!-- Spring的配置文件,这里主要配置和业务逻辑有关的,比如数据源,事务控制等 -->
    16. <!-- 引入外部配置文件-->
    17. <context:property-placeholder location="classpath:jdbc.properties"/>
    18. <!--c3p0数据库连接池-->
    19. <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    20. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
    21. <property name="driverClass" value="${jdbc.driverClass}"/>
    22. <property name="user" value="${jdbc.user}"/>
    23. <property name="password" value="${jdbc.password}"/>
    24. </bean>
    25. <!--=========================配置、整合MyBatis=========================-->
    26. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    27. <!--指定Mybatis全局配置文件位置-->
    28. <property name="configLocation" value="classpath:mybatis-config.xml"/>
    29. <!-- 指定数据源,引入c3p0-->
    30. <property name="dataSource" ref="pooledDataSource"/>
    31. <!-- 指定mybatis的mapper的文件位置,扫描mapper下的所有xml文件-->
    32. <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    33. </bean>
    34. <!--配置扫描器,将MyBatis接口的实现类加入到ioc容器中-->
    35. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    36. <!--扫描所有dao接口实现,加入到ios容器中-->
    37. <property name="basePackage" value="com.wzy.dao"/>
    38. </bean>
    39. <!--pring配置文件的核心点(数据源、与mybatis整合、事务控制)-->
    40. <!--=============================事务控制===================================-->
    41. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    42. <!--控制数据源-->
    43. <property name="dataSource" ref="pooledDataSource"/>
    44. </bean>
    45. <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式)-->
    46. <aop:config>
    47. <!--切入点表达式-->
    48. <aop:pointcut id="txPoint" expression="execution(* com.wzy.service..*(..))"/>
    49. <!--配置事务增强-->
    50. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    51. </aop:config>
    52. <!-- 配置事务增强,事务如何切入-->
    53. <tx:advice id="txAdvice">
    54. <tx:attributes>
    55. <!--* 代表所有方法都是事务方法-->
    56. <tx:method name="*"/>
    57. <!--以get开始的所有方法-->
    58. <tx:method name="get*" read-only="true"/><!--read-only="true":优化-->
    59. </tx:attributes>
    60. </tx:advice>
    61. </beans>

    jdbc.properties

    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/myb
    jdbc.driverClass=com.mysql.jdbc.Drive
    jdbc.user=root
    jdbc.password=941941