> 文档中心 > SSM整合配置文件——完整版

SSM整合配置文件——完整版


SSM框架整合

Spring+SpringMVC+Mybatis,
简称SSM,是后台程序员在编程道路上必备的三大框架,也是当前最流行的框架组合,减少代码的冗余,其中Mybatis更是避免了几乎所有的JDBC代码和手动设置参数以及获取结果集!

第一步:创建Maven项目

在这里插入图片描述
第二步:在pom.xml导入所需依赖

        <dependencies>  <dependency>     <groupId>junit</groupId>     <artifactId>junit</artifactId>     <version>4.12</version> </dependency>  <dependency>     <groupId>mysql</groupId>     <artifactId>mysql-connector-java</artifactId>     <version>5.1.47</version> </dependency>   <dependency>     <groupId>com.mchange</groupId>     <artifactId>c3p0</artifactId>     <version>0.9.5.2</version> </dependency>  <dependency>     <groupId>javax.servlet</groupId>     <artifactId>servlet-api</artifactId>     <version>2.5</version> </dependency> <dependency>     <groupId>javax.servlet.jsp</groupId>     <artifactId>jsp-api</artifactId>     <version>2.2</version> </dependency> <dependency>     <groupId>javax.servlet</groupId>     <artifactId>jstl</artifactId>     <version>1.2</version> </dependency>  <dependency>     <groupId>org.aspectj</groupId>     <artifactId>aspectjweaver</artifactId>     <version>1.8.13</version> </dependency>  <dependency>     <groupId>org.mybatis</groupId>     <artifactId>mybatis</artifactId>     <version>3.5.2</version> </dependency> <dependency>     <groupId>org.mybatis</groupId>     <artifactId>mybatis-spring</artifactId>     <version>2.0.2</version> </dependency>  <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-webmvc</artifactId>     <version>5.1.9.RELEASE</version> </dependency> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-jdbc</artifactId>     <version>5.1.9.RELEASE</version> </dependency> <dependency>     <groupId>org.projectlombok</groupId>     <artifactId>lombok</artifactId>     <version>1.18.18</version> </dependency>    </dependencies>        <build>  <resources>     <resource>  <directory>src/main/resources</directory>  <includes>      <include>**/*.properties</include>      <include>**/*.xml</include>  </includes>  <filtering>true</filtering>     </resource>     <resource>  <directory>src/main/java</directory>  <includes>      <include>**/*.properties</include>      <include>**/*.xml</include>  </includes>  <filtering>true</filtering>     </resource> </resources>    </build>

然后依次配置各类xml文件

配置Mybatis-config.xml文件

<configuration><settings><setting name="logImpl" value="STDOUT_LOGGING"/><setting name="mapUnderscoreToCamelCase" value="true"/></settings><typeAliases><package name="com.wumao.pojo"/></typeAliases><mappers><mapper class="com.wumao.mapper.UserMapper"/></mappers></configuration>

配置jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver#如果使用的是mysql8.0+,增加一个时区的设置; &serverTimezone=Asia/Shanghaijdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8jdbc.username=rootjdbc.password=123456

配置spring-mapper.xml文件

<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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/><property name="maxPoolSize" value="30"/><property name="minPoolSize" value="10"/><property name="autoCommitOnClose" value="false"/><property name="checkoutTimeout" value="10000"/><property name="acquireRetryAttempts" value="2"/></bean><bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatis-config.xml"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/><property name="basePackage" value="com.wumao.mapper"/></bean></beans>

配置spring-mvc.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><import resource="classpath:spring-mapper.xml"/><import resource="classpath:spring-service.xml"/><mvc:annotation-driven/><mvc:default-servlet-handler/><context:component-scan base-package="com.wumao.controller"/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean></beans>

配置spring-service.xml文件

<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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttps://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd"><context:component-scan base-package="com.wumao.service"/><bean id="UserServiceImpl" class="com.wumao.service.UserServiceImpl"><property name="userMapper" ref="userMapper"></property></bean><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*" propagation="REQUIRED"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="txPointCut" expression="execution(* com.wumao.mapper.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/></aop:config></beans>

配置applicationContext.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><import resource="classpath:spring-mapper.xml"/><import resource="classpath:spring-service.xml"/><import resource="classpath:spring-mvc.xml"/></beans>

配置web.xml文件

<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"><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><filter><filter-name>encodingFilter</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></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><session-config><session-timeout>15</session-timeout></session-config></web-app>