SSM整合
Spring简介
Spring是由Rod Johnson 发起的,最初名为Interface21,Interface21是Spring的前身,Spring的目的是为了解决企业级应用的开发的复杂性。
简单来说,Spring是一个控制反转(IOC)和面向切面编程(AOP)的轻量级框架。
Mybatis简介
MyBatis 本是apache的一个开源项目iBatis,后改名Mybatis。Mybatis是一款优秀的持久层框架。
简单来说,Mybatis是为了简化JDBC的操作,几乎避免了所有的JDBC代码。
SpringMVC简介
Spring MVC属于SpringFrameWork的后续产品。Spring MVC用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发 。
SSM整合
创建一个干净的maven工程,不使用任何模框架
添加web框架支持(主要是maven自带提供的web框架太老了,里面的配置文件都需要一一改动)
在pom.xml文件中添加jar包依赖
<?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>org.example</groupId>
<artifactId>SpringSpringMVCMybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<!--@test测试支持-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<!--Mysql支持-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<!--连接池,这里采用c3p0,具体看实际情况采用-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.5</version>
</dependency>
</dependencies>
<!--静态资源无法导出-->
<!--maven特性:约束大于配置,无法导出在java包下的配置文件,所以配置以下内容-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
搭建项目结构
这里为了结构分明,applicationContext.xml分解成spring-mybatis.xml spring-service.xml springMVC.xml
在applicationContext.xml中引入另外三个spring配置文件
配置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">
<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>
<!--启动级别设置为1-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/与/*的区别
/ 指的是所有的路径形url
/* 指的是所有的url 包括.jsp .html文件
这里配置的是DispatcherServlet前端控制器,接收用户发送过来的请求,都是路径形url
Servlet 的 RequestDispatcher 必须通过名称而不是路径来检索。 换句话说就是 Spring MVC 将接收到的 所有请求都看作是一个普通的请求,包括对于静态资源的请求。
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置filter 防止中文乱码的问题-->
<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>
</web-app>
配置springMVC.xml并在WEB-INF目录下创建test目录test.jsp文件准备测试
<?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">
<!--配置要扫描的包,springmvc接收前端控制器发来的请求,需要对handler进行处理,handler处于controller层-->
<context:component-scan base-package="com.tan.controller"/>
<!--支持注解-->
<context:annotation-config/>
<!--配置HandlerMapping与HandlerAdapter(处理器映射器和处理器适配器)-->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!--视图过滤器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/test/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
编写Controller包下的ControllerTest类进行测试
package com.tan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ControllerTest {
@RequestMapping("/test")
public String test(){
return "test";
}
}
特别注意!因为是没有导模板的maven项目,是后来添加的web框架支持,所以在WEB-INF目录下是没有lib目录的!
也就是说并没有jar包的支持,需要手动添加
配置Tomcat进行测试
编写实体类(数据库用的测试的,表就三个实体类的字段)
配置spring-mybatis.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">
<!--关联数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置连接池池-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置sqlSessionFactoryBean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<!--连接mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--配置dao接口扫描包,动态的实现了dao接口可以注入到Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.tan.dao"/>
</bean>
</beans>
编写dao类的UserMapper接口
package com.tan.dao;
import com.tan.pojo.User;
import java.util.List;
public interface UserMapper {
//查询所有用户
List<User> users();
}
编写UserMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tan.dao.UserMapper">
<select id="users" resultType="user">
select id,name,pwd from user
</select>
</mapper>
配置mybatis-config.xml
<?xml version="1.0" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--别名尽量不要用包的形式,如果配置了日志log4j,包的形式会导致日志提取不出来-->
<typeAliases>
<typeAlias type="com.tan.pojo.User" alias="user"/>
</typeAliases>
<!--注册Mapper.xml文件-->
<mappers>
<mapper resource="com/tan/dao/UserMapper.xml"/>
</mappers>
</configuration>
给spring-service.xml中添加配置,扫描service包下的类以及注解支持
<?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">
<context:component-scan base-package="com.tan.service"/>
<context:annotation-config/>
</beans>
编写UserService接口以及它的实现类
package com.tan.service;
import com.tan.pojo.User;
import java.util.List;
public interface UserService {
List<User> users();
}
package com.tan.service;
import com.tan.dao.UserMapper;
import com.tan.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
//自动注入UserMapper的bean
@Autowired
@Qualifier("userMapper")
private UserMapper userMapper;
public List<User> users() {
return userMapper.users();
}
}
编写Controller包下的类以及在test.jsp中添加el表达式取出数据显示
package com.tan.controller;
import com.tan.pojo.User;
import com.tan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class ControllerTest {
@Autowired
@Qualifier("userService")
private UserService userService;
@RequestMapping("/test")
public String test(Model model){
List<User> users = userService.users();
model.addAttribute("msg",users);
return "test";
}
}
最后再进行测试
总结:今天课程太多,也发现自己对整合的有些步骤记不清楚,索性一步一步再慢慢来搭一次,系统的回忆一下。
注意上面的步骤只是最简要的步骤,日志都没有配。尽量整合一些东西就进行测试一下,不然排错很麻烦。