1. 在整合SSM框架时有很多的配置文件,每次都自己写很痛苦。自己整理一个小模板,以后直接粘贴即可。

依赖

  1. <packaging>war</packaging>
  2. <dependencies>
  3. <dependency>
  4. <groupId>mysql</groupId>
  5. <artifactId>mysql-connector-java</artifactId>
  6. <version>5.1.47</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.alibaba</groupId>
  10. <artifactId>druid</artifactId>
  11. <version>1.1.15</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.mybatis</groupId>
  15. <artifactId>mybatis</artifactId>
  16. <version>3.5.1</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>4.12</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-context</artifactId>
  26. <version>5.1.5.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.aspectj</groupId>
  30. <artifactId>aspectjweaver</artifactId>
  31. <version>1.8.13</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-jdbc</artifactId>
  36. <version>5.1.5.RELEASE</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-tx</artifactId>
  41. <version>5.1.5.RELEASE</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-test</artifactId>
  46. <version>5.1.5.RELEASE</version>
  47. </dependency>
  48. <!--mybatis整合spring坐标-->
  49. <dependency>
  50. <groupId>org.mybatis</groupId>
  51. <artifactId>mybatis-spring</artifactId>
  52. <version>1.3.1</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework</groupId>
  56. <artifactId>spring-webmvc</artifactId>
  57. <version>5.1.5.RELEASE</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>javax.servlet</groupId>
  61. <artifactId>javax.servlet-api</artifactId>
  62. <version>3.1.0</version>
  63. <scope>provided</scope>
  64. </dependency>
  65. <dependency>
  66. <groupId>javax.servlet.jsp</groupId>
  67. <artifactId>jsp-api</artifactId>
  68. <version>2.2</version>
  69. <scope>provided</scope>
  70. </dependency>
  71. <dependency>
  72. <groupId>jstl</groupId>
  73. <artifactId>jstl</artifactId>
  74. <version>1.2</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>com.fasterxml.jackson.core</groupId>
  78. <artifactId>jackson-databind</artifactId>
  79. <version>2.9.8</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>com.fasterxml.jackson.core</groupId>
  83. <artifactId>jackson-core</artifactId>
  84. <version>2.9.8</version>
  85. </dependency>
  86. <dependency>
  87. <groupId>com.fasterxml.jackson.core</groupId>
  88. <artifactId>jackson-annotations</artifactId>
  89. <version>2.9.0</version>
  90. </dependency>
  91. </dependencies>
  1. 最后Jackson相关的是返回json时候用的。

Spring配置文件

  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:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop.xsd">
  16. <context:component-scan base-package="com.ning.service"/>
  17. <context:property-placeholder location="classpath:jdbc.properties"/>
  18. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  19. <property name="driverClassName" value="${jdbc.driver}"/>
  20. <property name="url" value="${jdbc.url}"/>
  21. <property name="username" value="${jdbc.username}"/>
  22. <property name="password" value="${jdbc.password}"/>
  23. </bean>
  24. <!-- spring整合mybatis-->
  25. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <property name="dataSource" ref="dataSource"/>
  27. <property name="typeAliasesPackage" value="com.ning.entity"/>
  28. <!-- <property name="configLocation" value="classpath:sqlMapConfig.xml"/>-->
  29. </bean>
  30. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  31. <property name="basePackage" value="com.ning.dao"/>
  32. </bean>
  33. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  34. <property name="dataSource" ref="dataSource"/>
  35. </bean>
  36. <tx:annotation-driven/>
  37. </beans>
  1. 这个文件是整合了MyBatis的,所以不用再做一个sqlMapConfig.xml

SpringMVC配置文件

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:mvc="http://www.springframework.org/schema/mvc"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc
  8. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd">
  11. <context:component-scan base-package="com.ning.controller"/>
  12. <mvc:annotation-driven/>
  13. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <property name="prefix" value="/"/>
  15. <property name="suffix" value=".jsp"/>
  16. </bean>
  17. <bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
  18. <property name="converters">
  19. <set>
  20. <bean class="com.ning.converter.DateConverter"/>
  21. </set>
  22. </property>
  23. </bean>
  24. <mvc:default-servlet-handler/>
  25. </beans>
  1. 这里主要是和spring扫描不同的包,加载spring配置文件,配置视图解析器的前缀后缀,以及一个自定义类型转换器的配置方法。自定义类型转换器可以不用。

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <servlet>
  7. <servlet-name>dispatcherServlet</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <init-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:spring-mvc.xml</param-value>
  12. </init-param>
  13. <load-on-startup>2</load-on-startup>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>dispatcherServlet</servlet-name>
  17. <url-pattern>/</url-pattern>
  18. </servlet-mapping>
  19. <!-- 中文乱码过滤器-->
  20. <filter>
  21. <filter-name>characterEncodingFilter</filter-name>
  22. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  23. <init-param>
  24. <param-name>encoding</param-name>
  25. <param-value>UTF-8</param-value>
  26. </init-param>
  27. </filter>
  28. <filter-mapping>
  29. <filter-name>characterEncodingFilter</filter-name>
  30. <url-pattern>/*</url-pattern>
  31. </filter-mapping>
  32. <!-- spring监听器-->
  33. <listener>
  34. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  35. </listener>
  36. <context-param>
  37. <param-name>contextConfigLocation</param-name>
  38. <param-value>classpath:applicationContext.xml</param-value>
  39. </context-param>
  40. </web-app>