欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SSM框架的整合

程序员文章站 2022-10-08 20:05:41
本文主要是详细介绍如何利用maven对SSM框架整合的思路及其详解 ......

1.1 maven整合ssm框架简介

1.1.1 ssm框架整合概述

  • 利用maven整合ssm框架的思路

  1.在对每个框架整合之前都需要先在maven的pom.xml配置文件中导入相关的依赖

  2.导入完依赖接着再在pom.xml配置文件中导入相关插件

  3.整合各个框架对应的配置文件,把与创建对象相关交给spring即整合到spring.xml中

  4.重新整个各个框架的核心配置文件

  • 注意点:

  1.每整合完一个框架都要做相应的测试,防止一次性整合完,出现错误难以排查

  2.本文以创建一个web项目为例

  3.本文用的是eclipse的neon版本工具进行整合

  4.配置文件放在src/main/resources目录下

1.2 maven整合ssm框架整合详解

1.2.1 在eclipse中创建一个工程

  • 创建工程详解

  1.创建一个maven工程,选择骨架webapp,填写好坐标三要素

  SSM框架的整合

     SSM框架的整合

  2.因为webapp骨架缺少src/main/java文件结构,所以需要添加该文件结构

  3.如果创建的是普通java骨架,其会缺少src/main/resource文件结构

  4.创建完了,项目目录结构如下:

  SSM框架的整合

  5.在maven的pom.xml中添加所需的插件,如source生成插件,编译插件,tomcat插件

 1 <project xmlns="http://maven.apache.org/pom/4.0.0" 
 2 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 3 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 4   <modelversion>4.0.0</modelversion>
 5   <groupid>cn.baidu</groupid>
 6   <artifactid>uer-order-ssm</artifactid>
 7   <packaging>war</packaging>
 8   <version>0.0.1-snapshot</version>
 9   <name>uer-order-ssm maven webapp</name>
10   <url>http://maven.apache.org</url>
11   <dependencies>
12     <dependency>
13       <groupid>junit</groupid>
14       <artifactid>junit</artifactid>
15       <version>3.8.1</version>
16       <scope>test</scope>
17     </dependency>
18   </dependencies>
19   <build>
20     <finalname>uer-order-ssm</finalname>
21     <!-- source插件 -->
22     <plugins>
23         <plugin>
24             <groupid>org.apache.maven.plugins</groupid>
25             <artifactid>maven-compiler-plugin</artifactid>
26             <configuration>
27                 <source>1.8</source>
28                 <target>1.8</target>
29                 <encoding>utf-8</encoding>
30             </configuration>
31         </plugin>
32         <!-- tomcat插件整合springmvc的时候补充 -->
33     </plugins>
34   </build>
35 </project>

 

  6.注意:

    • maven插件资源,将当前maven工程进行compile编译时加载,完成java1.8,1.7的使用,这里统一使用1.8的版本
    • 往下整合关于pom.xml文件的配置内容,本人只给出需要添加的部分,读者往上面代码中添加即可

1.2.2 将spring框架整合到maven中

  • 整体思路

  1.先在pom.xml文件中导入spring框架所依赖的资源

  2.然后编写spring和核心配置文件,spring.xml,该配置文件进行包扫描,打开注解配置

  3.往下的整合只要跟创建对象,对象管理相关的配置都在spring.xml文件中配置

  4.编写测试代码,测试整合是否成功

  • 先在pom.xml文件中导入spring框架的依赖资源

  1.导入spring-context依赖资源,但是依赖具有传递性,导入该资源可能也会引入其他依赖资源 

1 <dependency>
2    <groupid>org.springframework</groupid>
3    <artifactid>spring-context</artifactid>
4    <version>4.3.7.release</version>
5 </dependency>
  • 编写spring的核心配置文件spring.xml

  1.将spring.xml配置文件放到src/main/resources目录下

  2.在src目录下的资源在编译的时候都会将字节码加载到target/classes中

  3.在配置文件中会经常看见"classpath:xxx.xml"路径来读取文件,而classpath路径是什么位置?

  4.在maven项目中classpath路径默认src/main/resource路径

  5.往后跟创建对象相关的配置需要放在spring的核心配置文件中,用于创建bean对象

 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" 
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xmlns:util="http://www.springframework.org/schema/util" 
 8     xmlns:context="http://www.springframework.org/schema/context"
 9     xmlns:mvc="http://www.springframework.org/schema/mvc"
