目录
1.SpringMVC项目搭建流程
2.检查一下 idea 插件
3.创建项目需要使用的文件夹目录
4.检查WebApp目录是否正确
5.检查Webapp目录
6.引入所有相关 SSM 框架的依赖包(最完全版本)
7.创建 Spring 以及SpringMVC配置文件
8.修改Web.xml文件中的相关配置
9. 配置SpringMVC相关内容
10. MyBatis 集成Spring、SpringMVC的配置
11.SSM完整调用demo案例
1.SpringMVC项目搭建流程
2.检查一下idea插件
Spring以及SpringMVC相关插件
lombok插件
MyBatis插件
3.创建项目需要使用的文件夹目录
4.检查WebApp目录是否正确
5.检查Webapp目录
6.引入所有相关SSM框架的依赖包(最完全版本)
<dependencies>
<!-- springMVC、Spring相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring- jdbc </artifactId>
<version>.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>.8.9</version>
</dependency>
<!--测试-->
<dependency>
<groupId> junit </groupId>
<artifactId>junit</artifactId>
<version>.11</version>
<scope>test</scope>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slfj</groupId>
<artifactId>slfj-log4j12</artifactId>
<version>.7.21</version>
</dependency>
<!--JEE-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>.2</version>
</dependency>
<!-- mysql 驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>.0.22</version>
</dependency>
<!-- Fastjoson JSON处理工具 --><!-- TODO:gai -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>.2.78</version>
</dependency>
<!-- 文件上传下载-->
<dependency>
<groupId>org. apache .commons</groupId>
<artifactId>commons-lang</artifactId>
<version>.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>.18.22</version>
<scope>provided</scope>
</dependency>
<!-- 数据校验 -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>.1.0.CR2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>.1.0.Final</version>
</dependency>
<!-- @ResponesBody的新解决方案-jackson包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>.12.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>.12.3</version>
</dependency>
<!-- Swagger - fasterxml -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>.12.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>.5.1</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>.5.7</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>.3.0</version>
</dependency>
<!-- 阿里 的druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>.2.8</version>
<scope>compile</scope>
</dependency>
<!--日志-->
<dependency>
<groupId> log4j </groupId>
<artifactId>logj</artifactId>
<version>.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>.2</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger</artifactId>
<version>.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>.6.1</version>
</dependency>
<!-- github分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>.2.1</version>
</dependency>
</dependencies>
7.创建Spring以及SpringMVC配置文件
把生成的两个配置文件拖拽到resource目录下
8.修改Web.xml文件中的相关配置
(1)在resource目录创建Mybatis配置文件(名字叫mybatisConfig.xml)
先编写根标签即可,留着后面写
<?xml version=".0" encoding="UTF-8"?>
<beans xmlns="#;
xmlns:xsi="#; xmlns:p="#34;
xmlns:context="#;
xmlns:mvc="#;
xsi:schemaLocation="
#;>
<!-- 集成SpringMVC时Mybatis配置文件也将使用SpringBean的方式来配置 -->
</beans>
( 2 )修改web.xml-加载mybatis配置文件以及项目名称
<web-app>
<!-- 修改项目名称与创建的项目文件同名即可 -->
<display-name>CZ_SSM</display-name>
<!-- 当spring容器加载时就会加载mybatis配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 指向mybatis配置文件路径 -->
<param-value>classpath:mybatisConfig.xml</param-value>
</context-param>
(3)修改web.xml-SpringMVC前端 控制器 配置
<!-- SpringMVC-前端控制器Servlet配置 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 当前端控制器servlet实例时,加载SpringMVC-前端控制器配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指向生成的dispatcher-servlet.xml -->
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 拦截所有客户端请求进入mvc的前端控制器 -->
<url-pattern>/</url-pattern>
</ servlet -mapping>
(4)修改web.xml-添加Spring 字符编码 过滤器
<!-- 字符编码过滤器 -->
<!-- spring字符编码过滤器 -->
<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-</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(5)修改web.xml- 添加SpringMVC-REST请求规范过滤器
<!-- REstFul规范请求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(6)修改web.xml-添加阿里druid连接池数据监控中心url路径映射
<!-- 阿里的druid连接池数据监控中心servlet -->
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
<!-- 项目上下文/druid 进入阿里数据源监控中心 -->
9.配置SpringMVC相关内容
(1)各自小组创建自己的业务相关包,一定要在根目录下创建
附录:控制器层、业务接口层、业务实现层代码demo
/*控制器层*/package com.czdsj.base.controller;
import com.czdsj.base.service.BaseService;
import lombok.extern.slfj.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind. annotation . Request Mapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax .annotation.Resource;
/**
* 学生基础信息采集模块-控制器层
*/6/1
*/@Controller
@RequestMapping("/base")
@Slfj //日志注解 自动生成一个log对象
public class BaseController {
/**
* 自动注入接口实现层, name是实现层类型首字母小写即可
*/ @Resource(name = "baseServiceImpl")
private BaseService baseService;
/**
* 页面跳转方法:返回值是 String 类型返回一个逻辑视图名
* 页面跳转方法不需要写@ResponseBody注解 ,请求类型是GET类型
* @return
*/ @RequestMapping(value = "/toLogin",method = RequestMethod.GET)
public String toLogin(){
log.info("页面跳转-到登录页面");
return "逻辑视图名";
}
}
/*业务接口层*/package com.czdsj.base.service;
/**
* 学生基础信息采集-业务接口层
*/6/1
*/public interface BaseService {
}
/*业务接口层*/package com.czdsj.base.service;
/**
* 学生基础信息采集-业务接口层
*/6/1
*/public interface BaseService {
}
(2)配置SpringMVC配置文件(dispatcher-servlet.xml)
完整引入内容案例参考:
<?xml version=".0" encoding="UTF-8"?>
<beans xmlns="#;
xmlns:xsi="#;
xmlns:mvc="#;
xmlns:context="#;
xsi:schemaLocation=" #;>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 最好手动去打 -->
<!-- 依赖注入、控制器、service、@Valid -->
<mvc:annotation-driven />
<!-- 扫描注解,加载实例 : @Component @Controller @Service -->
<context:component-scan base-package="com.czdsj" />
<!-- 文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value=""/>
<property name="defaultEncoding" value="UTF-" />
</bean>
<!-- 静态资源文件引入 -->
<mvc:resources location="/static/" mapping="/static/**" />
</beans>
(1)配置视图解析器
配置视图文件夹
配置视图解析器
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
(2)配置自动包扫描加入ioc容器
<!-- 最好手动去打 -->
<!-- 依赖注入、控制器、service、@Valid -->
<mvc:annotation-driven>
<!-- 扫描注解,加载实例 : @Component @Controller @Service -->
<context:component-scan base-package="com.czdsj" />
(3)文件上传下载解析器
<!-- 文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value=""/>
<property name="defaultEncoding" value="UTF-" />
</bean>
(4)静态资源引入
1.创建一个静态资源文件夹
一定要创建在webapp目录下,与WEB-INF目录同级
2.在配置文件中配置静态资源路径
<!-- 静态资源文件引入 -->
<mvc:resources location="/static/" mapping="/static/**" />
3.配置maven打包静态资源
在pom.xml文件中<build>标签下
<!-- maven静态资源打包 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.js</include>
<include>**/*.css</include>
<include>**/*.html</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.js</include>
<include>**/*.css</include>
<include>**/*.html</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
10.Mybatis集成Spring、SpringMVC的配置
(1)引入依赖(注意不要重复引入,之前的demo中已经帮大家引入了)
<!-- mybatis/spring包 实现Spring的ORM框架接口 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>.3.0</version>
</dependency>
(2)编写Mybatis配置文件
1.创建一个外部配置mybatis数据源以及连接属性的配置文件(db.properties)
创建在resource目录下
#mybatis数据源以及连接池相关属性配置文件
#mysql驱动
driver=com.mysql.cj.jdbc.Driver
#mysql的jdbc连接地址
url=jdbc:mysql://localhost:/huawei?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
#mysql数据库的用户名
userName=root
#mysql数据库的密码
pwd=root
#连接池初始化连接数量
initialSize=
#同一时间点允许活跃的最大连接数
maxActive=
#最小连接池数量(当没有连接时自动创建新连接的数量)
minIdle=
#每个连接请求的最长时间(单位毫秒)
maxWait=
2.在mybatis配置文件中引入properties的配置
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- value值指向你的外部mybatis属性配置文件 -->
<property name="location" value="classpath:db.properties" />
</bean>
3.mybatis配置文件配置数据源(基于阿里巴巴druid连接池的配置)
<!-- 配置连接池(阿里druid连接池) -->
<!-- !!!!!写着${}的地方都要修改成你自己的配置引入 !!!!!!-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${initialSize}" />
<property name="minIdle" value="${minIdle}" />
<property name="maxActive" value="${maxActive}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${maxWait}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="" />
<property name="testWhileIdle" value="true" />
<!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="" />
<!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
<property name="defaultAutoCommit" value="true" />
<!-- 验证连接有效与否的SQL,不同的数据配置不同 mysql:select ;oracle : select 1 from dual -->
<property name="validationQuery" value="select from dual" />
<property name="filters" value="wall,stat" />
<property name="proxyFilters">
<list>
<ref bean="logFilter" />
<ref bean="stat-filter" />
</list>
</property>
</bean>
<!-- 慢SQL记录 -->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<!-- 慢sql时间设置,即执行时间大于毫秒的都是慢sql -->
<property name="slowSqlMillis" value=""/>
<property name="logSlowSql" value="true"/>
</bean>
<bean id="logFilter" class="com.alibaba.druid.filter.logging.SlfjLogFilter">
<property name="dataSourceLogEnabled" value="true" />
<property name="statementExecutableSqlLogEnable" value="true" />
</bean>
4.配置创建SQLSessionFactory工厂类
<!-- spring和mybatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- ref指向数据源配置id -->
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描包中的类生成别名,如:com.a.User包 别名可以是User或user -->
<property name="typeAliasesPackage" value="com.czdsj" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/czdsj/**/mybatis/dao/*.xml"></property>
</bean>
5.配置sqlSession创建规则
<!-- DAO接口所在的包名,spring会自动查找其下的类,!注意这句 value="sqlSessionFactory",不是ref而是value -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指向mybatis接口文件所在的包 -->
<property name="basePackage" value="com.czdsj.**.mybatis.dao" />
<!-- value指向配置sqlSessionFactory类的id -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
6.配置mybatis分页
a.引入pagehelper分页工具(注意不要重复导入依赖,之前案例中已经导入)
<!-- github分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>.2.1</version>
</dependency>
b.在mybatis配置文件中配置分页拦截器
<!-- pageHepler分页拦截器 -->
<bean id="pageInterceptor" class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
<prop key="supportMethodsArguments">true</prop>
<prop key="params">count=countSql</prop>
</props>
</property>
</bean>
c.在SQLSessionFactory配置中添加分页工具配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/huawei/mybatis/dao/*.xml"></property>
<!-- 自动扫描包中的类生成别名,如:com.a.User包 别名可以是User或user -->
<property name="typeAliasesPackage" value="com.czdsj" />
<!-- 附加的工具包 指向分页工具拦截器的id -->
<property name="plugins" ref="pageInterceptor" />
</bean>
7.配置@Mapper注解的自动扫描
作用:在业务层中要使用mybatis的mapper接口实例时可以从ioc容器中自动注入调用,在mybatis配置文件中配置
<!-- 自动扫描@Mapper注解,将mybatis接口自动实现实例且交由给springMVC管理 -->
<context:component-scan base-package="com.czdsj.**.mybatis.dao">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
11.SSM完整调用demo案例:
(1)编写数据库与java映射实体对象
package com.czdsj.base.mybatis.domain;
import lombok.Data;
/**
* 数据库Student表与JAVA映射的实体类
*/6/1
*/@Data
public class StudentDo {
private String code;
private String userName;
private Integer age;
}
(2)编写mybatis映射接口(注意:一定要加@Mapper注解在类上)
package com.czdsj.base.mybatis.dao;
import com.czdsj.base.mybatis.domain.StudentDo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* Mybatis接口层
*/6/1
*/@Mapper
public interface BaseMapper {
/**
* 查询出所有的学生信息-接口方法
* @return
*/ List<StudentDo> getStudentList();
}
(3)编写接口同名对应的xml映射文件(注意:要在同一个包且名字完全相同)
<?xml version=".0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper.0//EN" "#34;>
<mapper namespace="com.czdsj.base.mybatis.dao.BaseMapper">
<select id="getStudentList" resultType="studentDo">
select * from student
</select>
</mapper>
(4)编写controller层
package com.czdsj.base.controller;
import com.czdsj.base.mybatis.domain.StudentDo;
import com.czdsj.base.service.BaseService;
import lombok.extern.slfj.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
/**
* 学生基础信息采集模块-控制器层
*/6/1
*/@Controller
@RequestMapping("/base")
@Slfj //日志注解 自动生成一个log对象
public class BaseController {
/**
* 自动注入接口实现层, name是实现层类型首字母小写即可
*/ @Resource(name = "baseServiceImpl")
private BaseService baseService;
/**
* 页面跳转方法:返回值是String类型返回一个逻辑视图名
* 页面跳转方法不需要写@ResponseBody注解 ,请求类型是GET类型
* @return
*/ @RequestMapping(value = "/toLogin",method = RequestMethod.GET)
public String toLogin(){
log.info("页面跳转-到登录页面");
return "index";
}
/**
* 查询学生的所有数据-返回json
* @return
*/ @RequestMapping(value = "/getStudentList",method = RequestMethod.GET)
@ResponseBody
public Object getStudentListData(){
log.info("查询学生的所有数据");
//控制器层调用-业务接口
List<StudentDo> studentList = baseService.getStudentList();
return studentList;
}
}
(5)编写业务层接口
package com.czdsj.base.service;
import com.czdsj.base.mybatis.domain.StudentDo;
import java.util.List;
/**
* 学生基础信息采集-业务接口层
*/6/1
*/public interface BaseService {
/**
* 获取学生的所有数据
* @return
*/ List<StudentDo> getStudentList();
}
(6)编写业务层实现-调用mybatis接口实例
package com.czdsj.base.service.impl;
import com.czdsj.base.mybatis.dao.BaseMapper;
import com.czdsj.base.mybatis.domain.StudentDo;
import com.czdsj.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 学生基础信息采集-业务实现层
*/6/1
*/@Service
public class BaseServiceImpl implements BaseService {
/**
* 通过容器自动注入mybatis接口的实现
*/ @Autowired
private BaseMapper baseMapper;
/**
* 获取学生的所有数据 业务接口层调用实现层方法
* @return
*/ @Override
public List<StudentDo> getStudentList() {
//实现层方法-调用mybatis接口
List<StudentDo> studentList = baseMapper.getStudentList();
return studentList;
}
}