一、引入jar包
SSM搭建的第一件事就是引入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>
<packaging>war</packaging>
<groupId>cn.tuling.mvc</groupId>
<artifactId>spring-mvc</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.0</version>
</dependency>
<!--servlet包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!--EL表达式-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<type>jar</type>
</dependency>
<!--数据校验的包, 实现了JSR349规范-->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.0.Final</version>
</dependency>
<!--jackson包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.5</version>
</dependency>
<!--junit4-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--日志-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- mysql数据库驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.0</version>
</dependency>
<!--c3p0 数据连接池 jar-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<!--C3P0额外依赖的一个jar包-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.10</version>
</dependency>
<!-- Mybatis框架:-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MyBatis整合Spring的适配包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
二、web.xml
web.xml中主要的配置有三个类:
1、ContextLoaderListener:这个监听器的作用是创建spring根上下文,也就是root上下文
2、DispatcherServlet:这个Servlet是SpringMVC的核心,DispatcherServlet中持有视图解析器、映射器等引用,并且在DispatcherServlet初始化时创建SpringMVC的应用上下文
3、CharacterEncodingFilter:这个过滤器的作用主要是为了解决中文乱码的问题
下面是配置文件:
<?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">
<!--SpringMVC 中文乱码问题解决-->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcherServlet</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>
<!--
servlet默认是懒加载,也就是当servlet第一次被访问时,才会加载被访问的servlet,
如果配置了<load-on-startup>,就是在tomcat容器启动时就加载当前servlet
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--
/ 匹配除jsp以外的所有的请求路径
/* 匹配所有请求路径
*.do 匹配所有以.do结尾的所有请求
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
三、配置SpringMVC
为了功能清晰,一般在SpringMVC中的应用上下文中只注入SpringMVC中的处理器,也就是被@Controller注解标注的类。
业务处理类以及与数据打交道的类都注册到Spring的根容器中。
<?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">
<!--开启注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--用来指定需要扫描的包,
默认情况: 在指定的包下,如果类被@Service、@Compont、@Controller、@Repository标注,则这些被标注的类会被注册到Spring容器
此时use-default-filters="false" 说明碰到被上述四个注解标注的类,并不会注册到Spring容器。
把被Controller注解标注的类注入到Spring容器
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-->
<context:component-scan base-package="com.*" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置静态资源有两种方式
1、配置mvc:resources 通过mvc来放行静态资源
2、配置默认的servlet
-->
<!-- <mvc:resources mapping="" location=""></mvc:resources>-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:view-controller path="/param08" view-name="home"/>
</beans>
四、Spring根容器配置
<?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">
<!--把application.properties的配置内容加载到spring环境中 -->
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>
<!--扫描com包下所有的类,并被Controller注解标注的类排除-->
<context:component-scan base-package="com.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置数据源-->
<bean name="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="driverClassName" value="${database.driver}"/>
</bean>
<!--mybatis整合包中的bean,
配置mybitas工厂,同时指定数据源,并与MyBatis完美整合
-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSouce"/>
<!-- configLocation的属性值为Mybatis的核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!--mybatis整合包中的bean,需要把mapper接口扫描到Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.haojie.mapper"/>
</bean>
</beans>
五、mybatis配置文件
<?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>
<mappers><!-- 映射器告诉MyBatis到哪里去找映射文件 -->
<mapper resource="mappers/TableMapper.xml" />
<mapper resource="mappers/TPermssionMapper.xml" />
<mapper resource="mappers/TRoleMapper.xml" />
<mapper resource="mappers/TRolePermssionMapper.xml" />
<mapper resource="mappers/TUserMapper.xml" />
<mapper resource="mappers/TUserRoleMapper.xml" />
</mappers>
</configuration>
六、数据库配置文件
database.username=root
database.password=123456
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/rbac?characterEncoding=utf-8
七、代码
7.1、mapper.xml文件
根据mybatis配置文件中的配置mapper.xml文件放在resources的mappers目录下:
<?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="com.haojie.mapper.TUserMapper">
<select id="qryTUser" resultType="com.haojie.beans.TUser">
select * from t_user
</select>
</mapper>
7.2、mapper接口文件
根据Spring根容器中的配置,mapper接口文件放在com.haojie.mapper包下
package com.haojie.mapper;
import com.haojie.beans.TUser;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface TUserMapper {
List<TUser> qryTUser();
}
7.3、测试代码
package com.haojie.controller;
import com.haojie.beans.TUser;
import com.haojie.mapper.TUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.support.RequestContextUtils;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
public class ParamController {
@Autowired
private TUserMapper tUserMapper;
@RequestMapping("/database")
public String database(HttpServletRequest request){
List<TUser> tUsers = tUserMapper.qryTUser();
System.out.println(tUsers.size());
return "database";
}
}