SpringMVC第三天
第一章:搭建整合环境
1. 搭建整合环境
整合说明:SSM整合可以使用多种方式,咱们会选择XML + 注解的方式
整合的思路
先搭建整合的环境
先把Spring的配置搭建完成
再使用Spring整合SpringMVC框架
最后使用Spring整合MyBatis框架
创建数据库和表结构
- 语句
1
2
3
4
5
6
7create database ssm;
use ssm;
create table account(
id int primary key auto_increment,
name varchar(20),
money double
)
- 语句
创建maven的工程(今天会使用到工程的聚合和拆分的概念,这个技术maven高级会讲)
创建ssm_parent父工程(打包方式选择pom,必须的)
创建ssm_web子模块(打包方式是war包)
创建ssm_service子模块(打包方式是jar包)
创建ssm_dao子模块(打包方式是jar包)
创建ssm_domain子模块(打包方式是jar包)
web依赖于service,service依赖于dao,dao依赖于domain
在ssm_parent的pom.xml文件中引入坐标依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131<properties>
<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>compile</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>
<pluginManagement>
<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>
</pluginManagement>
</build>
部署ssm_web的项目,只要把ssm_web项目加入到tomcat服务器中即可
编写实体类,在ssm_domain项目中编写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34import java.io.Serializable;
public class Account implements Serializable {
private static final long serialVersionUID = 7355810572012650248L;
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}编写dao接口
1
2
3
4
5
6
7
8
9
10
11import cn.itcast.domain.Account;
import java.util.List;
public interface AccountDao {
public void saveAccount(Account account);
public List<Account> findAll();
}编写service接口和实现类
1 |
|
第二章:Spring框架代码的编写
- 搭建和测试Spring的开发环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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">
<!-- 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架去管理 -->
<context:component-scan base-package="cn.itcast">
<!-- 配置要忽略的注解 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans> - 在ssm_web项目中编写测试方法,进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import cn.itcast.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceTest {
public void run1() {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
AccountService as = (AccountService) ac.getBean("accountService");
as.findAll();
}
}第三章:Spring整合SpringMVC框架
1. 搭建和测试SpringMVC的开发环境
- 在web.xml中配置DispatcherServlet前端控制器
1 | <!-- 配置前端控制器:服务器启动必须加载,需要加载springmvc.xml配置文件 --> |
在web.xml中配置DispatcherServlet过滤器解决中文乱码
1
2
3
4
5
6
7
8
9
10
11
12
13
14<!-- 配置解决中文乱码的过滤器 -->
<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>创建springmvc.xml的配置文件,编写配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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="cn.itcast">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</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:resources location="/css/" mapping="/css/**" />
<mvc:resources location="../images/" mapping="../images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!-- 开启对SpringMVC注解的支持 -->
<mvc:annotation-driven />
</beans>测试SpringMVC的框架搭建是否成功
- 编写index.jsp和list.jsp编写,超链接
1
<a href="account/findAll">查询所有</a>
- 创建AccountController类,编写方法,进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
public class AccountController {
/**
* 查询所有的数据
* @return
*/
public String findAll() {
System.out.println("表现层:查询所有账户...");
return "list";
}
}2. Spring整合SpringMVC的框架
- 编写index.jsp和list.jsp编写,超链接
目的:在controller中能成功的调用service对象中的方法。
在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置
1 | <!-- 配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件 --> |
- 在controller中注入service对象,调用service对象的方法进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class AccountController {
private AccountService accoutService;
/**
* 查询所有的数据
* @return
*/
public String findAll() {
System.out.println("表现层:查询所有账户...");
accoutService.findAll();
return "list";
}
}第四章:Spring整合MyBatis框架
1. 搭建和测试MyBatis的环境
在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件
在AccountDao接口的方法上添加注解,编写SQL语句
编写测试的方法
2. Spring整合MyBatis框架
- 目的:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中
1 | <!-- 配置连接池对象 --> |
在AccountDao接口中添加@Repository注解
在service中注入dao对象,进行测试
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public interface AccountDao {
public void saveAccount( Account account );
public List<Account> findAll();
}
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void saveAccount( Account account )
{
}
public List<Account> findAll()
{
System.out.println( "业务层:查询所有账户..." );
return(accountDao.findAll() );
}
}
public class AccountController {
private AccountService accoutService;
/**
* 查询所有的数据
* @return
*/
public String findAll()
{
System.out.println( "表现层:查询所有账户..." );
List<Account> list = accoutService.findAll();
for ( Account account : list )
{
System.out.println( account );
}
return("list");
}
}配置Spring的声明式事务管理
1 | <!--配置Spring框架声明式事务管理--> |