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插件
1234567891011121314151617181920212223242526272829303132333435
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" modelVersion4.0.0/modelVersion groupIdcn.baidu/groupId artifactIdUER-ORDER-SSM/artifactId packagingwar/packaging version0.0.1-SNAPSHOT/version nameUER-ORDER-SSM Maven Webapp/name urlhttp://maven.apache.org/url dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency /dependencies build finalNameUER-ORDER-SSM/finalName !-- source插件 -- plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId configuration source1.8/source target1.8/target encodingUTF-8/encoding /configuration /plugin !-- Tomcat插件整合SpringMVC的时候补充 -- /plugins /build/project
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"
modelVersion4.0.0/modelVersion
groupIdcn.baidu/groupId
artifactIdUER-ORDER-SSM/artifactId
packagingwar/packaging
version0.0.1-SNAPSHOT/version
nameUER-ORDER-SSM Maven Webapp/name
urlhttp://maven.apache.org/url
dependencies
dependency
groupIdjunit/groupId
artifactIdjunit/artifactId
version3.8.1/version
scopetest/scope
/dependency
/dependencies
build
finalNameUER-ORDER-SSM/finalName
!– source插件 –
plugins
plugin
groupIdorg.apache.maven.plugins/groupId
artifactIdmaven-compiler-plugin/artifactId
configuration
source1.8/source
target1.8/target
encodingUTF-8/encoding
/configuration
/plugin
!– Tomcat插件整合SpringMVC的时候补充 –
/plugins
/build
/project
6.注意:
-
- maven插件资源,将当前maven工程进行compile编译时加载,完成java1.8,1.7的使用,这里统一使用1.8的版本
- 往下整合关于pom.xml文件的配置内容,本人只给出需要添加的部分,读者往上面代码中添加即可
- 整体思路
- 先在Pom.xml文件中导入Spring框架的依赖资源
- 编写Spring的核心配置文件spring.xml
1.2.2 将Spring框架整合到Maven中
** **1.先在pom.xml文件中导入Spring框架所依赖的资源
2.然后编写spring和核心配置文件,spring.xml,该配置文件进行包扫描,打开注解配置
3.往下的整合只要跟创建对象,对象管理相关的配置都在spring.xml文件中配置
4.编写测试代码,测试整合是否成功
** **1.导入spring-context依赖资源,但是依赖具有传递性,导入该资源可能也会引入其他依赖资源
12345
dependency groupIdorg.springframework/groupId artifactIdspring-context/artifactId version4.3.7.RELEASE/version/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-context/artifactId
version4.3.7.RELEASE/version
/dependency
** **1.将spring.xml配置文件放到src/main/resources目录下
2.在src目录下的资源在编译的时候都会将字节码加载到target/classes中
3.在配置文件中会经常看见”classpath:xxx.xml”路径来读取文件,而classpath路径是什么位置?
4.在maven项目中classpath路径默认src/main/resource路径
5.往后跟创建对象相关的配置需要放在spring的核心配置文件中,用于创建bean对象
1234567891011121314151617181920212223242526272829
?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 /beans
?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
/beans
- 编写测试代码,测试maven整合spring框架是否成功
** **1.依据三层架构建好目录结构
2.编写控制层测试程序
1234567891011121314151617181920
package cn.baidu.controller; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller; import cn.baidu.service.HelloService; @Controllerpublic class HelloController { // 注入service层对象 @Autowired private HelloService helloService; public String sayHi(String name){// 调用对应service层的方法 return helloService.sayHi(name); } }
package cn.baidu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import cn.baidu.service.HelloService;
@Controller
public class HelloController {
// 注入service层对象
@Autowired
private HelloService helloService;
public String sayHi(String name){
// 调用对应service层的方法
return helloService.sayHi(name);
}
}
3.编写业务逻辑层的接口
12345678
package cn.baidu.service; //service层的接口public interface HelloService { public String sayHi(String name); }
package cn.baidu.service;
//service层的接口
public interface HelloService {
public String sayHi(String name);
}
4.编写业务层的实现类
1234567891011121314151617181920
package cn.baidu.service; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service; import cn.baidu.mapper.HelloMapper; @Servicepublic class HelloServiceImp implements HelloService{ // 注入持久层对象 @Autowired private HelloMapper helloMapper; @Override public String sayHi(String name) {// 调用持久层方法 return helloMapper.sayHi(name); } }
package cn.baidu.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.baidu.mapper.HelloMapper;
@Service
public class HelloServiceImp implements HelloService{
// 注入持久层对象
@Autowired
private HelloMapper helloMapper;
@Override
public String sayHi(String name) {
// 调用持久层方法
return helloMapper.sayHi(name);
}
}
5.编写持久层接口
1234567
package cn.baidu.mapper; public interface HelloMapper { public String sayHi(String name); }
package cn.baidu.mapper;
public interface HelloMapper {
public String sayHi(String name);
}
6.编写持久层实现类
12345678910111213
package cn.baidu.mapper; import org.springframework.stereotype.Repository; @Repositorypublic class HelloMapperImp implements HelloMapper{ @Override public String sayHi(String name) { return "Hi,欢迎测试Spring框架的搭建情况"; } }
package cn.baidu.mapper;
import org.springframework.stereotype.Repository;
@Repository
public class HelloMapperImp implements HelloMapper{
@Override
public String sayHi(String name) {
return "Hi,欢迎测试Spring框架的搭建情况";
}
}
7.变成测试类,测试整合情况
123456789101112131415161718192021222324
package cn.baidu.test; import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.baidu.controller.HelloController; public class TestHello { @Test public void test(){ // 初始化Spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");// 获取控制层对象 HelloController helloController = context.getBean(HelloController.class);// 调用控制层方法 String str = helloController.sayHi("谭小杰"); System.out.println(str); } }
package cn.baidu.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.baidu.controller.HelloController;
public class TestHello {
@Test
public void test(){
// 初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext(“spring.xml”);
// 获取控制层对象
HelloController helloController = context.getBean(HelloController.class);
// 调用控制层方法
String str = helloController.sayHi(“谭小杰”);
System.out.println(str);
}
}
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
123456789101112131415161718192021222324252627282930
!-- 连接池依赖 --dependency groupIdcom.alibaba/groupId artifactIddruid/artifactId version1.0.14/version/dependency!-- jdbc依赖 --dependency groupIdorg.springframework/groupId artifactIdspring-jdbc/artifactId version4.3.7.RELEASE/version/dependency!-- mysql依赖 --dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.0.8/version/dependency!-- mybatis依赖资源 --dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId version3.4.5/version/dependency!-- mybatis-spring依赖资源 --dependency groupIdorg.mybatis/groupId artifactIdmybatis-spring/artifactId version1.3.1/version/dependency
!– 连接池依赖 –
dependency
groupIdcom.alibaba/groupId
artifactIddruid/artifactId
version1.0.14/version
/dependency
!– jdbc依赖 –
dependency
groupIdorg.springframework/groupId
artifactIdspring-jdbc/artifactId
version4.3.7.RELEASE/version
/dependency
!– mysql依赖 –
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
version5.0.8/version
/dependency
!– mybatis依赖资源 –
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis/artifactId
version3.4.5/version
/dependency
!– mybatis-spring依赖资源 –
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis-spring/artifactId
version1.3.1/version
/dependency
- 在spring.xml中配置对应的bean
** **1.配置数据源的bean标签,该标签主要数用于创建连接数据库对象
12345678
!-- 配置数据源 --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="root"//bean
!– 配置数据源 –
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=”root”/
/bean
2.配置sqlSession的bean标签,该标签用于创建session对象,用于执行sql语句和获取执行结果
12345678910111213
!-- 配置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
!– 配置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
- 编写mybatis-config.xml配置文件
** **1.在src/main/resources目录下编写mybatis-config.xml配置文件
1234567891011
?xml version="1.0" encoding="UTF-8" ?!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"configuration !-- 打开驼峰命名设置,用于封装持久层数据对象的内容-- settings setting name="mapUnderscoreToCamelCase" value="true"/ /settings /configuration
?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
- 编写xxxMapper.xml配置文件
** **1.在src/main/resources/mapper目录下配置对应的xxxMapper.xml文件,将sql语句添加其中
12345
?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"/mapper
?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”
/mapper
- 编写测试代码,测试Mybatis整合是否成功
** **1.在连接的数据库中建立mssm数据库,创建student表格,其有如下字段和数据
1234
//创建表create table student(id varchar(225),name varchar(225));//添加数据insert into student values('a','王重阳'),('b','欧阳锋'),('c','黄药师');
//创建表
create table student(id varchar(225),name varchar(225));
//添加数据
insert into student values(‘a’,’王重阳’),(‘b’,’欧阳锋’),(‘c’,’黄药师’);
2.编写一个用于封装数据的domain类
123456789101112131415161718192021222324252627282930313233
package cn.baidu.domain; public class Student { // 定义属性 private String id; private String name;// 定义构造方法 public Student(){} public Student(String id, String name) { super(); this.id = id; this.name = name; }// 定义getter和sett方法 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }
package cn.baidu.domain;
public class Student {
// 定义属性
private String id;
private String name;
// 定义构造方法
public Student(){}
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
// 定义getter和sett方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return “Student [id=” + id + “, name=” + name + “]”;
}
}
3.编写控制层代码
123456789101112131415161718
package cn.baidu.controller; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller; import cn.baidu.domain.Student;import cn.baidu.service.StudentService; @Controllerpublic class StudentController { @Autowired private StudentService studentService; public Student queryStudentById(String id){ return studentService.queryStudentById(id); } }
package cn.baidu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import cn.baidu.domain.Student;
import cn.baidu.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
public Student queryStudentById(String id){
return studentService.queryStudentById(id);
}
}
4.编写业务层接口
12345678
package cn.baidu.service; import cn.baidu.domain.Student; public interface StudentMapper { public Student queryOne(String id);}
package cn.baidu.service;
import cn.baidu.domain.Student;
public interface StudentMapper {
public Student queryOne(String id);
}
5.编写业务层的实现类
12345678910111213141516171819
package cn.baidu.service; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service; import cn.baidu.domain.Student; @Servicepublic class StudentServiceImp implements StudentService{ @Autowired private StudentMapper studentMapper; @Override public Student queryStudentById(String id) { return studentMapper.queryOne(id); } }
package cn.baidu.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.baidu.domain.Student;
@Service
public class StudentServiceImp implements StudentService{
@Autowired
private StudentMapper studentMapper;
@Override
public Student queryStudentById(String id) {
return studentMapper.queryOne(id);
}
}
6.编写studentMapper.xml文件,把写入查询sql语句
12345678
?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
?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
7.编写对应的Mapper配置文件对应的接口
12345678
package cn.baidu.mapper; import cn.baidu.domain.Student; public interface StudentMapper { public Student queryOne(String id);}
package cn.baidu.mapper;
import cn.baidu.domain.Student;
public interface StudentMapper {
public Student queryOne(String id);
}
8.编写测试类,测试是否整合成功
1234567891011121314151617181920212223242526
package cn.baidu.test; import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.baidu.controller.StudentController;import cn.baidu.domain.Student; public class StudentTest { @Test public void test(){ // 初始化Spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");// 获取控制层对象 StudentController studentController = context.getBean(StudentController.class);// 调用控制层方法 Student student = studentController.queryStudentById("c"); System.out.println(student); } }
package cn.baidu.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.baidu.controller.StudentController;
import cn.baidu.domain.Student;
public class StudentTest {
@Test
public void test(){
// 初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext(“spring.xml”);
// 获取控制层对象
StudentController studentController = context.getBean(StudentController.class);
// 调用控制层方法
Student student = studentController.queryStudentById(“c”);
System.out.println(student);
}
}
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插件
12345678910111213141516
!-- 引入Tomcat插件 --plugin groupIdorg.apache.tomcat.maven/groupId artifactIdtomcat7-maven-plugin/artifactId version2.2/version !-- Tomcat的启动配置 -- configuration !-- 端口号访问路径默认80端口 -- port80/port !-- 应用程序的访问路径 -- path//path !-- 接收数据编解码的格式为utf-8 -- uriEncodingutf-8/uriEncoding useBodyEncodingForURIutf-8/useBodyEncodingForURI /configuration/plugin
!– 引入Tomcat插件 –
plugin
groupIdorg.apache.tomcat.maven/groupId
artifactIdtomcat7-maven-plugin/artifactId
version2.2/version
!– Tomcat的启动配置 –
configuration
!– 端口号访问路径默认80端口 –
port80/port
!– 应用程序的访问路径 –
path//path
!– 接收数据编解码的格式为utf-8 –
uriEncodingutf-8/uriEncoding
useBodyEncodingForURIutf-8/useBodyEncodingForURI
/configuration
/plugin
注:如果不想引入Tomcat插件,将项目打成war包扔至外部Tomcat也可以,这里选择导入插件
- 导入SpringMVC所需要的依赖
** ** 1.SpringMVC需要导入spring-web和spring-webMVC依赖
2.有时为了将对象转化成json字符串,还需要Jackson依赖
1234567891011121314151617181920212223
!-- 引入spring-web依赖 --dependency groupIdorg.springframework/groupId artifactIdspring-web/artifactId version4.3.7.RELEASE/version/dependency!-- 引入spring-webMVC依赖 --dependency groupIdorg.springframework/groupId artifactIdspring-webmvc/artifactId version4.3.7.RELEASE/version/dependency!-- 引入Jackson依赖 --dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-core/artifactId version2.8.8/version/dependencydependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-databind/artifactId version2.8.8/version/dependency
!– 引入spring-web依赖 –
dependency
groupIdorg.springframework/groupId
artifactIdspring-web/artifactId
version4.3.7.RELEASE/version
/dependency
!– 引入spring-webMVC依赖 –
dependency
groupIdorg.springframework/groupId
artifactIdspring-webmvc/artifactId
version4.3.7.RELEASE/version
/dependency
!– 引入Jackson依赖 –
dependency
groupIdcom.fasterxml.jackson.core/groupId
artifactIdjackson-core/artifactId
version2.8.8/version
/dependency
dependency
groupIdcom.fasterxml.jackson.core/groupId
artifactIdjackson-databind/artifactId
version2.8.8/version
/dependency
- 在Tomcat启动加载的web.xml文件中,配置dispacherservlet
** *1.在dispacherservlet的配置中初始化spring.xml文件,使得服务器加载spring的所有配置内容
2.让服务器加载所有配置内容的目的是使得各层的注解生效,web.xml配置如下
123456789101112131415161718192021222324
!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-nameArchetype Created Web Application/display-name !-- 创建dispachersevlet -- servlet servlet-namespringmvc/servlet-name servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class !-- 配置初始化参数,使得Tomcat启动的时候,加载spring*.xml文件 -- init-param param-namecontextConfigLocation/param-name param-valueclasspath:spring*.xml/param-value /init-param /servlet servlet-mapping servlet-namespringmvc/servlet-name url-pattern/*/url-pattern /servlet-mapping welcome-file-listindex.html/welcome-file-list/web-app
!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-nameArchetype Created Web Application/display-name
!– 创建dispachersevlet –
servlet
servlet-namespringmvc/servlet-name
servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class
!– 配置初始化参数,使得Tomcat启动的时候,加载spring*.xml文件 –
init-param
param-namecontextConfigLocation/param-name
param-valueclasspath:spring*.xml/param-value
/init-param
/servlet
servlet-mapping
servlet-namespringmvc/servlet-name
url-pattern/*/url-pattern
/servlet-mapping
welcome-file-listindex.html/welcome-file-list
/web-app
- 在src/main/resources目录下编写spring-mvc.xml文件
** **1.文件中开启mvc注解生效配置和视图解析器的配置
12345678910111213141516171819202122232425262728293031323334353637383940
?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
?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
- 编写测试代码,测试SpringMvc整合是否成功
** **1.测试代码实现的功能需求:通过浏览器,传递一个get的请求参数id=a/b/c
2.项目返回一个从数据库查询的student对象,页面的浏览器展示
3.在整合mybatis测试代码的基础上,编写该测试代码,把之前的test类删除
4.修改控制层的代码,如下,其他的与mybatis测试代码一致,不再展示
1234567891011121314151617181920212223242526
package cn.baidu.controller; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody; import cn.baidu.domain.Student;import cn.baidu.service.StudentService; @Controllerpublic class StudentController { @Autowired private StudentService studentService; // 以下两个注解,他们的作用分别是:将以下的方法变成处理器// RequestBody是实现将返回的对象变成json字符串,展示在浏览器端 @RequestMapping("queryStudent") @ResponseBody public Student queryStudentById(String id){ return studentService.queryStudentById(id); } }
package cn.baidu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.baidu.domain.Student;
import cn.baidu.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
// 以下两个注解,他们的作用分别是:将以下的方法变成处理器
// RequestBody是实现将返回的对象变成json字符串,展示在浏览器端
@RequestMapping(“queryStudent”)
@ResponseBody
public Student queryStudentById(String id){
return studentService.queryStudentById(id);
}
}
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整体配置文件
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
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" modelVersion4.0.0/modelVersion groupIdcn.baidu/groupId artifactIdUER-ORDER-SSM/artifactId packagingwar/packaging version0.0.1-SNAPSHOT/version nameUER-ORDER-SSM Maven Webapp/name urlhttp://maven.apache.org/url dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency dependency groupIdorg.springframework/groupId artifactIdspring-context/artifactId version4.3.7.RELEASE/version /dependency !-- 连接池依赖 -- dependency groupIdcom.alibaba/groupId artifactIddruid/artifactId version1.0.14/version /dependency !-- jdbc依赖 -- dependency groupIdorg.springframework/groupId artifactIdspring-jdbc/artifactId version4.3.7.RELEASE/version /dependency !-- mysql依赖 -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.0.8/version /dependency !-- mybatis依赖资源 -- dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId version3.4.5/version /dependency !-- mybatis-spring依赖资源 -- dependency groupIdorg.mybatis/groupId artifactIdmybatis-spring/artifactId version1.3.1/version /dependency !-- 引入spring-web依赖 -- dependency groupIdorg.springframework/groupId artifactIdspring-web/artifactId version4.3.7.RELEASE/version /dependency !-- 引入spring-webMVC依赖 -- dependency groupIdorg.springframework/groupId artifactIdspring-webmvc/artifactId version4.3.7.RELEASE/version /dependency !-- 引入Jackson依赖 -- dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-core/artifactId version2.8.8/version /dependency dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-databind/artifactId version2.8.8/version /dependency /dependencies build finalNameUER-ORDER-SSM/finalName !-- source插件 -- plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId configuration source1.8/source target1.8/target encodingUTF-8/encoding /configuration /plugin !-- 引入Tomcat插件 -- plugin groupIdorg.apache.tomcat.maven/groupId artifactIdtomcat7-maven-plugin/artifactId version2.2/version !-- Tomcat的启动配置 -- configuration !-- 端口号访问路径默认80端口 -- port80/port !-- 应用程序的访问路径 -- path//path !-- 接收数据编解码的格式为utf-8 -- uriEncodingutf-8/uriEncoding useBodyEncodingForURIutf-8/useBodyEncodingForURI /configuration /plugin /plugins /build/project
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"
modelVersion4.0.0/modelVersion
groupIdcn.baidu/groupId
artifactIdUER-ORDER-SSM/artifactId
packagingwar/packaging
version0.0.1-SNAPSHOT/version
nameUER-ORDER-SSM Maven Webapp/name
urlhttp://maven.apache.org/url
dependencies
dependency
groupIdjunit/groupId
artifactIdjunit/artifactId
version3.8.1/version
scopetest/scope
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-context/artifactId
version4.3.7.RELEASE/version
/dependency
!– 连接池依赖 –
dependency
groupIdcom.alibaba/groupId
artifactIddruid/artifactId
version1.0.14/version
/dependency
!– jdbc依赖 –
dependency
groupIdorg.springframework/groupId
artifactIdspring-jdbc/artifactId
version4.3.7.RELEASE/version
/dependency
!– mysql依赖 –
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
version5.0.8/version
/dependency
!– mybatis依赖资源 –
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis/artifactId
version3.4.5/version
/dependency
!– mybatis-spring依赖资源 –
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis-spring/artifactId
version1.3.1/version
/dependency
!– 引入spring-web依赖 –
dependency
groupIdorg.springframework/groupId
artifactIdspring-web/artifactId
version4.3.7.RELEASE/version
/dependency
!– 引入spring-webMVC依赖 –
dependency
groupIdorg.springframework/groupId
artifactIdspring-webmvc/artifactId
version4.3.7.RELEASE/version
/dependency
!– 引入Jackson依赖 –
dependency
groupIdcom.fasterxml.jackson.core/groupId
artifactIdjackson-core/artifactId
version2.8.8/version
/dependency
dependency
groupIdcom.fasterxml.jackson.core/groupId
artifactIdjackson-databind/artifactId
version2.8.8/version
/dependency
/dependencies
build
finalNameUER-ORDER-SSM/finalName
!– source插件 –
plugins
plugin
groupIdorg.apache.maven.plugins/groupId
artifactIdmaven-compiler-plugin/artifactId
configuration
source1.8/source
target1.8/target
encodingUTF-8/encoding
/configuration
/plugin
!– 引入Tomcat插件 –
plugin
groupIdorg.apache.tomcat.maven/groupId
artifactIdtomcat7-maven-plugin/artifactId
version2.2/version
!– Tomcat的启动配置 –
configuration
!– 端口号访问路径默认80端口 –
port80/port
!– 应用程序的访问路径 –
path//path
!– 接收数据编解码的格式为utf-8 –
uriEncodingutf-8/uriEncoding
useBodyEncodingForURIutf-8/useBodyEncodingForURI
/configuration
/plugin
/plugins
/build
/project
2.web.xml整体配置文件
123456789101112131415161718192021222324
!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-nameArchetype Created Web Application/display-name !-- 创建dispachersevlet -- servlet servlet-namespringmvc/servlet-name servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class !-- 配置初始化参数,使得Tomcat启动的时候,加载spring*.xml文件 -- init-param param-namecontextConfigLocation/param-name param-valueclasspath:spring*.xml/param-value /init-param /servlet servlet-mapping servlet-namespringmvc/servlet-name url-pattern/*/url-pattern /servlet-mapping welcome-file-listindex.html/welcome-file-list/web-app
!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-nameArchetype Created Web Application/display-name
!– 创建dispachersevlet –
servlet
servlet-namespringmvc/servlet-name
servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class
!– 配置初始化参数,使得Tomcat启动的时候,加载spring*.xml文件 –
init-param
param-namecontextConfigLocation/param-name
param-valueclasspath:spring*.xml/param-value
/init-param
/servlet
servlet-mapping
servlet-namespringmvc/servlet-name
url-pattern/*/url-pattern
/servlet-mapping
welcome-file-listindex.html/welcome-file-list
/web-app
3.spring.xml整体配置文件
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
?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
?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整体配置文件
1234567891011
?xml version="1.0" encoding="UTF-8" ?!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"configuration !-- 打开驼峰命名设置,用于封装持久层数据对象的内容-- settings setting name="mapUnderscoreToCamelCase" value="true"/ /settings /configuration
?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整体配置文件
12345678910111213141516171819202122232425262728293031323334353637383940
?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
?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整体配置文件,此配置文件多变,以下只是范例
12345678
?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
?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 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_v1ORDER-USER-SSMtargetclassescnbaidumapperStudentMapper.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_v1ORDER-USER-SSMtargetclassesmapperstudentMapper.xml]'; nested exception is org.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类的路径是否写正确
-
-
- 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项目即可
原文始发于:
错误原因:配置文件找不到Student类,说明在填写配置文件全路径可能写错了
** **1.在添加pom内容的时候,工程可能会出现一个红叉子,查看problem的内容标签,会报如下错误