10     xsi:schemalocation="
11         http://www.springframework.org/schema/beans
12         http://www.springframework.org/schema/beans/spring-beans.xsd
13         http://www.springframework.org/schema/aop 
14         http://www.springframework.org/schema/aop/spring-aop.xsd
15         http://www.springframework.org/schema/tx 
16         http://www.springframework.org/schema/tx/spring-tx.xsd
17         http://www.springframework.org/schema/util 
18         http://www.springframework.org/schema/util/spring-util.xsd
19         http://www.springframework.org/schema/context
20         http://www.springframework.org/schema/context/spring-context.xsd
21         http://www.springframework.org/schema/mvc
22         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
23         
24         <!-- 开启包扫描 -->
25         <context:component-scan base-package="cn.baidu"></context:component-scan>
26         <!-- 开启注解di -->
27         <context:annotation-config></context:annotation-config>
28     
29  </beans>

 

  • 编写测试代码,测试maven整合spring框架是否成功

  1.依据三层架构建好目录结构

  SSM框架的整合

  2.编写控制层测试程序

 1 package cn.baidu.controller;
 2 
 3 import org.springframework.beans.factory.annotation.autowired;
 4 import org.springframework.stereotype.controller;
 5 
 6 import cn.baidu.service.helloservice;
 7 
 8 @controller
 9 public class hellocontroller {
10     
11 //    注入service层对象
12     @autowired
13     private helloservice helloservice;
14     
15     public string sayhi(string name){
16 //        调用对应service层的方法
17         return helloservice.sayhi(name);
18     }
19 
20 }

  3.编写业务逻辑层的接口

1 package cn.baidu.service;
2 
3 //service层的接口
4 public interface helloservice {
5 
6     public string sayhi(string name);
7 
8 }

  4.编写业务层的实现类

 1 package cn.baidu.service;
 2 
 3 import org.springframework.beans.factory.annotation.autowired;
 4 import org.springframework.stereotype.service;
 5 
 6 import cn.baidu.mapper.hellomapper;
 7 
 8 @service
 9 public class helloserviceimp implements helloservice{
10     
11 //    注入持久层对象
12     @autowired
13     private hellomapper hellomapper;
14     @override
15     public string sayhi(string name) {
16 //        调用持久层方法
17         return hellomapper.sayhi(name);
18     }
19 
20 }

  5.编写持久层接口

1 package cn.baidu.mapper;
2 
3 public interface hellomapper {
4 
5     public string sayhi(string name);
6 
7 }

  6.编写持久层实现类

 1 package cn.baidu.mapper;
 2 
 3 import org.springframework.stereotype.repository;
 4 
 5 @repository
 6 public class hellomapperimp implements hellomapper{
 7 
 8     @override
 9     public string sayhi(string name) {
10         return "hi,欢迎测试spring框架的搭建情况";
11     }
12     
13 }

  7.变成测试类,测试整合情况

 1 package cn.baidu.test;
 2 
 3 import org.junit.test;
 4 import org.springframework.context.applicationcontext;
 5 import org.springframework.context.support.classpathxmlapplicationcontext;
 6 
 7 import cn.baidu.controller.hellocontroller;
 8 
 9 public class testhello {
10     
11     @test
12     public void test(){
13         
14 //        初始化spring容器
15         applicationcontext context = new classpathxmlapplicationcontext("spring.xml");
16 //        获取控制层对象
17         hellocontroller hellocontroller = context.getbean(hellocontroller.class);
18 //        调用控制层方法
19         string str = hellocontroller.sayhi("谭小杰");
20         system.out.println(str);
21         
22     }
23 
24 }

  7.出现如下结果说明测试成功

  SSM框架的整合

1.2.3 将mybatis框架整合至maven中

  • 整合思路

  1.mybatis框架主要有两个配置文件,一个是mybatis配置文件和xxxmapper.xml(核心)配置文件

  2.映射配置文件中主要配置数据源,用于创建与数据库连接的对象和配置核心配置文件的映射

  3.有以上两个文件的配置内容可知,数据源的配置移到spring.xml配置文件中

  4.mybatis框架需要创建sqlsession对象执行sql语句,获取执行结果

  5.所以在spring.xml文件中配置sqlsession的bean,用于创建session对象

  6.mybatis需要mapper接口,需要在spring.xml配置对应的bean利用动态代理创建其接口的实现类

  7.编写mybatis-config.xml配置文件,里面添加一些mybatis的功能

  8.编写xxxmapper.xml配置文件,定义好namespace和定义所需的sql语句

  • 在pom.xml配置文件中导入mybatis框架所需的依赖资源

  1.配置数据源需要连接池依赖,jdbc依赖和数据库类型依赖

  2.本文用的连接池是阿里巴巴的druid,数据库用的是mysql

 1 <!-- 连接池依赖 -->
 2 <dependency>
 3     <groupid>com.alibaba</groupid>
 4     <artifactid>druid</artifactid>
 5     <version>1.0.14</version>
 6 </dependency>
 7 <!-- jdbc依赖 -->
 8 <dependency>
 9     <groupid>org.springframework</groupid>
