一. 添加依赖
在整合之前,我们先来看一下,如果不适用Maven,我们应该如何将ssm整合,如果您想查看使用Maven整合SSM,请直接打开本文第五段
二. Spring 整合SpringMVC
1. Spring配置文件:applicationContext.xml
-
配置自动扫描组件,不扫描控制层的组件
2. SpringMVC的配置文件:spring-mvc.xml
-
配置自动扫描组件,只扫描控制层的组件
-
视图解析器
-
静态资源访问
3. web.xml
-
处理乱码的过滤器:CharacterEncodingFilter
-
请求分配器:初始化SpringMVC的IOC容器,DispatchServlet
-
上下文监听:初始化Spring的IOC容器,ContextLoaderListener
三. 整合Mybatis
1. 配置mybatis-config.xml
2. 配置jdbc.properties
3. 配置log4j.properties
4. 添加BookMapper.xml
5. 修改applicationContext.xml
-
配置DruidDataSource
-
配置DataSourceTransactionManager
-
配置开启使用注解配置事务
-
配置SQLSessionFactoryBean
-
配置MapperScannerConfigurer
四. 代码文件
1. 数据库
数据库采用的还是 《MyBatis:简单使用》一文中创建的数据库
2. Book
package io.zhangjia.ssm.entity; public class Book { private Integer bookId; private String name; private String author; private Double price; public Integer getBookId() { return bookId; } public void setBookId(Integer bookId) { this.bookId = bookId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } @Override public String toString() { return "Book{" + "bookId=" + bookId + ", name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}'; } }
3. Controller
package io.zhangjia.ssm.controller; import io.zhangjia.ssm.entity.Book; import io.zhangjia.ssm.mapper.BookMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class TestController { @Autowired private BookMapper bookMapper; @GetMapping("/test") public String Test(Integer id, Model model) { Book book = bookMapper.queryById(id); model.addAttribute("book",book); System.out.println("book = " + book); return "test"; } }
4. BookMapper
package io.zhangjia.ssm.mapper; import io.zhangjia.ssm.entity.Book; public interface BookMapper { Book queryById(Integer bookId); }
5. Service
package io.zhangjia.ssm.service; import org.springframework.stereotype.Service; @Service public class TestService { }
6. BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="io.zhangjia.ssm.mapper.BookMapper"> <select id="queryById" resultType="book"> SELECT * FROM BOOK WHERE BOOK_ID = #{BOOK_ID} </select> </mapper>
7. applicationContext.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" 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/tool http://www.springframework.org/schema/tool/spring-tool.xsd"> <context:component-scan base-package="io.zhangjia.ssm" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <!-- OGNL--> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:mapper/*.xml" /> <property name="typeAliasesPackage" value="io.zhangjia.ssm.entity" /> <!--起别名--> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="basePackage" value="io.zhangjia.ssm.mapper"/> </bean> </beans>
8. jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/demo2?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=zhangjia
9. log4j.properties
# Global logging configuration log4j.rootLogger=DEBUG, stdout #log4j.rootLogger=ERROR, stdout # MyBatis logging configuration... log4j.logger.org.mybatis.example.BlogMapper=TRACE # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
10. 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> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> </settings> </configuration>
11. 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"> <context:component-scan base-package="io.zhangjia.ssm" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!--静态资源的请求转由Web容器处理--> <mvc:default-servlet-handler/> <!--配置默认组件--> <mvc:annotation-driven/> <!--如果不配置这个,那么静态资源可以访问,Controller无法访问--> </beans>
12. test.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <!DOCTYPE html> <html> <head> <title>Title</title> </head> <body> <h1>张大嘎</h1> ${requestScope.book} </body> </html>
五. 使用Maven整合SSM
使用Maven整合SSM非常简单,首先按照本站《Maven:使用IDEA基于Maven创建JavaWeb项目》一文的教程创建JavaWeb项目,
并将pom.xml的内容替换为:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.oaec</groupId> <artifactId>ssm2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ssm2 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--*********************************************Spring依赖*************************************************--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.8.RELEASE</version> <!--会自动添加以下依赖 Maven: org.springframework:spring-aop:5.1.8.RELEASE Maven: org.springframework:spring-beans:5.1.8.RELEASE Maven: org.springframework:spring-context:5.1.8.RELEASE Maven: org.springframework:spring-core:5.1.8.RELEASE Maven: org.springframework:spring-expression:5.1.8.RELEASE Maven: org.springframework:spring-jcl:5.1.8.RELEASE --> </dependency> <!--*********************************************SpringMVC依赖*************************************************--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.8.RELEASE</version> <!--会自动添加以下依赖 Maven: org.springframework:spring-web:5.1.8.RELEASE Maven: org.springframework:spring-webmvc:5.1.8.RELEASE --> </dependency> <!--*********************************************Mybatis依赖*************************************************--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <!--*********************************************spring-jdbc依赖*************************************************--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.8.RELEASE</version> <!--会自动添加以下依赖 Maven: org.springframework:spring-jdbc:5.1.8.RELEASE Maven: org.springframework:spring-tx:5.1.8.RELEASE --> </dependency> <!--*********************************************log4j*************************************************--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--*********************************************mybatis-spring*************************************************--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> <!--该依赖用于mybatis和spring整合--> </dependency> <!--*********************************************mysql驱动*************************************************--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--*********************************************数据库连接池*************************************************--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <!--*********************************************Mybatis插件*************************************************--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.9</version> </dependency> <!--*********************************************JSTL插件*************************************************--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!--*********************************************JSTL插件*************************************************--> </dependencies> <build> <finalName>ssm2</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
接下来将上文前四节中所有文件,按照下图的方式,分为放入指定文件夹即可完成ssm的整合
请登录之后再进行评论