SpringMVC第三篇,SSM的整合。
环境搭建
数据库的准备
1 | create database ssm; |
导入坐标
1 | properties> |
代码准备
-
实体类
1
2
3
4
5
6public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
//getter & setter
} -
持久层
1
2
3
4public interface IAccountDao {
public List<Account> findAll();
public void saveAccount(Account account);
} -
业务层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public interface IAccountService {
public List<Account> findAll();
public void saveAccount(Account account);
}
public class AccountService implements IAccountService {
public List<Account> findAll() {
System.out.println("业务层:输出所有");
return null;
}
public void saveAccount(Account account) {
System.out.println("业务层:保存了");
}
} -
控制器
1
2public class AccountController {
}
Spring搭建
-
编写Spring配置文件
applicationContext.xml
,并导入依赖。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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,Controller交给SpringMVC处理 -->
<context:component-scan base-package="top.tyzhang">
<!-- 配置哪些不扫描 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans> -
使用注解配置业务层。
1
2
3
4
5
6
7
8
9
public class AccountService implements IAccountService {
public List<Account> findAll() {
System.out.println("业务层:输出所有");
return null;
}
//...
} -
测试Spring是否能独立运行。
1
2
3
4//加载Spring配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService as = (AccountService) applicationContext.getBean("accountService");
as.findAll();
Spring整合SpringMVC
SpringMVC搭建
-
配置
web.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
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 启动服务器,创建该Servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码过滤器-->
<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>
</web-app> -
创建
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
<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="top.tyzhang">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<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/**" />
<!-- 开启注解的支持-->
<mvc:annotation-driven/>
</beans> -
创建控制器类
1
2
3
4
5
6
7
8
9
public class AccountController {
public String findAll(){
System.out.println("表现层:查询所有信息");
return "list";
}
} -
配置相应JSP文件,启动服务器测试。
1
<a href="account/findAll">查询所有</a>
整合
-
配置监听器实现启动服务创建容器。
1
2
3
4
5
6
7
8
9
10
11
12<!-- 配置 spring 提供的监听器,用于启动服务时加载容器 。
该间监听器只能加载 WEB-INF 目录中名称为 applicationContext.xml 的配置文件 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 手动指定 spring 配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> -
依赖注入
1
2
3
4
5
6
7
8
9
10
11
12
public class AccountController {
private AccountService accountService;
public String findAll(){
System.out.println("表现层:查询所有信息");
accountService.findAll();
return "list";
}
}
Spring整合MyBatis
搭建测试MyBatis环境
-
配置文件
SqlMapConfig.xml
。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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:///ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 使用的是注解 -->
<mappers>
<!-- <mapper class="top.tyzhang.dao.IAccountDao"/> -->
<!-- 该包下所有的dao接口都可以使用 -->
<package name="top.tyzhang.dao"/>
</mappers>
</configuration> -
Dao中编写SQL语句。
1
2
3
4
5
6public interface IAccountDao {
public List<Account> findAll();
public void saveAccount(Account account);
} -
测试。
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
36public class MyBaTest {
public void run() throws IOException {
//加载mybatis配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
SqlSession session = factory.openSession();
//获取代理对象
IAccountDao accountDao=session.getMapper(IAccountDao.class);
System.out.println(accountDao.findAll().size());
session.close();
in.close();
}
public void run2() throws IOException {
Account account = new Account();
account.setName("lala");
account.setMoney(14.0);
//加载mybatis配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
SqlSession session = factory.openSession();
//获取代理对象
IAccountDao accountDao=session.getMapper(IAccountDao.class);
accountDao.saveAccount(account);
//提交事务
session.commit();
System.out.println(accountDao.findAll().size());
session.close();
in.close();
}
}
整合
-
把
SqlMapConfig.xml
配置文件中的内容配置到applicationContext.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
<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,Controller交给SpringMVC处理 -->
<context:component-scan base-package="top.tyzhang">
<!-- 配置哪些不扫描 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- spring整合mybatis框架,就是把mybatis的配置文件交给spring-->
<!-- 配置连接池-->
<bean id="dSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///ssm"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置工厂对象SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dSource"/>
</bean>
<!-- 配置接口所在的包-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.tyzhang.dao"/>
</bean>
</beans> -
在IAccountDao接口中添加
@Repository
注解。1
2
3
4
5
6
7
public interface IAccountDao {
public List<Account> findAll();
public void saveAccount(Account account);
} -
在Service中注入Dao对象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class AccountService implements IAccountService {
private IAccountDao iAccountDao;
public List<Account> findAll() {
System.out.println("业务层:输出所有");
return iAccountDao.findAll();
}
public void saveAccount(Account account) {
System.out.println("业务层:保存了");
iAccountDao.saveAccount(account);
}
} -
Controller中注入Service。
1
2
3
4
5
6
7
8
9
10
11
12
13
public class AccountController {
private AccountService accountService;
public String findAll(Model model){
System.out.println("表现层:查询所有信息");
List<Account> accountList = accountService.findAll();
model.addAttribute("list",accountList);
return "list";
}
} -
截止到现在,增删查还没有提交事务的操作,需要在applicationContext.xml进行声明式事务管理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<!-- 配置声明式事务管理-->
<!--配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dSource"/>
</bean>
<!-- 配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP增强-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* top.tyzhang.service.impl.*Service.*(..))"/>
</aop:config> -
控制器
1
2
3
4
5
6
7
public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("表现层:保存");
accountService.saveAccount(account);
response.sendRedirect(request.getContextPath()+"/user/findAll");
return;
}