1.搭建整合环境
1.1整合的思路
- 先搭建整合的环境
- 先把Spring的配置搭建完成
- 再使用Spring整合SpringMVC框架
- 最后使用Spring整合MyBatis框架
ssm整合 : ssm中有那些配置文件,每个配置文件中必须要有那些内容
spring:
配置文件: applicationContent.xml
内容:
扫描包(service) , 数据源 , sqlSessionFactory ,
和mybatis整合的扫描, dubbox配置
springmvc:
配置文件 : springmvc.xml
内容:
扫描包(controller), 视图解析器, 驱动注解
mybatis:
配置文件: mapper文件(可选的)、sqlMapConfig.xml
内容: 啥也没有 但是 有个头约束 web.xml :
spring和web整合的监听器, springmvc的前端控制器 , 解决post乱码的过滤器
pom.xml : spring + springmvc+ mybatis + mybatis和spring整合 + 数据库驱动 + 连接池 + dubbox +zookeeper
create database springmvc;
use springmvc;
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
1.3创建maven的工程
2.开发准备
2.1 第一步:创建web工程
2.2 第二步:在pom.xml文件中导入jar包
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
2.3 第三步:创建实体类
创建包com.ssm.domain,创建类Account.java
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
}
//下面添加set、get方法 重写tostring
2.4第四步:创建Dao
创建包com.ssm.dao,创建类AccountDao.java
/**
* AccountMapper接口,不用编写实现类,框架生成代理对象
*/
public interface AccountDao {
public List<Account> findAll();
public void saveAccount(Account account);
}
2.5第五步:创建Service
创建包com.ssm.service,创建接口AccountService.java
public interface AccountService {
public List<Account> findAll();
public void saveAccount(Account account);
}
创建类AccountServiceImpl,实现接口
/**
* 账号的实现类
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public List<Account> findAll() {
System.out.println("业务层:查询所有的帐户...");
return accountDao.findAll();
}
public void saveAccount(Account account) {
System.out.println("业务层:保存帐户...");
accountDao.saveAccount(account);
}
}
2.6 第六步:创建Controller
创建包com.ssm.controller,创建类AccountController.java
/**
* 帐户
*/
public class AccountController {
private AccountService accountService;
/**
* 查询所有
* @return
*/
public String findAll() {
System.out.println("表现层:查询所有账户...");
List<Account> list = accountService.findAll();
for (Account account : list) {
System.out.println(account);
}
return "success";
}
/**
* 保存
* @return
*/
public String saveAccount(Account account) {System.out.println("表现层:保存账户...");
accountService.saveAccount(account);
return "success";
}
}
最终包和类的结构是:
3.搭建Spring的环境
3.1applicationContext.xml
在项目中的resources文件夹下创建applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.ssm">
<!-- 忽略某些注解,把Controller给忽略了 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
3.2AccountService.java
/**
* 账号的实现类
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
}
3.3 测试类
在项目中编写测试方法,进行测试
创建测试类:com.test.TestService.java
public class TestService {
/**
* 测试Service层
*/
@Test
public void testService(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService as = (AccountService) ac.getBean("accountService");
as.findAll();
}
}
测试结果:
4.SpringMVC框架
4.1web.xml
在web.xml中配置DispatcherServlet前端控制器
<!-- 配置前端控制器:服务器启动必须加载,需要加载springmvc.xml配置文件 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在web.xml中配置CharacterEncodingFilter过滤器解决post请求中文乱码
<!-- 配置解决中文乱码的过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.2 springmvc.xml
在resources下,创建springmvc.xml
创建springmvc.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描controller的注解,别的不扫描 -->
<context:component-scan base-package="com.ssm">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!-- 排除Service注解的扫描,让spring扫描,否则事务可能会失效 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"></context:exclude-filter>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- JSP文件所在的目录 -->
<property name="prefix" value="/WEB-INF/pages/" />
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 设置静态资源不过滤,可以使用:--><mvc:default-servlet-handler></mvc:default-servlet-handler> <!-- <mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />-->
<!-- 开启对SpringMVC注解的支持 -->
<mvc:annotation-driven />
</beans>
其中:<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
表示springmvc的注解包含@Controller的注解。
为什么要包含@Controller注解呢?
1)SpringMVC IOC容器 存在Controller的实例对象
2)Spring IOC容器 存在Controller的实例和Service层实例以及Dao层实例 重叠会引发一些问题:
例如:事务失效、相关注解失效
解决方案一:SpringMVC只扫描 @Controller 注解、不扫描@Service注解 Spring不扫描 @Controller 注解
解决方案二:(建议使用)只让SpringMVC扫描,不让Spring扫描 在springMVC的配置中添加<import resource="applicationContext.xml"></import>
4.3 AccountController.java
/**
* 帐户
*/
@Controller
@RequestMapping(path = "account")
public class AccountController {
@Autowired
private AccountService accountService;
/**
* 查询所有
* @return
*/
@RequestMapping(path = "/findAll")
public String findAll() {
System.out.println("表现层:查询所有账户...");
return "success";
}
}
4.4 index.jsp
<body>
<h3>账号操作</h3>
<a href="account/findAll">查询所有账号</a>
</body>
4.5 success.jsp(WEB-INF/pages/success.jsp)
<body>
<h1>访问成功</h1>
</body>
发布到tomcat,进行测试
5.Spring整合SpringMVC框架
目的:在controller中能成功的调用service对象中的方法。(由spring创建对象,注入到Action中使用)
在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(默认该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)。使用contextConfigLocation的属性让它加载类路径下的applicationContext.xml。
5.1 web.xml
目的:web容器启动,加载spring容器
<!-- 配置Spring的监听器,默认加载WEB-INF下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 该监听器默认只加载WEB-INF下的applicationContext.xml配置文件,通过contextConfigLocation改变位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
5.2AccountController.java
@Controller
@RequestMapping(path = "account")
public class AccountController {
@Autowired
private AccountService accountService;
/**
* 查询所有
* @return
*/
@RequestMapping(path = "/findAll")
public String findAll() {
System.out.println("表现层:查询所有账户...");
List<Account> list = accountService.findAll();
System.out.println(list);
return "success";
}
}
测试:
6.Mybatis框架
6.1 SqlMapConfig.xml
在web项目中编写SqlMapConfig.xml的配置文件,放置到resources下,编写核心配置文件
<?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>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///springmvc"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 使用的是注解 -->
<mappers>
<!-- <mapper class="com.ssm.dao.AccountDao"/> -->
<!-- 该包下所有的dao接口都可以使用 -->
<package name="com.ssm.dao"/>
</mappers>
</configuration>