SSM项目整合

SSM 整合系列之 SSM 项目整合 Spring,SpringMVC 和 MyBatis 已经是非常成熟的框架了,使用该框架开发已有些时日了,从今天开始呢就对用此框架开发的过程进行一系列的总结

本文包含相关资料包-----> 点击直达获取<-------

SSM 整合系列之 SSM 项目整合

Spring、SpringMVC 和 MyBatis 已经是非常成熟的框架了,使用该框架开发已有些时日了,从今天开始呢就对用此框架开发的过程进行一系列的总结,旨在个人总结中提升自己,同时能帮到需要的人就更好了(大牛可忽略此文)。本文将着重介绍项目的搭建整合。 1 Maven Web 项目创建 2 SSM 整合 项目结构如下图: 2.1 web.xml 配置 webapp 下创建 WEB-INF 然后创建 web.xml 文件,文件内容如下:

```

SSM整合项目Demo

contextConfigLocation classpath :spring/ .xml,classpath :servlet/ .xml

<!-- Spring监听器 -->  
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 编码过滤器 -->  
<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>

<!-- Spring MVC servlet -->  
<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*:servlet/*.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>

<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

```

2.2 Maven 引入需要的 JAR 包

``` 4.0.0 com.ssm SSM_DEMO 0.0.1-SNAPSHOT war SSM整合项目Demo

UTF-8 4.1.4.RELEASE 2.5.0 1.2.3



org.springframework
spring-core
${spring.version}

    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-web</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-tx</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-webmvc</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-aop</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-test</artifactId>  
        <version>${spring.version}</version>  
    </dependency>

    <!-- mybatis 包 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.8</version>
    </dependency>

    <!--mybatis spring 插件 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.2</version>
    </dependency>

    <!-- mysql连接 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>

    <!-- 数据源druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.11</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.4</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>

    <!-- servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- json -->
    <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-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <!-- 解析xml -->
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>${jackson.version}</version>
    </dependency>


    <!-- gson -->
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>


    <!-- 文件上传 -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>

    <dependency>  
         <groupId>commons-collections</groupId>  
         <artifactId>commons-collections</artifactId>  
         <version>3.2.1</version>  
    </dependency>

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.1.Final</version>
    </dependency>

    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.3</version>
        <classifier>jdk15</classifier>
    </dependency>

    <dependency>
        <groupId>net.sf.ezmorph</groupId>
        <artifactId>ezmorph</artifactId>
        <version>1.0.5</version>
    </dependency>

    <!-- 常用工具包 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
    </dependency>

org.apache.maven.plugins maven-surefire-plugin true 2.18.1 maven-war-plugin WebRoot WebRoot WEB-INF/lib/javax.annotation-api-1.2.jar 2.4 org.apache.maven.plugins maven-compiler-plugin 3.2 1.7 1.7 ```

2.3、数据库连接和 Log4j 配置 src-main-resource 下创建 config 文件夹,然后创建 jdbc.properties 文件,文件内容如下:

```

连接驱动

jdbc.driverClassName=com.mysql.jdbc.Driver

主库

jdbc.url = jdbc\:mysql\://localhost\:3306/db_ssmdemo?useUnicode\=true&characterEncoding\=UTF-8&allowMultiQueries\=true jdbc.username = root jdbc.password = 123456

jdbc.initialSize=2 jdbc.maxActive=5 jdbc.maxIdle=5 jdbc.minIdle=1 jdbc.validationQuery=select 1 ```

src-main-resource 创建文件 log4j.properties,文件内容如下:

``` log4j.rootLogger = DEBUG,stdout,A1,A2

STDOUT

log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss}][%p] %m %n

INFO

log4j.appender.A1 = org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File =../logs/info.log log4j.appender.A1.Append = true log4j.appender.A1.Threshold =INFO log4j.appender.A1.layout = org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss}][%p] %m %n

ERROR

log4j.appender.A2 = org.apache.log4j.DailyRollingFileAppender log4j.appender.A2.File =../logs/error.log log4j.appender.A2.Append = true log4j.appender.A2.Threshold =ERROR log4j.appender.A2.layout = org.apache.log4j.PatternLayout log4j.appender.A2.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss}][%p] %m %n

log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG ```

2.4Spring 配置 src-main-resource 下创建 Spring 文件夹,然后创建 applicationContext.xml 文件

``` <?xml version="1.0" encoding="UTF-8"?> xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd ">

<!--引入配置属性文件 -->
<context:property-placeholder location="classpath*:config/*.properties" />

<!-- 线程池配置 -->
<bean id="taskExecutor" class="uc6_0270_70fec12 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <!-- 核心线程数 -->   
    <property name="corePoolSize" value="5"/>
    <!-- 最大线程数 -->  
    <property name="maxPoolSize" value="50"/>
    <!-- 队列最大长度 -->
    <property name="queueCapacity" value="1000"/>
    <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
    <property name="keepAliveSeconds" value="6"/>
</bean>

```

2.5 数据源配置 Spring 文件加下创建 dataAccessContext.xml 文件

