SSM项目整合基本步骤
Posted 勇闯天涯zfc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM项目整合基本步骤相关的知识,希望对你有一定的参考价值。
SSM项目整合
1、基本概念
1.1、Spring
Spring 是一个开源框架, Spring 是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作 Expert One-On-One J2EE Development and Design 中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。 Spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情。然而, Spring 的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。 简单来说, Spring 是一个轻量级的控制反转( IoC )和面向切面( AOP )的容器框架。
1.2、SpringMVC
Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 里面。 Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
1.3、MyBatis
MyBatis 本是 Apache 的一个开源项目iBatis , 2010 年这个项目由 apache software foundation 迁移到了 google code ,并且改名为 MyBatis 。 MyBatis 是一个基于 Java 的 持久层框架。 iBATIS 提供的持久层框架包括 SQL Maps 和 Data Access Objects ( DAO ) MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。 MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs ( Plain Old Java Objects ,普通的 Java 对象)映射成数据库中的记录。
2、SSM 项目整合——系统结构图
3、SSM项目结构搭建
3.1、需要的Jar包
①Spring:
spring-aop-4.3.0.RELEASE.jar
spring-aspects-4.3.0.RELEASE.jar
spring-beans-4.3.0.RELEASE.jar
spring-context-4.3.0.RELEASE.jar
spring-context-support-4.3.0.RELEASE.jar
spring-core-4.3.0.RELEASE.jar
spring-expression-4.3.0.RELEASE.jar
spring-jdbc-4.3.0.RELEASE.jar
spring-tx-4.3.0.RELEASE.jar
spring-web-4.3.0.RELEASE.jar
spring-webmvc-4.3.0.RELEASE.jar
spring-webmvc-portlet-4.3.0.RELEASE.jar
spring-websocket-4.3.0.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
③MyBatis:
mybatis-3.1.1.jar
log4j-1.2.16.jar
mybatis-spring-1.2.1.jar
④文件上传/下载:
commons-fileupload-1.2.2.jar
commons-io-2.4.jar
⑤C3P0数据源:
c3p0-0.9.5.1.jar
mchange-commons-java-0.2.10.jar
classmate-0.8.0.jar
⑥mysql:
mysql-connector-java-5.0.8-bin.jar
⑦JSTL:
taglibs-standard-compat-1.2.5.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar
taglibs-standard-spec-1.2.5.jar
⑧JSON:
jackson-annotations-2.6.0.jar
jackson-core-2.6.0.jar
jackson-databind-2.6.0.jar
⑨Hibernate Validate:
hibernate-validator-4.3.0.Final.jar
validation-api-1.0.0.GA.jar
validation-api-1.1.0.CR1.jar
jboss-logging-3.1.0.CR2.jar
jboss-logging-3.1.1.GA.jar
3.2、SSM项目的目录结构
3.3、web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 id="WebApp_ID" version="3.1"> 6 <display-name>SSM_01</display-name> 7 <welcome-file-list> 8 <welcome-file>index.jsp</welcome-file> 9 </welcome-file-list> 10 11 12 13 <!-- 1、配置 Spring 配置文件的名称和位置 --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:spring/applicationContext.xml</param-value> 17 </context-param> 18 <!-- ContextLoaderListener:监听器 --> 19 <!--当 web项目启动的时候,会把 Spring IOC 容器获取到,并放入 application 域中 --> 20 <listener> 21 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 22 </listener> 23 24 25 26 <!-- 2、配置 SpringMVC 的前端控制器(要过滤所有的请求) --> 27 <servlet> 28 <servlet-name>springDispatcherServlet</servlet-name> 29 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 30 <!-- 配置 SpringMVC 配置文件的名称和位置 --> 31 <init-param> 32 <param-name>contextConfigLocation</param-name> 33 <param-value>classpath:springmvc/springMvc.xml</param-value> 34 </init-param> 35 <load-on-startup>1</load-on-startup> 36 </servlet> 37 38 39 40 <servlet-mapping> 41 <servlet-name>springDispatcherServlet</servlet-name> 42 <!-- SpringMVC 的前端控制器过滤所有的请求 --> 43 <url-pattern>/</url-pattern> 44 </servlet-mapping> 45 46 47 48 <!-- 3、配置 CharacterEncodingFilter:映射出现乱码 --> 49 <filter> 50 <filter-name>encodingFilter</filter-name> 51 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 52 <async-supported>true</async-supported> 53 <init-param> 54 <param-name>encoding</param-name> 55 <param-value>UTF-8</param-value> 56 </init-param> 57 </filter> 58 <filter-mapping> 59 <filter-name>encodingFilter</filter-name> 60 <url-pattern>/*</url-pattern> 61 </filter-mapping> 62 63 64 65 <!-- 4、配置 HiddenHttpMethodFilter:支持 REST 风格的 CRUD --> 66 <filter> 67 <filter-name>HiddenHttpMethodFilter</filter-name> 68 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 69 </filter> 70 71 72 73 <filter-mapping> 74 <filter-name>HiddenHttpMethodFilter</filter-name> 75 <url-pattern>/*</url-pattern> 76 </filter-mapping> 77 </web-app>
3.4、applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 7 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 8 <!-- 1、配置自动扫描的(Spring IOC 容器不扫描Controller) --> 9 <context:component-scan base-package="cn.com.zfc.ssm"> 10 <context:include-filter type="annotation" 11 expression="org.springframework.stereotype.Component" /> 12 <context:include-filter type="annotation" 13 expression="org.springframework.stereotype.Repository" /> 14 <context:include-filter type="annotation" 15 expression="org.springframework.stereotype.Service" /> 16 </context:component-scan> 17 18 <!-- 2.加载配数据源配置文件 db.properties --> 19 <context:property-placeholder location="classpath:db.properties" /> 20 21 <!-- 3、配置 C3P0 数据源 --> 22 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 23 <property name="user" value="${user}"></property> 24 <property name="password" value="${password}"></property> 25 <property name="jdbcUrl" value="${jdbcUrl}"></property> 26 <property name="driverClass" value="${driverClass}"></property> 27 </bean> 28 29 <!-- 4.事务管理器 (JDBC) --> 30 <bean id="transactionManager" 31 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 32 <property name="dataSource" ref="dataSource"></property> 33 </bean> 34 35 <!-- 5.启动声明式事务驱动 --> 36 <tx:annotation-driven transaction-manager="transactionManager" /> 37 38 <!-- MyBatis 整合 spring --> 39 <!-- 1. spring 通过 sqlSessionFactoryBean 获取 sqlSessionFactory 工厂类 --> 40 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 41 <property name="dataSource" ref="dataSource"></property> 42 <!-- 使用mapper 时可以实用化别名(BeanName) --> 43 <property name="typeAliasesPackage" value="cn.com.zfc.ssm.entity"></property> 44 </bean> 45 46 <!-- 2.打开 sqlSession 会话对象(扫描映射配置文件:XXXmapper.xml/XXXmapper.java) --> 47 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 48 <property name="basePackage" value="cn.com.zfc.ssm.dao" /> 49 <!-- 注意使用 sqlSessionFactoryBeanName 避免出现spring 扫描组件失效问题 --> 50 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 51 </bean> 52 53 </beans>
3.5、springMvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> 9 10 11 <!-- 1、配置自动扫描的包,SpringMVC IOC 容器只扫描Controller --> 12 <context:component-scan base-package="cn.com.zfc.ssm"> 13 <context:include-filter type="annotation" 14 expression="org.springframework.stereotype.Controller" /> 15 </context:component-scan> 16 17 <!-- 2、配置视图解析器 --> 18 <bean id="internalResourceViewResolver" 19 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 20 <property name="prefix" value="/WEB-INF/views/"></property> 21 <property name="suffix" value=".jsp"></property> 22 </bean> 23 24 <!-- 3.处理静态资源,默认 Servlet 处理 --> 25 <mvc:default-servlet-handler /> 26 27 <!-- 4.启动 MVC 注解驱动 --> 28 <mvc:annotation-driven /> 29 30 <!--5、启用AOP --> 31 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 32 </beans>
3.6、mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <!-- 可以写定义别名/扫描包(spring 管理) --> 8 9 </configuration>
3.7、db.properties
1 user=root 2 password=root 3 driverClass=com.mysql.jdbc.Driver 4 jdbcUrl=jdbc:mysql://localhost:3306/ssm_01
3.8、log4j.properties
1 # Global logging configuration 2 log4j.rootLogger=DEBUG, stdout 3 # Console output... 4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n 7 8 log4j.logger.java.sql.ResultSet=INFO 9 log4j.logger.org.apache=INFO 10 log4j.logger.java.sql.Connection=DEBUG 11 log4j.logger.java.sql.Statement=DEBUG 12 log4j.logger.java.sql.PreparedStatement=DEBUG
3.9、实体类:User.java
1 package cn.com.zfc.ssm.entity; 2 3 import org.springframework.stereotype.Component; 4 5 /** 6 * 定义实体类:如果实体类的属性名和数据表中的字段名不一致,则需要在 映射文件中配置 resultMap 7 * 8 * @author FuchangZhang 9 * 10 */ 11 @Component 12 public class User { 13 private Integer id; 14 private String userName; 15 private String userSex; 16 private Integer userAge; 17 18 public User() { 19 super(); 20 } 21 22 public User(String userName, String userSex, Integer userAge) { 23 super(); 24 this.userName = userName; 25 this.userSex = userSex; 26 this.userAge = userAge; 27 } 28 29 public User(Integer id, String userName, String userSex, Integer userAge) { 30 super(); 31 this.id = id; 32 this.userName = userName; 33 this.userSex = userSex; 34 this.userAge = userAge; 35 } 36 37 public Integer getId() { 38 return id; 39 } 40 41 public void setId(Integer id) { 42 this.id = id; 43 } 44 45 public String getUserName() { 46 return userName; 47 } 48 49 public void setUserName(String userName) { 50 this.userName = userName; 51 } 52 53 public String getUserSex() { 54 return userSex; 55 } 56 57 public void setUserSex(String userSex) { 58 this.userSex = userSex; 59 } 60 61 public Integer getUserAge() { 62 return userAge; 63 } 64 65 public void setUserAge(Integer userAge) { 66 this.userAge = userAge; 67 } 68 69 @Override 70 public String toString() { 71 return "User [id=" + id + ", userName=" + userName + ", userSex=" + userSex + ", userAge=" + userAge + "]"; 72 } 73 74 }
3.10 UserMapper.java
1 package cn.com.zfc.ssm.dao; 2 3 import java.util.List; 4 5以上是关于SSM项目整合基本步骤的主要内容,如果未能解决你的问题,请参考以下文章