SSM 快速搭建笔记
1、SSM组成部分 spring+springmvc+mybatis
2、本教程基于idea和maven 来实现ssm的搭建工作
3、开始:
1. 使用idea创建一个maven web 项目。注意!:一定要选取自己已经配置好的maven环境【带阿里云的】
2. 检项目结构是否正常,在控制台中是否会出现build success字样,如果没有视为创建失败
- 找到醒目中的pox.xml(项目依赖管理文件),并写入如下代码,一定在dependencies标签中添加
<!-- ssm框架终身可用 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- c3p0 数据库连接池 start -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- c3p0 数据库连接池 end -->
<!-- jsp-servlet支持 start -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- jsp-servlet支持 end -->
<!-- 标签库 start -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.1</version>
</dependency>
<!-- 标签库 end -->
<!-- apache的commons系列包 start -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- apache的commons系列包 end -->
<!-- apache 工具包 start -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
<!-- apache 工具包 end -->
<!-- spring start -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring start -->
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- mybatis start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.0.1</version>
</dependency>
<!-- mybatis end -->
<!--日志管理 start-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<!--日志管理 end-->
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<!--getter setter 注解 start-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!--getter setter 注解 end-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<!-- 导入数据校验jar包 -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.4.2.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!--json start-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.verson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.verson}</version>
</dependency>
<!--json end-->
- 找到pox.xml中的properties标签 并填入如下代码
<spring.version>4.0.6.RELEASE</spring.version>
<jackson.verson>2.8.7</jackson.verson>
-
选中main文件夹 创建java源文件 和resources配置文件夹并将其标记成对应的类型
-
在java 源文件夹中创建如下包
- cn
- kgc
- ssm
- common (存放项目中所有的公共类,比如统一返回对象)
- dao (存放到层接口,起名方式为 *Mapper.java)
- service (存放项目中的所有的业务逻辑接口)
- impl (存放接口的具体实现方法)
- controller (控制层 负责调用service层来实现业务的调用)
- uitl (工具类,比如分页工具类,日期格式转换之类的工具)
- entity (实体类,主要负责MySQL中的字段和java属性的一一对应)
- vo (view-object 视图对象,当页面中所要的字段和实体类冲突时,可使用vo来解决)
- ssm
- kgc
- 在resources文件夹中创建如下内容:
-
resources
-
mappers (【文件夹】存放所有的mapper映射文件,这里的映射文件要和到层接口一一对应)
-
applicationContext.xml (spring - mybatis 配置文件,主要负责全项目的注解扫描,对象创建等服务)
-
applicationContext-mvc.xml (spring mvc 方向的配置文件,主要负责文件上传,视图解析器,格式化日期格式,消息转换器等配置内容)
-
db.properties (数据库信息配置文件)
-
logback-xml (日志记录配置文件)
-
- 编辑web.xml 【全局最大的配置文件,在项目启动时会优先加载此配置文件】
1、更换配置文件的头部信息,因为2.3不支持el表达式
<?xml version="1.0" encoding="UTF-8"?>
<!-- 把2.3换成3.0 是因为2.3不支持el表达式 -->
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
2、写入如下代码
<!-- 监听器上下文启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring 容器加载配置文件,这里一定要写对名字,不带MVC的那个 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 陪孩子spring mvc 相关内容 -->
<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-mvc.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>
<!-- 添加过滤器 解决中文乱码,终身都可使用 -->
<!-- 配置过滤器,如果页面在传递数据时乱码,配置这个贼好使 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<!-- 这里一定要写 /* 过滤所有的请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
- applicationContext.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 http://www.springframework.org/schema/context/spring-context.xsd">
<!-- todo 开启注解扫描 -->
<context:component-scan base-package="cn.kgc"/>
<!-- todo 引入配置文件 由于要和MVC整合 所以需要添加classpath 来确定配置文件的固定位置 -->
<context:property-placeholder location="classpath*:db.properties"/>
<!-- TODO 配置数据源 连接数据库 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="user" value="${jdbc.username}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<!-- TODO 配置会话工厂 用于执行sql语句开关资源 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- ref name 配置会话工厂的数据源 引入配置 -->
<property name="dataSource" ref="dataSource"/>
<!-- value naem 配置实体类别名【用户不大】 手动配置内容 -->
<property name="typeAliasesPackage" value="cn.kgc.ssm.entity"/>
<!-- 配置mapper映射文件,这里同样采用固定定位的方式来查找配置文件 -->
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
</bean>
<!-- 扫描dao 层接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.kgc.ssm.dao"/>
</bean>
<!-- 配置事务管理 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 由于这次例子没有数据库增删改的操作,所以不用写声明事务 -->
</beans>
- applicationContext-mvc.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: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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="cn.kgc"/>
<!-- 标注控制层 -->
<mvc:annotation-driven conversion-service="conversionService2"/>
<!-- 配置静态资源 **代表路径 本地路径 -->
<mvc:resources mapping="/statics/**" location="/statics/"/>
<!-- 取消对静态文件的拦截 -->
<!-- <mvc:annotation-driven />-->
<!--
<!– TODO 全局异常处理 –>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!– 配置错误信息的参数名称 –>
<property name="exceptionAttribute" value="exception"/>
<!– 配置展示错误信息的页面 –>
<property name="defaultErrorView" value="error"/>
</bean>
-->
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- TODO 配置date转换 -->
<bean id="conversionService2" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="cn.kgc.ssm.utils.DateConvert"/>
</set>
</property>
</bean>
<!-- 配置文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置最大文件上传大小 -->
<property name="maxUploadSize" value="4096000000"/>
<!-- 设置文件编码格式 -->
<property name="defaultEncoding" value="utf-8"/>
<property name="maxInMemorySize" value="4096"/>
</bean>
</beans>
至此所有的SSM配置工作完成
4、项目中注解的解释
1. @Service 向spring容器注入当前类为service层,用于service的实现类上!
2. @Controller 标注当前类为控制层
3. @RequestMapping 这个注解比较厉害可以修饰类也可以修饰方法,他的主要功能是只要赔标注,就说明可以解决网络请求和响应
4. @Resources 注入其他类,【这里特别容易空指针,请注意扫描内容】
5. @ResponseBody 表示当前方法返回的内容不走视图解析器
6. @RequestParam 主要用来接收前端传来的参数,主要负责当前端参数名称和后端参数名称不对称的情况,如果mvc的一个参数被修饰了此注解参数默认为必传项
7. @PathVariable 主要用于restful风格,从路径中获取参数