神创天陆活动专区

整合SSM框架-基于xml

目录

1.1 Maven整合SSM框架简介

1.1.1 SSM框架整合概述

1.2 Maven整合SSM框架整合详解

1.2.1 在eclipse中创建一个工程

1.2.2 将Spring框架整合到Maven中

1.2.3 将Mybatis框架整合至Maven中

1.2.4 将SpringMVC框架整合到Maven中

1.3 SSM框架整合总结

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

1.3.2 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,填写好坐标三要素

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

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

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

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

1

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 4.0.0

5 cn.baidu

6 UER-ORDER-SSM

7 war

8 0.0.1-SNAPSHOT

9 UER-ORDER-SSM Maven Webapp

10 http://maven.apache.org

11

12

13 junit

14 junit

15 3.8.1

16 test

17

18

19

20 UER-ORDER-SSM

21

22

23

24 org.apache.maven.plugins

25 maven-compiler-plugin

26

27 1.8

28 1.8

29 UTF-8

30

31

32

33

34

35

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

2 org.springframework

3 spring-context

4 4.3.7.RELEASE

5

编写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

2

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

26

27

28

29

编写测试代码,测试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

3 com.alibaba

4 druid

5 1.0.14

6

7

8

9 org.springframework

10 spring-jdbc

11 4.3.7.RELEASE

12

13

14

15 mysql

16 mysql-connector-java

17 5.0.8

18

19

20

21 org.mybatis

22 mybatis

23 3.4.5

24

25

26

27 org.mybatis

28 mybatis-spring

29 1.3.1

30

在spring.xml中配置对应的bean

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

1

2

3

4

5

6

7

8

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

1

2

3

4

5

6

7

8

9

10

11

12

13

编写mybatis-config.xml配置文件

1.在src/main/resources目录下编写mybatis-config.xml配置文件

1

2

3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

4 "http://mybatis.org/dtd/mybatis-3-config.dtd">

5

6

7

8

9

10

11

编写xxxMapper.xml配置文件

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

1

2

3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

4

5

编写测试代码,测试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

2

3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

4

5

8

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

2

3 org.apache.tomcat.maven

4 tomcat7-maven-plugin

5 2.2

6

7

8

9 80

10

11 /

12

13 utf-8

14 utf-8

15

16

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

导入SpringMVC所需要的依赖

1.SpringMVC需要导入spring-web和spring-webMVC依赖

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

1

2

3 org.springframework

4 spring-web

5 4.3.7.RELEASE

6

7

8

9 org.springframework

10 spring-webmvc

11 4.3.7.RELEASE

12

13

14

15 com.fasterxml.jackson.core

16 jackson-core

17 2.8.8

18

19

20 com.fasterxml.jackson.core

21 jackson-databind

22 2.8.8

23

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

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

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

1

2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

3 "http://java.sun.com/dtd/web-app_2_3.dtd" >

4

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 Archetype Created Web Application

9

10

11 springmvc

12 org.springframework.web.servlet.DispatcherServlet

13

14

15 contextConfigLocation

16 classpath:spring*.xml

17

18

19

20 springmvc

21 /*

22

23 index.html

24

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

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

1

2

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

26

27

28

29

30

31 class="org.springframework.web.servlet.view.InternalResourceViewResolver">

32

33

34

35

36

37

38

39

40

编写测试代码,测试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

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 4.0.0

5 cn.baidu

6 UER-ORDER-SSM

7 war

8 0.0.1-SNAPSHOT

9 UER-ORDER-SSM Maven Webapp

10 http://maven.apache.org

11

12

13 junit

14 junit

15 3.8.1

16 test

17

18

19 org.springframework

20 spring-context

21 4.3.7.RELEASE

22

23

24

25 com.alibaba

26 druid

27 1.0.14

28

29

30

31 org.springframework

32 spring-jdbc

33 4.3.7.RELEASE

34

35

36

37 mysql

38 mysql-connector-java

39 5.0.8

40

41

42

43 org.mybatis

44 mybatis

45 3.4.5

46

47

48

49 org.mybatis

50 mybatis-spring

51 1.3.1

52

53

54

55 org.springframework

56 spring-web

57 4.3.7.RELEASE

58

59

60

61 org.springframework

62 spring-webmvc

63 4.3.7.RELEASE

64

65

66

67 com.fasterxml.jackson.core

68 jackson-core

69 2.8.8

70

71

72 com.fasterxml.jackson.core

73 jackson-databind

74 2.8.8

75

76

77

78 UER-ORDER-SSM

79

80

81

82 org.apache.maven.plugins

83 maven-compiler-plugin

84

85 1.8

86 1.8

87 UTF-8

88

89

90

91

92 org.apache.tomcat.maven

93 tomcat7-maven-plugin

94 2.2

95

96

97

98 80

99

100 /

101

102 utf-8

103 utf-8

104

105

106

107

108

1.pom.xml整体配置文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

cn.baidu

UER-ORDER-SSM

war

0.0.1-SNAPSHOT

UER-ORDER-SSM Maven Webapp

http://maven.apache.org

junit

junit

3.8.1

test

org.springframework

spring-context

4.3.7.RELEASE

com.alibaba

druid

1.0.14

org.springframework

spring-jdbc

4.3.7.RELEASE

mysql

mysql-connector-java

5.0.8

org.mybatis

mybatis

3.4.5

org.mybatis

mybatis-spring

1.3.1

org.springframework

spring-web

4.3.7.RELEASE

org.springframework

spring-webmvc

4.3.7.RELEASE

com.fasterxml.jackson.core

jackson-core

2.8.8

com.fasterxml.jackson.core

jackson-databind

2.8.8

UER-ORDER-SSM

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

UTF-8

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

80

/

utf-8

utf-8

2.web.xml整体配置文件

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

Archetype Created Web Application

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring*.xml

springmvc

/*

index.html

3.spring.xml整体配置文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

4.mybatis-config.xml整体配置文件

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

5.spring-mvc.xml整体配置文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

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

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

1.3.2 SSM整合易错点

错误调试思路

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

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

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

SSM框架整合出现异常,一般都是由前一个异常引起后面异常的抛出,所以查看异常的时候,查看报错的第一句(第一句内容比较长)

例如报如下错误:出现异常按照以下红色字体标识往下找,通常找到最后一行即可找到出错导致的原因,或者某些关键字,按照其提示的关键字,自己到对应的地方仔细检查,排查错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressedthrough field 'studentService'; nested exception isorg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentServiceImp': Unsatisfied dependencyexpressed through field 'studentMapper'; nested exception isorg.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 isorg.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [E:\workspace_v1\ORDER-USER-SSM\target\classes\mapper\studentMapper.xml]'; nested exception isorg.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: 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项目即可