SSM框架的整合

譚小杰發表於2019-06-22

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,填寫好座標三要素

  

    

  2.因為webapp骨架缺少src/main/java檔案結構,所以需要新增該檔案結構

  3.如果建立的是普通java骨架,其會缺少src/main/resource檔案結構

  4.建立完了,專案目錄結構如下:

  

  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.依據三層架構建好目錄結構

  

  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.出現如下結果說明測試成功

  

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整合成功

   

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

  

  

  6.執行debuge/run的maven build的命令,啟動Tomcat出現如下資訊說明啟動成功

  

  7.在瀏覽器測試訪問localhost/queryStudent?id=a,出現如下的結果說明整合成功

  

  8.完成了以上內容說明maven整合的SSM框架已將全部整合完畢

1.3 SSM框架整合總結

1.3.1 SSM框架整合整體配置檔案和目錄結構展示

  • 目錄結構如下:

  1.磁碟中的目錄結構如下

  

  2.eclipse中的目錄結構如下

  

  • 整體配置檔案

  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>
View Code

  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>
View Code

  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>
View Code

  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>
View Code

  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>
View Code

  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> 
View Code

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專案即可

       

 

相關文章