10     <artifactid>spring-jdbc</artifactid>
11     <version>4.3.7.release</version>
12 </dependency>
13 <!-- mysql依赖 -->
14 <dependency>
15     <groupid>mysql</groupid>
16     <artifactid>mysql-connector-java</artifactid>
17     <version>5.0.8</version>
18 </dependency>
19 <!-- mybatis依赖资源 -->
20 <dependency>
21     <groupid>org.mybatis</groupid>
22     <artifactid>mybatis</artifactid>
23     <version>3.4.5</version>
24 </dependency>
25 <!-- mybatis-spring依赖资源 -->
26 <dependency>
27     <groupid>org.mybatis</groupid>
28     <artifactid>mybatis-spring</artifactid>
29     <version>1.3.1</version>
30 </dependency>

 

  • 在spring.xml中配置对应的bean

  1.配置数据源的bean标签,该标签主要数用于创建连接数据库对象

1 <!-- 配置数据源 -->
2 <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource">
3     <!-- 4个属性,数据库驱动,url,用户名和密码 -->
4       <property name="driverclassname" value="com.mysql.jdbc.driver"/>
5        <property name="url" value="jdbc:mysql:///mssm"/>
6        <property name="username" value="root"/>
7        <property name="password" value="root"/>
8 </bean>

  2.配置sqlsession的bean标签,该标签用于创建session对象,用于执行sql语句和获取执行结果

 1 <!-- 配置sqlsession,动态代理实现持久层sqlsession接口实现的对象 -->
 2 <bean id="sqlsession" class="org.mybatis.spring.sqlsessionfactorybean">
 3     <!-- 绑定数据源即获取connection对象 -->
 4     <property name="datasource" ref="datasource"/>
 5     <!-- 加载mybatis的独立配置文件 -->
 6     <property name="configlocation" value="classpath:mybatis-config.xml"/>
 7     <!-- 扫描映射xxxmapper.xml映射文件 -->
 8     <property name="mapperlocations"value="classpath:mapper/*.xml"/>
 9 </bean>
10 <!-- 配置mapper接口的扫描配置用于创建mapper接口的实现类 -->
11 <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
12      <property name="basepackage" value="cn.baidu.mapper"/>
13 </bean>
  • 编写mybatis-config.xml配置文件

  1.在src/main/resources目录下编写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     <settings>
 8         <setting name="mapunderscoretocamelcase" value="true"/>
 9     </settings>
10     
11 </configuration>
  • 编写xxxmapper.xml配置文件

  1.在src/main/resources/mapper目录下配置对应的xxxmapper.xml文件,将sql语句添加其中

1 <?xml version="1.0" encoding="utf-8"?>
2 <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4 <mapper namespace="cn.baidu.mapper.studentmapper">
5 </mapper> 
  • 编写测试代码,测试mybatis整合是否成功

  1.在连接的数据库中建立mssm数据库,创建student表格,其有如下字段和数据

1 //创建表
2 create table student(id varchar(225),name varchar(225));
3 //添加数据
4 insert into student values('a','王重阳'),('b','欧阳锋'),('c','黄药师');

  2.编写一个用于封装数据的domain类

 1 package cn.baidu.domain;
 2 
 3 public class student {
 4     
 5 //    定义属性
 6     private string id;
 7     private string name;
 8 //    定义构造方法
 9     public student(){}
10     public student(string id, string name) {
11         super();
12         this.id = id;
13         this.name = name;
14     }
15 //    定义getter和sett方法
16     public string getid() {
17         return id;
18     }
19     public void setid(string id) {
20         this.id = id;
21     }
22     public string getname() {
23         return name;
24     }
25     public void setname(string name) {
26         this.name = name;
27     }
28     @override
29     public string tostring() {
30         return "student [id=" + id + ", name=" + name + "]";
31     }
32     
33 }

  3.编写控制层代码

 1 package cn.baidu.controller;
 2 
 3 import org.springframework.beans.factory.annotation.autowired;
 4 import org.springframework.stereotype.controller;
 5 
 6 import cn.baidu.domain.student;
 7 import cn.baidu.service.studentservice;
 8 
 9 @controller
