整合SSM框架-基于xml
目录
正文
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 <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 </proj
1.pom.xml整体配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" 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"> <modelVersion>4.0.0</modelVersion> <groupId>cn.baidu</groupId> <artifactId>UER-ORDER-SSM</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>UER-ORDER-SSM Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- 连接池依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.14</version> </dependency> <!-- jdbc依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- mysql依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> </dependency> <!-- mybatis依赖资源 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- mybatis-spring依赖资源 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- 引入spring-web依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- 引入spring-webMVC依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- 引入Jackson依赖 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8</version> </dependency> </dependencies> <build> <finalName>UER-ORDER-SSM</finalName> <!-- source插件 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 引入Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <!-- Tomcat的启动配置 --> <configuration> <!-- 端口号访问路径默认80端口 --> <port>80</port> <!-- 应用程序的访问路径 --> <path>/</path> <!-- 接收数据编解码的格式为utf-8 --> <uriEncoding>utf-8</uriEncoding> <useBodyEncodingForURI>utf-8</useBodyEncodingForURI> </configuration> </plugin> </plugins> </build> </project>
2.web.xml整体配置文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <display-name>Archetype Created Web Application</display-name> <!-- 创建dispachersevlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置初始化参数,使得Tomcat启动的时候,加载spring*.xml文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <welcome-file-list>index.html</welcome-file-list> </web-app>
3.spring.xml整体配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!-- 开启包扫描 --> <context:component-scan base-package="cn.baidu"></context:component-scan> <!-- 开启注解DI --> <context:annotation-config></context:annotation-config> <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 4个属性,数据库驱动,URL,用户名和密码 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///mssm"/> <property name="username" value="root"/> <property name="password" value="041X"/> </bean> <!-- 配置sqlSession,动态代理实现持久层sqlSession接口实现的对象 --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 绑定数据源即获取connection对象 --> <property name="dataSource" ref="dataSource"/> <!-- 加载mybatis的独立配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 扫描映射xxxMapper.xml映射文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 配置mapper接口的扫描配置用于创建mapper接口的实现类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.baidu.mapper"/> </bean> </beans>
4.mybatis-config.xml整体配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 打开驼峰命名设置,用于封装持久层数据对象的内容-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
5.spring-mvc.xml整体配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!-- 静态资源html js css访问 --> <mvc:resources location="/" mapping="/**"/> <!-- springmvc开启所有注解功能的标签 --> <mvc:annotation-driven/> <!-- 视图解析的前后缀 --> <bean id="viewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前后缀拼接 --> <property name="prefix" value="views/"/> <property name="suffix" value=".html"></property> </bean> <!-- 以下配置可以解决springmvc返回数据的编解码问题 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportMediaTypes" value="text/html;charset=utf-8"></property> </bean> </beans>
6.xxxMapper.xml整体配置文件,此配置文件多变,以下只是范例
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.baidu.mapper.StudentMapper"> <select id="queryOne" resultType="cn.baidu.domain.Student"> select * from student where id=#{id}; </select> </mapper>
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项目即可