定义
SSM框架指的是SpringMVC+Spring+Mybatis
创建Spring的目的就是用来替代更加重量级的的企业级Java技术
- 简化Java的开发基于POJO轻量级和最小侵入式开发
- 通过依赖注入和面向接口实现松耦合
- 基于切面和惯例进行声明式编程
- 通过切面和模板减少样板式代码
一、环境+软件下载
tip:以下为本人电脑使用配置,可按需求自行选择其他配置安装。
1.TOMCAT:apache-tomcat-10.0.0-M10
2.JAVA SE(JDK):jdk-11.0.9_windows-x64_bin
3.maven:apache-maven-3.6.0-bin
4.IDEA:ideaIU-2018.3.4.win
【教程】tomcat+jdk安装配置方法详情请戳这里【资源】maven+idea下载请戳这里 提取码:47n6
二、搭建
1.新建项目
Maven->选择jdk->打勾单选选项->选择webapp行->next
这一步打勾“Overwrite
”,选择maven文件包下conf-
settings.xml文件
Finish后进入IDEA界面,代表新建项目成功了~
点击“import changes”
2.完善项目结构
现在我们初始化项目结构如下图:
- .idea文件夹:和helloworld.iml是idea的一些配置文件
- src文件夹:放置项目代码
- pom.xml:maven的配置文件,我们需要的jar包,可以在里面添加依赖,然后maven就会自动下载到本地仓库
最终文件结构需要为:(如果会操作建各种文件和层次,可以直接看图建,并可直接看第三大点)
图片有误,index.jsp应该在jsp文件包下
1.在main下创建两个文件夹:java,resource
具体做法:main右键->new->directory->输入文件夹名称
创建好以后如下图:
java文件包右键->mark directory as->sources root
将resource文件包,按相同办法设为resource Root
结果如下图即可:
** 2.在src/main/resources/目录下创建spring文件夹,在文件夹中再创建spring和springmvc对应的xml配置文件**
在resource下创建文件夹spring
在spring文件夹下创建两个xml文件:spring-context.xml和springmvc-context.xml
3.创建dao、service层对应的package,在src/main/resources/目录下创建mybatis文件夹,并在该文件夹下面创建mybatis的配置文件config.xml
先java右键->new->package->输入名称,例“com.ssm”
同样的操作,在com.ssm下创建三个package:controller,dao,service
然后在resource下创建文件包mybatis
mybatis下创建config.xml文件
4.创建类和接口controller->new->java class
DemoController这里选择class
同理,
在dao包中创建DemoDao空接口(图标为绿色I);
在service包中创建DemoService业务接口,并在service下创建一个包impl,impl包下再创建一个DemoServiceImpl类。
如图:
5.创建网页文件jsp
在src\main\webapp\WEB-INF\目录下,创建jsp文件夹,并在该文件夹中创建一个名字为index.jsp文件
三、补充代码
1.在创建的Web项目的pom.xml文件中添加SSM的web项目依赖,pom.xml文件中的依赖如下:(可自行找其他版本)
pom.xml完整代码:
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--<dependency>-->
<!--<groupId>javax.servlet.jsp</groupId>-->
<!--<artifactId>jsp-api</artifactId>-->
<!--<version>2</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<!-- Spring整合MyBatis -->
<!-- MyBatis中延迟加载需要使用Cglib -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 控制日志输出:结合log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- ********其他****************************** -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
点击“import changes”,就会开始下载。
2.在 src\main\webapp\WEB-INF\web.xml 文件中集成配置spring和springmvc框架
web.xml完整代码:
<!-- 集成Spring框架 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-*.xml</param-value>
</context-param>
<listener>
<!-- 在项目启动前初始化当前web应用环境 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器,可以保证项目不乱码 -->
<filter>
<filter-name>encoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<servlet-name>springmvc</servlet-name>
</filter-mapping>
<!-- 集成SpringMVC框架 -->
<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:spring/springmvc-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.spring-context.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 以下IOC配置:创建对象组合对象之间的关系 -->
<!-- 扫描注解类,需要扫描除springmvc组件controller外的所有组件 -->
<context:component-scan base-package="com.ssm.*" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://hadoop01:3306/ct?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 集成Mybatis框架 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="configLocation" value="classpath:mybatis/config.xml" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" >
<list>
<value>classpath*:mybatis/mapper-*.xml</value>
</list>
</property>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.ssm.dao" />
</bean>
<!-- AOP配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- AOP功能增强器 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- spring默认的回滚策略为java.lang.RuntimeException -->
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception" />
<tx:method name="query*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.hlk..*Service.*(..))"/>
</aop:config>
</beans>
4.springmvc-context.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
<context:component-scan base-package="com.ssm.*" use-default-filters="false" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters" >
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="supportedMediaTypes" >
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" >
<property name="maxUploadSize" value="2097152"/>
<property name="resolveLazily" value="true"/>
</bean>
</beans>
5.src/main/resources/mybatis文件夹中创建的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>
<typeAliases>
</typeAliases>
</configuration>
5.在controller包中创建类DemoController类,内容如下:
(开头package com.hlk.web.controller;中的com.hlk.web换成你项目中java文件包下的第一个包名即可)
DemoController代码:
package com.ssm.controller;
import com.ssm.service.DemoService;
public class DemoController {
private DemoService demoService;
protected String testDemo(){
return "demo";
}
}
6.DemoDao代码:
package com.ssm.dao;
public interface DemoDao {
}
7.DemoService代码:
package com.ssm.service;
public interface DemoService {
}
8.DemoServiceImpl代码:
package com.ssm.service.impl;
import com.ssm.dao.DemoDao;
import com.ssm.service.DemoService;
public class DemoServiceImpl implements DemoService {
}
9.index.jsp:
<%@page pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>help me i am crazy!</h1>
</body>
</html>
四、配置tomcat服务器
1.点击Add configuration->tomcat server->local
除名字外,URL,httpport 和JMX port使用默认,JRE用自己电脑安装的。
如果第一次配置点击configure
选择电脑下载好的tomcat版本,并命名保存。
2.点击Project Structure/fix
Artifacts->web Application: Exploded->from modeules
选定当前项目->一路Apply、ok
选择刚刚创建的那个
重要的地方来了,把Application context修改的只剩一个‘/’即可。
且转回到Server栏,将URL也删掉一部分,确定。
运行,结果如图:
附上一个ssm项目源码:链接: https://pan.baidu.com/s/1OPIEDG0W2d_CiNiaENoM1g 提取码:ssmh