```

数据库、事务配置

<!-- 数据源-->
<bean id="dataSource" class="v0a_d4c7_c7edaaf com.alibaba.druid.pool.DruidDataSource" destroy-method="close">

    <property name="driverClassName" value="${jdbc.driverClassName}" />

    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

    <property name="initialSize" value="${jdbc.initialSize}" />
    <property name="maxActive" value="${jdbc.maxActive}" />
    <property name="maxIdle" value="${jdbc.maxIdle}" />
    <property name="minIdle" value="${jdbc.minIdle}" />
    <property name="validationQuery" value="${jdbc.validationQuery}" />

    <!-- 监控数据库 -->
    <!--<property name="filters" value="mergeStat" />-->
    <property name="filters" value="stat" /> 
    <property name="connectionProperties" value="druid.stat.mergeSql=true" />  
</bean>

```

2.6 MyBatis 配置 Spring 文件加下创建 mybatis.xml 文件,该文件为 Spring 和 MyBatis 整合内容

```

<bean id="sqlSessionFactory" >
  <property name="dataSource" ref="dataSource" />
  <property name="mapperLocations">
     <list>
      <value>classpath:sql/*.xml</value>
    </list>
  </property>
</bean>

<bean >
  <property name="basePackage" value="com.ssm.mapper" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id="sqlSession" scope="prototype">
  <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

<!-- 使用annotation定义事务,使用cglib代理,解决同一service中事务方法相互调用的 嵌套事务失效问题 -->

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-/>
<!--事务配置 -->
<bean id="transactionManager" >
  <property name="dataSource" ref="dataSource" />
</bean>

```

2.7 整合 SpringMVC src-main-resource 下创建 servlet 文件夹,然后创建 spring-mvc.xml 文件

```

<!-- 自动扫描,只扫描Controller -->
<context:component-scan base-package="com.ssm">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

<!-- 启用spring mvc 注解 -->
<mvc:annotation-driven>
  <mvc:message-converters register-defaults="true">
    <!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
    <bean >
      <constructor-arg value="UTF-8" />
    </bean>
    <bean /> 
    <!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true -->
    <bean >
      <property name="prettyPrint" value="true"/>
    </bean>     
  </mvc:message-converters>
</mvc:annotation-driven>

<!-- 静态资源 -->
<!-- <mvc:resources mapping="/static/**" location="/static/" /> -->

<!-- 试图解析后台 -->
<bean >
  <property name="prefix" value="/WEB-INF/views" />
  <property name="suffix" value=".jsp" />
  <property name="order" value="1" />
</bean>

<!-- 配置多文件上传 -->
<bean id="multipartResolver"
  >
  <property name="defaultEncoding">
    <value>UTF-8</value>
  </property>
  <property name="maxUploadSize">
    <!-- 上传文件大小限制为100M,100*1024*1024 -->
    <value>104857600</value>
  </property>
  <property name="maxInMemorySize">
    <value>4096</value>
  </property>
</bean>

```

2.8 创建 index.jsp 页面 在 webapp 创建 index.jsp 页面

``` <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

Insert title here

index.jsp

```

2.9 创建一个测试 Controller,类为 HelloController

``` package com.ssm.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("test") public class HelloController {

@RequestMapping("hello")
@ResponseBody
public String hello() {
    return "hello";
}

} ```

访问 http://IP 地址:端口号/SSM_DEMO/test/hello,即可查看返回值,正常返回,说明项目搭建成功

参考文献

  • 基于SSM框架的资质管理系统开发(首都经济贸易大学·田国兵)
  • 印刷公司内容管理平台的设计与实现(吉林大学·郎彩虹)
  • 朔城区发改局投资项目管理系统的设计与实现(大连理工大学·闫惠宇)
  • 朔城区发改局投资项目管理系统的设计与实现(大连理工大学·闫惠宇)
  • 基于多开源框架构建中国移动项目管理系统(四川大学·冯国仕)
  • 基于多开源框架构建中国移动项目管理系统(四川大学·冯国仕)
  • 基于工作流的项目管理系统的设计与实现(华中科技大学·袁晓军)
  • 朔城区发改局投资项目管理系统的设计与实现(大连理工大学·闫惠宇)
  • 基于SSM框架的资产证券化系统的设计与实现(西安电子科技大学·贾田田)
  • 基于SSM框架的资产证券化系统的设计与实现(西安电子科技大学·贾田田)
  • 基于SSM框架的资产证券化系统的设计与实现(西安电子科技大学·贾田田)
  • 基于SSM框架的投资项目经济评价系统分析与实现(山东大学·白春强)
  • 基于工作流的项目管理系统的设计与实现(华中科技大学·袁晓军)
  • 朔城区发改局投资项目管理系统的设计与实现(大连理工大学·闫惠宇)
  • 朔城区发改局投资项目管理系统的设计与实现(大连理工大学·闫惠宇)

本文内容包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主题。发布者:代码海岸 ,原文地址:https://m.bishedaima.com/yuanma/35783.html

相关推荐

发表回复

登录后才能评论