SSM整合
文章已托管到GitHub,大家可以去GitHub查看阅读,欢迎老板们前来Star!
搜索关注微信公众号 【码出Offer】 领取各种学习资料!
一、创建一个Maven项目
File -> NewProject |
---|
image-20200711100406109 |
创建Maven项目 |
image-20200711100511187 |
二、声明war打包方式并创建web项目目录结构
2.1 创建web项目结构
有关于Maven不了解的小伙伴不要灰心,请参考Maven教程
声明war包打包方式,即在pom.xml文件中加入一行标签
,随后创建web项目目录结构!
war
手动构建web项目结构 |
---|
基于main目录下创建webapp文件夹 |
image-20200617124249878 |
image-20200617124319926 |
基于webapp目录创建WEB-INF文件夹 |
image-20200617124624648 |
基于WEB-INF目录创建web.xml文件 |
image-20200617125038803 |
image-20200617125055216 |
xml文件内容展示 |
image-20200617125729115 |
基于webapp目录创建index.jsp文件 |
image-20200617125613861 |
目录展示 (完整的web项目目录结构) |
image-20200617125808227 |
2.2 所需依赖
javax.servlet
servlet-api
2.5
javax.servlet
jsp-api
2.0
javax.servlet
jstl
1.2
三、引入tomact服务器并设置
关于Tomact服务的引入,需要我们手动添加tomact服务
添加tomact服务后,如果对tomact服务器在IDEA中的开发流程不熟悉的小伙伴,不要灰心。请参考tomact服务器基础和开发步骤即可,此文章中详细讲到了关于tomact的各种知识点!
添加tomact服务 |
---|
image-20200617131801103 |
四、spring+MyBatis(Dao层/Mapper层)
4.1 jdbc.properties
JDBC配置文件
#jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/temp?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
4.2 spring-mybatis.xml(Dao/Mapper层相关配置)
功能组件: JDBC、Durid连接池、SQLSessionFactory(等价于Connection,可生产连接,内需添加注册Mapper、别名和关联mybaits配置文件)、Dao/Mapper层注解扫描器
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
"classpath:jdbc.properties"/>
"dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
"driverClassName" value="${jdbc.driver}"/>
"password" value="${jdbc.password}"/>
"url" value="${jdbc.url}"/>
"username" value="${jdbc.username}"/>
"initialSize" value="1"/>
"minIdle" value="1"/>
"maxActive" value="3"/>
"maxWait" value="60000"/>
"timeBetweenEvictionRunsMillis" value="60000"/>
"minEvictableIdleTimeMillis" value="300000"/>
"sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
"dataSource" ref="dataSource"/>
"mapperLocations" value="classpath:com/ziphtracks/mapper/*.xml"/>
"typeAliasesPackage" value="com.ziphtracks.bean"/>
"configLocation" value="classpath:mybatis-config.xml">
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
"basePackage" value="com.ziphtracks.mapper"/>
4.3 mybatis-congfig.xml(分页和缓存)
功能组件: 分页查询、二级缓存
"1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
"cacheEnabled" value="true"/>
"com.github.pagehelper.PageInterceptor">
4.4 Mapper.xml
书写SQL语句的Mapper.xml
"1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"">
4.5 所需依赖
所需依赖: Spring核心、Spring提供的JDBC、Spring与MyBatis继承、MyBatis核心、MySQL驱动、连接池Durid、分页插件、Lombok类库、Spring提供的Junit集成、Junit4测试
org.springframework
spring-context
5.1.6.RELEASE
org.springframework
spring-jdbc
5.1.6.RELEASE
org.mybatis
mybatis-spring
1.3.1
org.mybatis
mybatis
3.4.6
mysql
mysql-connector-java
5.1.47
com.alibaba
druid
1.1.16
com.github.pagehelper
pagehelper
5.1.11
org.projectlombok
lombok
1.18.12
org.springframework
spring-test
5.1.6.RELEASE
junit
junit
4.12
五、spring整合(Service层)
5.1 spring-context.xml(Service层)
Servie层整合组件: 注解扫描器(扫描Service层注解)、事务管理器、事务驱动器、事务增强(注解和事务增强可选)、开启AOP注解
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
package="com.ziphtracks" use-default-filters="true">
"annotation" expression="org.springframework.stereotype.Controller"/>
"txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
"dataSource" ref="dataSource"/>
"txManager"/>
5.2 logback.xml(Logback+SLF4j日志)
实现日志管理功能
占位符 | 描述 |
---|---|
%d{yyyy-MM-dd HH:mm:ss.SSS} | 日期 |
%5p | 日志级别,5位字符长度显示,如果内容占不满5位则内容右对齐并在左侧补空格 |
%-5p | 5位字符长度显示日志级别,如果内容占不满5位则内容左对齐并在右侧补空格 -代表左对齐 |
%logger | 日志所在包和类 |
%M | 日志所在方法名 |
%L | 日志所在代码行 |
%m | 日志正文 |
%n | 换行 |
"1.0" encoding="UTF-8"?>
"true" scanPeriod="60 seconds" debug="true">
"log.path" value="D:/log" />
"CONSOLE_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} |-[%-5p] in %logger.%M[line-%L] -%m%n"/>
"CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
class="ch.qos.logback.classic.filter.ThresholdFilter">
debug
${CONSOLE_LOG_PATTERN}
UTF-8
"file" class="ch.qos.logback.core.FileAppender">
${log.path}/hello2.log
${CONSOLE_LOG_PATTERN}
"file2" class="ch.qos.logback.core.rolling.RollingFileAppender">
${log.path}/hello.log
${CONSOLE_LOG_PATTERN}
UTF-8
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
${log.path}/hello-%d{yyyy-MM-dd}.%i.log
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
8kb
1
"trace">
"CONSOLE"/>
"file"/>
"file2"/>
5.3 spring-quartz.xml(Quartz定时任务调度器)
实现定时任务调度管理功能
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
"myJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
"name" value="job2004"/>
"group" value="job_group2004"/>
"jobClass" value="com.mylifes1110.job.MyJob2004"/>
"myJob2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
"name" value="job2005"/>
"group" value="job_group2004"/>
"jobClass" value="com.mylifes1110.job.MyJob2005"/>
"cronTrigger2004" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
"name" value="trigger2004"/>
"group" value="trigger_group2004"/>
"jobDetail" ref="myJob"/>
"cronExpression" value="* * * * * ?" />
"cronTrigger2005" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
"name" value="trigger2005"/>
"group" value="trigger_group2005"/>
"jobDetail" ref="myJob2"/>
"cronExpression" value="* * * * * ?" />
"scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
"triggers">
"quartzProperties">
# 指定调度器名称,实际类型为:QuartzScheduler
org.quartz.scheduler.instanceName = MyScheduler2004
# 指定连接池
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
# 连接池线程数量
org.quartz.threadPool.threadCount = 11
# 优先级
org.quartz.threadPool.threadPriority = 5
# 不持久化job
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
5.4 spring-aop.xml(配置AOP)
SpringAOP面向切面编程(动态代理)
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
"userService" class="com.mylifes1110.service.impl.UserServiceImpl"/>
"userServiceProxy" class="com.mylifes1110.advice.MyAdvice"/>
"userServiceProxy1" class="com.mylifes1110.advice.AfterReturningAdvices"/>
"userServiceProxy2" class="com.mylifes1110.advice.MethodInterceptors"/>
"increaseUserService" expression="execution(* selectUserById())"/>
"userServiceProxy2" pointcut-ref="increaseUserService"/>
5.5 所需依赖
所需依赖: Springtx事务处理、logback日志管理依赖(传递依赖导入slf4j和logback-core)、Quartz任务调度依赖、Spring与Quartz任务调度
org.springframework
spring-tx
5.1.6.RELEASE
ch.qos.logback
logback-classic
1.2.3
org.quartz-scheduler
quartz
2.2.3
org.springframework
spring-context-support
5.1.6.RELEASE
六、springMVC整合
6.1 web.xml配置(前端控制器)
前端控制器组件: 加载SpringMVC工厂、解决同名Servlet与html冲突问题、加载Spring工厂相关配置、二维码配置
目的: 确保SpringMVC工厂和Spring工厂同时加载,否则运行失败
"1.0" encoding="UTF-8"?>
"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">
mvc
class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>mvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>defaultservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-*.xmlparam-value>
context-param>
<servlet>
<servlet-name>capservlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServletservlet-class>
<init-param>
<param-name>kaptcha.borderparam-name>
<param-value>noparam-value>
init-param>
<init-param>
<param-name>kaptcha.textproducer.char.lengthparam-name>
<param-value>4param-value>
init-param>
<init-param>
<param-name>kaptcha.textproducer.char.stringparam-name>
<param-value>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789param-value>
init-param>
<init-param>
<param-name>kaptcha.background.clear.toparam-name>
<param-value>211,229,237param-value>
init-param>
<init-param>
<param-name>kaptcha.session.keyparam-name>
<param-value>captchaparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>capservlet-name>
<url-pattern>/captchaurl-pattern>
servlet-mapping>
web-app>
6.2 spring-mvc.xml(SpringMVC组件配置)
SpringMVC组件: 注解扫描器(Controller层)、识别注解驱动、FastJson转换器、视图解析器、静态资源加载器、异常解析器、拦截器、上传解析器
"http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
package="com.ziphtracks" use-default-filters="false">
"annotation" expression="org.springframework.stereotype.Controller"/>
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
"supportedMediaTypes">
application/json
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
"prefix" value="/"/>
"suffix" value=".jsp"/>
default-servlet-handler/>
class="com.ziphtracks.exception.resolver.MyExResolver">
"/inter/test1"/>
"/inter/test2"/>
"/inter/test*"/>
"/inter/**"/>
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
6.3 所需依赖
所需依赖: Jackson(Json解析)、SpringMVC核心(webmvc)、IO、文件上传(因文件上传依赖与其他依赖重名所以需要排除其他依赖)、FastJson(Json解析)、二维码所需依赖(排除其他影响的二维码依赖)
com.fasterxml.jackson.core
jackson-databind
2.9.8
org.springframework
spring-webmvc
5.1.6.RELEASE
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.3.3
javax.servlet
servlet-api
com.alibaba
fastjson
1.2.54
com.github.penggle
kaptcha
2.3.2
javax.servlet
javax.servlet-api
七、IDEA创建模板
新建模板 |
---|
image-20200724184630438 |
创建模板并添加代码/名称/文件类型 |
image-20200724184952111 |
使用模板 |
image-20200724185317354 |
SSM所需依赖模板(pom.xml)
"1.0" encoding="UTF-8"?>
"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">
4.0.0
org.example
platfrom
1.0-SNAPSHOT
war
javax.servlet
servlet-api
2.5
javax.servlet
jsp-api
2.0
javax.servlet
jstl
1.2
org.springframework
spring-context
5.1.6.RELEASE
org.springframework
spring-jdbc
5.1.6.RELEASE
org.mybatis
mybatis-spring
1.3.1
org.mybatis
mybatis
3.4.6
mysql
mysql-connector-java
5.1.47
com.alibaba
druid
1.1.16
com.github.pagehelper
pagehelper
5.1.11
org.projectlombok
lombok
1.18.12
org.springframework
spring-test
5.1.6.RELEASE
junit
junit
4.12
org.springframework
spring-tx
5.1.6.RELEASE
ch.qos.logback
logback-classic
1.2.3
org.quartz-scheduler
quartz
2.2.3
org.springframework
spring-context-support
5.1.6.RELEASE
com.fasterxml.jackson.core
jackson-databind
2.9.8
org.springframework
spring-webmvc
5.1.6.RELEASE
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.3.3
javax.servlet
servlet-api
com.alibaba
fastjson
1.2.54
com.github.penggle
kaptcha
2.3.2
javax.servlet
javax.servlet-api