10 public class studentcontroller {
11     
12     @autowired
13     private studentservice studentservice;
14     public student querystudentbyid(string id){
15         return studentservice.querystudentbyid(id);
16     }
17 
18 }

  4.编写业务层接口

1 package cn.baidu.service;
2 
3 import cn.baidu.domain.student;
4 
5 public interface studentmapper {
6     
7     public student queryone(string id);
8 }

  5.编写业务层的实现类

 1 package cn.baidu.service;
 2 
 3 import org.springframework.beans.factory.annotation.autowired;
 4 import org.springframework.stereotype.service;
 5 
 6 import cn.baidu.domain.student;
 7 
 8 @service
 9 public class studentserviceimp implements studentservice{
10     
11     @autowired
12     private studentmapper studentmapper;
13 
14     @override
15     public student querystudentbyid(string id) {
16         return studentmapper.queryone(id);
17     }
18 
19 }

  6.编写studentmapper.xml文件,把写入查询sql语句

1 <?xml version="1.0" encoding="utf-8"?>
2 <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4 <mapper namespace="cn.baidu.mapper.studentmapper">
5     <select id="queryone" resulttype="cn.baidu.domain.student">
6         select * from student where id=#{id};
7     </select>
8 </mapper> 

  7.编写对应的mapper配置文件对应的接口

1 package cn.baidu.mapper;
2 
3 import cn.baidu.domain.student;
4 
5 public interface studentmapper {
6     
7     public student queryone(string id);
8 }

  8.编写测试类,测试是否整合成功

 1 package cn.baidu.test;
 2 
 3 import org.junit.test;
 4 import org.springframework.context.applicationcontext;
 5 import org.springframework.context.support.classpathxmlapplicationcontext;
 6 
 7 import cn.baidu.controller.studentcontroller;
 8 import cn.baidu.domain.student;
 9 
10 public class studenttest {
11     
12     @test
13     public void test(){
14         
15 //        初始化spring容器
16         applicationcontext context = new classpathxmlapplicationcontext("spring.xml");
17 //        获取控制层对象
18         studentcontroller studentcontroller = context.getbean(studentcontroller.class);
19 //        调用控制层方法
20         student student = studentcontroller.querystudentbyid("c");
21         system.out.println(student);
22         
23     }
24 
25 
26 }

   9.出现如下结果,说明mybatis整合成功

   SSM框架的整合

1.2.4 将springmvc框架整合到maven中

  • 整体思路

  1.在pom.xml文件中导入springmvc框架所需要的依赖资源和插件

  2.在web.xml文件中创建前端控制器即dispatcherservlet

  3.编写springmvc的核心配置文件spring-mvc.xml,用于配置视图解析器和打开mvc注解

  4.如有一些静态资源的访问,也可在spring-mvc.xml文件中配置

  • 在pom.xml配置文件中导入tomcat插件

  1.对于web项目,所有的内容都是由tomcat容器启动的,所以需要引入tomcat插件

 

 1 <!-- 引入tomcat插件 -->
 2 <plugin>
 3         <groupid>org.apache.tomcat.maven</groupid>
 4         <artifactid>tomcat7-maven-plugin</artifactid>
 5         <version>2.2</version>
 6         <!-- tomcat的启动配置 -->
 7         <configuration>
 8             <!-- 端口号访问路径默认80端口 -->
 9             <port>80</port>
10             <!-- 应用程序的访问路径 -->
11             <path>/</path>
12             <!-- 接收数据编解码的格式为utf-8 -->
13             <uriencoding>utf-8</uriencoding>
14             <usebodyencodingforuri>utf-8</usebodyencodingforuri>
15         </configuration>
16 </plugin>

 

  注:如果不想引入tomcat插件,将项目打成war包扔至外部tomcat也可以,这里选择导入插件

  • 导入springmvc所需要的依赖

  1.springmvc需要导入spring-web和spring-webmvc依赖

  2.有时为了将对象转化成json字符串,还需要jackson依赖

 

 1 <!-- 引入spring-web依赖 -->
 2 <dependency>
 3     <groupid>org.springframework</groupid>
 4     <artifactid>spring-web</artifactid>
 5     <version>4.3.7.release</version>
 6 </dependency>
 7 <!-- 引入spring-webmvc依赖 -->
 8 <dependency>
 9     <groupid>org.springframework</groupid>
