1.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>Mybatis</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>ssm</artifactId>
  12. <properties>
  13. <SPRING.VERSION>5.2.6.RELEASE</SPRING.VERSION>
  14. <ASPECTJWEAVER.VERSION>1.9.5</ASPECTJWEAVER.VERSION>
  15. </properties>
  16. <dependencies>
  17. <!--springmvc-->
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-webmvc</artifactId>
  21. <version>${SPRING.VERSION}</version>
  22. </dependency>
  23. <!--spring-->
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-aop</artifactId>
  27. <version>${SPRING.VERSION}</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.aspectj</groupId>
  31. <artifactId>aspectjweaver</artifactId>
  32. <version>${ASPECTJWEAVER.VERSION}</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-aspects</artifactId>
  37. <version>${SPRING.VERSION}</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-orm</artifactId>
  42. <version>${SPRING.VERSION}</version>
  43. </dependency>
  44. <!-- mybatis-spring适配器-->
  45. <dependency>
  46. <groupId>org.mybatis</groupId>
  47. <artifactId>mybatis-spring</artifactId>
  48. <version>2.0.5</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.mybatis</groupId>
  52. <artifactId>mybatis</artifactId>
  53. <version>3.5.5</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>com.alibaba</groupId>
  57. <artifactId>druid</artifactId>
  58. <version>1.1.21</version>
  59. </dependency>
  60. <!-- mysql 对应版本的连接器驱动-->
  61. <dependency>
  62. <groupId>mysql</groupId>
  63. <artifactId>mysql-connector-java</artifactId>
  64. <version>5.1.47</version>
  65. </dependency>
  66. <!--log start -->
  67. <dependency>
  68. <groupId>org.slf4j</groupId>
  69. <artifactId>slf4j-api</artifactId>
  70. <version>1.7.30</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>ch.qos.logback</groupId>
  74. <artifactId>logback-classic</artifactId>
  75. <version>1.2.3</version>
  76. </dependency>
  77. <!--log end -->
  78. <dependency>
  79. <groupId>com.fasterxml.jackson.core</groupId>
  80. <artifactId>jackson-core</artifactId>
  81. <version>2.10.3</version>
  82. </dependency>
  83. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  84. <dependency>
  85. <groupId>com.fasterxml.jackson.core</groupId>
  86. <artifactId>jackson-databind</artifactId>
  87. <version>2.10.3</version>
  88. </dependency>
  89. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
  90. <dependency>
  91. <groupId>com.fasterxml.jackson.core</groupId>
  92. <artifactId>jackson-annotations</artifactId>
  93. <version>2.10.3</version>
  94. </dependency>
  95. </dependencies>
  96. </project>

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- spring 基于web应用的启动-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 全局参数:spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-core.xml</param-value>
    </context-param>

    <!--前端调度器 -->
    <servlet>
        <servlet-name>dispatchServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 设置配置文件路径-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--设置启动以及加载 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatchServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置过滤器Filter -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--设置编码格式 -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <!--<url-pattern>/*</url-pattern>-->
        <servlet-name>dispatchServlet</servlet-name>
    </filter-mapping>



    <!-- 支持rest的过滤器-->
    <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <servlet-name>dispatchServlet</servlet-name>
    </filter-mapping>



</web-app>

3.spring-mvc.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- 1.扫描Controller包-->
    <!--1.1.<context:component-scan base-package="com.xixi.controller"></context:component-scan>-->
    <!--2.使用以下方式需要去设置哪些类型可以被扫描 use-default-filters=false -->
    <context:component-scan base-package="com.xixi" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 2.添加annotationDriver-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 3.添加视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
        <property name="prefix" value="/WEB-INF/views"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 静态资源解析-->
<!--    <mvc:resources mapping="" location=""></mvc:resources>-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>


</beans>

4.spring-core.xml

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

    <!--扫描所有除了controller包的其他包-->
    <context:component-scan base-package="com.xixi">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!--引入外部属性资源文件-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

    <!--创建Druid数据源-->
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="username" value="${mysql.username}"></property>
        <property name="password" value="${mysql.password}"></property>
        <property name="url" value="${mysql.url}"></property>
        <property name="driverClassName" value="${mysql.driverClassName}"></property>
    </bean>

    <!--声明式事-->
    <!--    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">-->
    <!--        <property name="dataSource" ref="dataSource"></property>-->
    <!--    </bean>-->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--基于驱动-->
    <!--    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>-->


    <!--用于声明事务切入的所有方法-->
    <!--用来明确切点匹配到的方法哪些方法需要使用事务-->
    <aop:config>
        <aop:pointcut id="txPoint" expression="execution(* com.xixi.service.*.*(..))"/>
        <!--事务建议:advice-ref:指向事务管理器的配置-->
        <aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"></aop:advisor>
    </aop:config>
    <!--用来明确切点匹配到的方法哪些方法需要使用事务-->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--可以使用通配符-->
            <tx:method name="add*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS"></tx:method>
            <tx:method name="query*" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>


    <!--SqlSessionFactory-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <!-- 指定spring中的数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:com/xixi/mapper/*.xml"></property>
<!--        <property name="mapperLocations" value="classpath:config/mybatis/*.xml"></property>-->
    </bean>
<!--    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!--        <property name="basePackage" value="com.xixi.mapper"></property>-->
<!--    </bean>-->

    <!--将mapper接口交给spring管理-->
    <mybatis:scan base-package="com.xixi.mapper"></mybatis:scan>

</beans>

5.mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--mybatis的配置选项,可以改变mybatis运行时行为 -->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <!-- 类型别名可为Java类型设置一个缩写的名称,它仅用于XML配置,在于降低冗余的全限定类名-->
    <typeAliases>
        <package name="com.xixi.pojo"/>
    </typeAliases>
</configuration>