10     <artifactid>spring-webmvc</artifactid>
11     <version>4.3.7.release</version>
12 </dependency>
13 <!-- 引入jackson依赖 -->
14 <dependency>
15     <groupid>com.fasterxml.jackson.core</groupid>
16     <artifactid>jackson-core</artifactid>
17     <version>2.8.8</version>
18 </dependency>
19 <dependency>
20     <groupid>com.fasterxml.jackson.core</groupid>
21     <artifactid>jackson-databind</artifactid>
22     <version>2.8.8</version>
23 </dependency>

 

  • 在tomcat启动加载的web.xml文件中,配置dispacherservlet

  1.在dispacherservlet的配置中初始化spring*.xml文件,使得服务器加载spring的所有配置内容

  2.让服务器加载所有配置内容的目的是使得各层的注解生效,web.xml配置如下

 

 1 <!doctype web-app public
 2  "-//sun microsystems, inc.//dtd web application 2.3//en"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 5          xmlns="http://java.sun.com/xml/ns/javaee" 
 6          xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 7          xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 8   <display-name>archetype created web application</display-name>
 9     <!-- 创建dispachersevlet -->
10     <servlet>
11         <servlet-name>springmvc</servlet-name>
12         <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
13         <!-- 配置初始化参数,使得tomcat启动的时候,加载spring*.xml文件 -->
14         <init-param>
15             <param-name>contextconfiglocation</param-name>
16             <param-value>classpath:spring*.xml</param-value>
17         </init-param>
18     </servlet>
19     <servlet-mapping>
20         <servlet-name>springmvc</servlet-name>
21         <url-pattern>/*</url-pattern>
22     </servlet-mapping>
23   <welcome-file-list>index.html</welcome-file-list>
24 </web-app>

 

  • 在src/main/resources目录下编写spring-mvc.xml文件

  1.文件中开启mvc注解生效配置和视图解析器的配置

 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" 
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xmlns:util="http://www.springframework.org/schema/util" 
 8     xmlns:context="http://www.springframework.org/schema/context"
 9     xmlns:mvc="http://www.springframework.org/schema/mvc"
10     xsi:schemalocation="
11         http://www.springframework.org/schema/beans
12         http://www.springframework.org/schema/beans/spring-beans.xsd
13         http://www.springframework.org/schema/aop 
14         http://www.springframework.org/schema/aop/spring-aop.xsd
15         http://www.springframework.org/schema/tx 
16         http://www.springframework.org/schema/tx/spring-tx.xsd
17         http://www.springframework.org/schema/util 
18         http://www.springframework.org/schema/util/spring-util.xsd
19         http://www.springframework.org/schema/context
20         http://www.springframework.org/schema/context/spring-context.xsd
21         http://www.springframework.org/schema/mvc
22         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
23     <!-- 静态资源html js css访问  -->
24     <mvc:resources location="/" mapping="/**"/>
25     <!-- springmvc开启所有注解功能的标签 -->
26 
27     <mvc:annotation-driven/>
28     
29     <!-- 视图解析的前后缀 -->
30     <bean id="viewresovler" 
31     class="org.springframework.web.servlet.view.internalresourceviewresolver">
32     <!-- 前后缀拼接 -->
33     <property name="prefix" value="views/"/>
34     <property name="suffix" value=".html"></property>
35     </bean>
36     <!-- 以下配置可以解决springmvc返回数据的编解码问题 -->
37     <bean class="org.springframework.http.converter.stringhttpmessageconverter">
38         <property name="supportmediatypes" value="text/html;charset=utf-8"></property>
39     </bean>
40 </beans>
  • 编写测试代码,测试springmvc整合是否成功

  1.测试代码实现的功能需求:通过浏览器,传递一个get的请求参数id=a/b/c

  2.项目返回一个从数据库查询的student对象,页面的浏览器展示

  3.在整合mybatis测试代码的基础上,编写该测试代码,把之前的test类删除

  4.修改控制层的代码,如下,其他的与mybatis测试代码一致,不再展示

 

 1 package cn.baidu.controller;
 2 
 3 import org.springframework.beans.factory.annotation.autowired;
 4 import org.springframework.stereotype.controller;
 5 import org.springframework.web.bind.annotation.requestmapping;
 6 import org.springframework.web.bind.annotation.responsebody;
 7 
 8 import cn.baidu.domain.student;
 9 import cn.baidu.service.studentservice;
10 
11 
12 @controller
13 public class studentcontroller {
14     
15     @autowired
16     private studentservice studentservice;
17     
18 //    以下两个注解,他们的作用分别是:将以下的方法变成处理器
19 //    requestbody是实现将返回的对象变成json字符串,展示在浏览器端
20     @requestmapping("querystudent")
21     @responsebody
22     public student querystudentbyid(string id){
23         return studentservice.querystudentbyid(id);
24     }
25 
26 }

  5.启动当前工程,运行tomcat插件,在maven build中创建一个运行命令:goals:tomcat7:run

  SSM框架的整合

   SSM框架的整合

  6.运行debuge/run的maven build的命令,启动tomcat出现如下信息说明启动成功

  SSM框架的整合

  7.在浏览器测试访问localhost/querystudent?id=a,出现如下的结果说明整合成功

  SSM框架的整合

  8.完成了以上内容说明maven整合的ssm框架已将全部整合完毕

1.3 ssm框架整合总结

1.3.1 ssm框架整合整体配置文件和目录结构展示

  • 目录结构如下:

  1.磁盘中的目录结构如下

  SSM框架的整合

  2.eclipse中的目录结构如下

  SSM框架的整合

  • 整体配置文件

  1.pom.xml整体配置文件

  1 <project xmlns="http://maven.apache.org/pom/4.0.0" 
  2 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  3 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4   <modelversion>4.0.0</modelversion>
  5   <groupid>cn.baidu</groupid>
  6   <artifactid>uer-order-ssm</artifactid>
  7   <packaging>war</packaging>
  8   <version>0.0.1-snapshot</version>
  9   <name>uer-order-ssm maven webapp</name>
 10   <url>http://maven.apache.org</url>
 11   <dependencies>
 12     <dependency>
 13       <groupid>junit</groupid>
 14       <artifactid>junit</artifactid>
 15       <version>3.8.1</version>
 16       <scope>test</scope>
 17     </dependency>
 18     <dependency>
 19         <groupid>org.springframework</groupid>
 20         <artifactid>spring-context</artifactid>
 21         <version>4.3.7.release</version>
 22     </dependency>
 23     <!-- 连接池依赖 -->
 24     <dependency>
 25         <groupid>com.alibaba</groupid>
 26         <artifactid>druid</artifactid>
 27         <version>1.0.14</version>
 28     </dependency>
 29     <!-- jdbc依赖 -->
 30     <dependency>
 31         <groupid>org.springframework</groupid>
 32         <artifactid>spring-jdbc</artifactid>
 33         <version>4.3.7.release</version>
 34     </dependency>
 35     <!-- mysql依赖 -->
 36     <dependency>
 37         <groupid>mysql</groupid>
 38         <artifactid>mysql-connector-java</artifactid>
 39         <version>5.0.8</version>
 40     </dependency>
 41     <!-- mybatis依赖资源 -->
 42     <dependency>
 43         <groupid>org.mybatis</groupid>
 44         <artifactid>mybatis</artifactid>
 45         <version>3.4.5</version>
 46     </dependency>
 47     <!-- mybatis-spring依赖资源 -->
 48     <dependency>
 49         <groupid>org.mybatis</groupid>
 50         <artifactid>mybatis-spring</artifactid>
 51         <version>1.3.1</version>
 52     </dependency>
 53     <!-- 引入spring-web依赖 -->
 54     <dependency>
 55         <groupid>org.springframework</groupid>
 56         <artifactid>spring-web</artifactid>
 57         <version>4.3.7.release</version>
 58     </dependency>
 59     <!-- 引入spring-webmvc依赖 -->
 60     <dependency>
 61         <groupid>org.springframework</groupid>
 62         <artifactid>spring-webmvc</artifactid>
 63         <version>4.3.7.release</version>
 64     </dependency>
 65     <!-- 引入jackson依赖 -->
 66     <dependency>
 67         <groupid>com.fasterxml.jackson.core</groupid>
 68         <artifactid>jackson-core</artifactid>
 69         <version>2.8.8</version>
 70     </dependency>
 71     <dependency>
 72         <groupid>com.fasterxml.jackson.core</groupid>
 73         <artifactid>jackson-databind</artifactid>
 74         <version>2.8.8</version>
 75     </dependency>
 76   </dependencies>
 77   <build>
 78     <finalname>uer-order-ssm</finalname>
 79     <!-- source插件 -->
 80     <plugins>
 81         <plugin>
 82             <groupid>org.apache.maven.plugins</groupid>
 83             <artifactid>maven-compiler-plugin</artifactid>
 84             <configuration>
 85                 <source>1.8</source>
 86                 <target>1.8</target>
 87                 <encoding>utf-8</encoding>
 88             </configuration>
 89         </plugin>
 90         <!-- 引入tomcat插件 -->
 91         <plugin>
 92             <groupid>org.apache.tomcat.maven</groupid>
 93             <artifactid>tomcat7-maven-plugin</artifactid>
 94             <version>2.2</version>
 95             <!-- tomcat的启动配置 -->
 96             <configuration>
 97                 <!-- 端口号访问路径默认80端口 -->
 98                 <port>80</port>
 99                 <!-- 应用程序的访问路径 -->
100                 <path>/</path>
101                 <!-- 接收数据编解码的格式为utf-8 -->
102                 <uriencoding>utf-8</uriencoding>
103                 <usebodyencodingforuri>utf-8</usebodyencodingforuri>
104             </configuration>
105         </plugin>
106     </plugins>
107   </build>
108 </project>

  2.web.xml整体配置文件

 1 <!doctype web-app public
 2  "-//sun microsystems, inc.//dtd web application 2.3//en"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 5          xmlns="http://java.sun.com/xml/ns/javaee" 
 6          xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 7          xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 8   <display-name>archetype created web application</display-name>
 9     <!-- 创建dispachersevlet -->
10     <servlet>
11         <servlet-name>springmvc</servlet-name>
12         <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
13         <!-- 配置初始化参数,使得tomcat启动的时候,加载spring*.xml文件 -->
14         <init-param>
15             <param-name>contextconfiglocation</param-name>
16             <param-value>classpath:spring*.xml</param-value>
17         </init-param>
18     </servlet>
19     <servlet-mapping>
20         <servlet-name>springmvc</servlet-name>
21         <url-pattern>/*</url-pattern>
22     </servlet-mapping>
23   <welcome-file-list>index.html</welcome-file-list>
24 </web-app>

  3.spring.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" 
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xmlns:util="http://www.springframework.org/schema/util" 
 8     xmlns:context="http://www.springframework.org/schema/context"
 9     xmlns:mvc="http://www.springframework.org/schema/mvc"
10     xsi:schemalocation="
11         http://www.springframework.org/schema/beans
12         http://www.springframework.org/schema/beans/spring-beans.xsd
13         http://www.springframework.org/schema/aop 
14         http://www.springframework.org/schema/aop/spring-aop.xsd
15         http://www.springframework.org/schema/tx 
16         http://www.springframework.org/schema/tx/spring-tx.xsd
17         http://www.springframework.org/schema/util 
18         http://www.springframework.org/schema/util/spring-util.xsd
19         http://www.springframework.org/schema/context
20         http://www.springframework.org/schema/context/spring-context.xsd
21         http://www.springframework.org/schema/mvc
22         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
23         
24         <!-- 开启包扫描 -->
25         <context:component-scan base-package="cn.baidu"></context:component-scan>
26         <!-- 开启注解di -->
27         <context:annotation-config></context:annotation-config>
28         <!-- 配置数据源 -->
29         <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource">
30             <!-- 4个属性,数据库驱动,url,用户名和密码 -->
31             <property name="driverclassname" value="com.mysql.jdbc.driver"/>
32             <property name="url" value="jdbc:mysql:///mssm"/>
33             <property name="username" value="root"/>
34             <property name="password" value="041x"/>
35         </bean>
36         <!-- 配置sqlsession,动态代理实现持久层sqlsession接口实现的对象 -->
37         <bean id="sqlsession" class="org.mybatis.spring.sqlsessionfactorybean">
38             <!-- 绑定数据源即获取connection对象 -->
39             <property name="datasource" ref="datasource"/>
40             <!-- 加载mybatis的独立配置文件 -->
41             <property name="configlocation" value="classpath:mybatis-config.xml"/>
42             <!-- 扫描映射xxxmapper.xml映射文件 -->
43             <property name="mapperlocations" value="classpath:mapper/*.xml"/>
44         </bean>
45         <!-- 配置mapper接口的扫描配置用于创建mapper接口的实现类 -->
46         <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
47             <property name="basepackage" value="cn.baidu.mapper"/>
48         </bean>
49     
50  </beans>

  4.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     <settings>
 8         <setting name="mapunderscoretocamelcase" value="true"/>
 9     </settings>
10     
11 </configuration>

  5.spring-mvc.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" 
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xmlns:util="http://www.springframework.org/schema/util" 
 8     xmlns:context="http://www.springframework.org/schema/context"
 9     xmlns:mvc="http://www.springframework.org/schema/mvc"
10     xsi:schemalocation="
11         http://www.springframework.org/schema/beans
12         http://www.springframework.org/schema/beans/spring-beans.xsd
13         http://www.springframework.org/schema/aop 
14         http://www.springframework.org/schema/aop/spring-aop.xsd
15         http://www.springframework.org/schema/tx 
16         http://www.springframework.org/schema/tx/spring-tx.xsd
17         http://www.springframework.org/schema/util 
18         http://www.springframework.org/schema/util/spring-util.xsd
19         http://www.springframework.org/schema/context
20         http://www.springframework.org/schema/context/spring-context.xsd
21         http://www.springframework.org/schema/mvc
22         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
23     <!-- 静态资源html js css访问  -->
24     <mvc:resources location="/" mapping="/**"/>
25     <!-- springmvc开启所有注解功能的标签 -->
26 
27     <mvc:annotation-driven/>
28     
29     <!-- 视图解析的前后缀 -->
30     <bean id="viewresovler" 
31     class="org.springframework.web.servlet.view.internalresourceviewresolver">
32     <!-- 前后缀拼接 -->
33     <property name="prefix" value="views/"/>
34     <property name="suffix" value=".html"></property>
35     </bean>
36     <!-- 以下配置可以解决springmvc返回数据的编解码问题 -->
37     <bean class="org.springframework.http.converter.stringhttpmessageconverter">
38         <property name="supportmediatypes" value="text/html;charset=utf-8"></property>
39     </bean>
40 </beans>

  6.xxxmapper.xml整体配置文件,此配置文件多变,以下只是范例

1 <?xml version="1.0" encoding="utf-8"?>
2 <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4 <mapper namespace="cn.baidu.mapper.studentmapper">
5     <select id="queryone" resulttype="cn.baidu.domain.student">
6         select * from student where id=#{id};
7     </select>
8 </mapper> 

1.3.2 ssm整合易错点

  • 错误调试思路

  1.在做整合的时候不免出现各种各样的错误,大多都是不细心造成的

  2.所以在遇见错误的时候,保持耐心,慢慢捋一捋思路

  3.以下是本人调试错误的一些思路,仅供参考

    • ssm框架整合出现异常,一般都是由前一个异常引起后面异常的抛出,所以查看异常的时候,查看报错的第一句(第一句内容比较长)
    • 例如报如下错误:出现异常按照以下红色字体标识往下找,通常找到最后一行即可找到出错导致的原因,或者某些关键字,按照其提示的关键字,自己到对应的地方仔细检查,排查错误
    • org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean with name 'studentcontroller': unsatisfied dependency expressed through field 'studentservice'; nested exception is org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean with name 'studentserviceimp': unsatisfied dependency expressed through field 'studentmapper'; nested exception is org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean with name 'studentmapper' defined in file [e:\workspace_v1\order-user-ssm\target\classes\cn\baidu\mapper\studentmapper.class]: unsatisfied dependency expressed through bean property 'sqlsessionfactory'; nested exception is org.springframework.beans.factory.beancreationexception: error creating bean with name 'sqlsession' defined in class path resource [applicationcontext.xml]: invocation of init method failed; nested exception is org.springframework.core.nestedioexception: failed to parse mapping resource: 'file [e:\workspace_v1\order-user-ssm\target\classes\mapper\studentmapper.xml]'; nested exception is org.apache.ibatis.builder.builderexception: error parsing mapper xml. cause: org.apache.ibatis.builder.builderexception: error resolving classcause: org.apache.ibatis.type.typeexception: could not resolve type alias 'student'.  cause: java.lang.classnotfoundexception: cannot find class: student

    • 错误原因:配置文件找不到student类,说明在填写配置文件全路径可能写错了

    • 解决方法:重新对student类进行检查,检查配置文件中student类的路径是否写正确
  • 整合过程可能出现的普遍性错误

  1.在添加pom内容的时候,工程可能会出现一个红叉子,查看problem的内容标签,会报如下错误

    • description resource  path  location   typeproject configuration is not up-to-date with pom.xml. select:maven->update project... from the project context menu or use quickfix.  order-user-ssm line1 maven configuration problem
    • 出错原因:每当修改pom.xml文件时,工程可能会出现一个红叉子,查看problem标签会报出以上的错误
    • 解决方法:更新maven项目即可

        SSM框架的整合