SpringMVC学习
Spring整合MyBatis
为了更好的学习SpringMVC和MyBatis进行整合开发
整合的目标: 控制层采用SpringMVC 、持久层使用MyBatis实现
开发步骤:
a.需求分析
b.梳理整合思路
c.搭建开发环境
d.根据需求完成dao层、Service层、Controller层、测试界面的开发
a.需求分析
连接数据库,实现商品列表的查询
b.梳理整合思路
- 第一步:整合DAO层
MyBatis和Spring整合:
通过spring管理mapper接口
使用mapper的自动扫描mapper接口在spring的注册
- 第二步:整合service层
通过spring管理service接口:
使用配置方式将service接口配置在spring配置文件中
实现事务管理
- 第三步:整合springmvc
由于springmvc是是属于spring的模块 所以不需要整合 直接使用
c.搭建开发环境
导入相关的jar包:
包括spring 和springmvc相关的jar包、MyBaits相关的jar包
Mybatis-spring整合的jar包、数据库的驱动jar包、第三方数据库连接池驱动的jar包
<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>com.guigu.springmvc</groupId>
<artifactId>SpringMVC_MyBatis</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVC_MyBatis Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>5.0.8.RELEASE</spring-version>
<mybatis-version>3.4.6</mybatis-version>
<log4j-version>1.2.17</log4j-version>
<logging-version>1.2</logging-version>
<mysql-version>5.1.6</mysql-version>
<druid-version>1.1.10</druid-version>
<jstl-version>1.2</jstl-version>
<mybatis-spring-version>1.3.2</mybatis-spring-version>
</properties>
<dependencies>
<!-- spring相关的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- springMVC相关的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- MyBatis相关的jar包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<!-- spring整合mybatis必备的jar包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring-version}</version>
</dependency>
<!-- 日志相关的jar包 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${logging-version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- mysql驱动的jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
</dependency>
<!-- 第三方数据库连接池相关jar包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
<!-- jstl相关的jar -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC_MyBatis</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
d.根据需求完成dao、Service、Controller、测试界面的开发
DAO层
目标: 使用Spring管理SqlSessionFactory 、Mapper
完成以下步骤:
a.导入db.properties文件(数据库连接配置文件)
b.导入log4j.properties日志文件
c.创建核心配置文件sqlMapConfig.xml,配置相关内容
d.使用逆向工程生成pojo类、mapper.java接口、mapper.xml配置文件
e.创建applicationContext-dao.xml配置文件,使用spring管理dao层
- a.导入db.properties文件(数据库连接配置文件)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc
jdbc.username=root
jdbc.password=root
- b.导入log4j.properties日志文件
- c.创建核心配置文件sqlMapConfig.xml,配置相关内容
- d.使用逆向工程生成pojo类、mapper.java接口、mapper.xml配置文件
- e.创建applicationContext-dao.xml配置文件,使用spring管理dao层
applicationContext-dao.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- a.加载指定的配置文件:实现数据库连接 -->
<context:property-placeholder location="classpath:config/db.properties" />
<!-- 使用druid数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--
b.配置sqlSessionFactory :
SqlSessionFactoryBean 需要配置两个属性:
配置数据库连接池:dataSource
配置加载mybatis的文件:configLocation
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置加载mybatis的文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
</bean>
<!--
c.定义mapper扫描器 :MapperScannerConfigurer
需要配置两个属性:
配置扫描包的路径信息:basePackage
配置sqlSessionFactory:sqlSessionFactoryBeanName
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描包的路径信息 -->
<property name="basePackage" value="com.guigu.ssm.mapper"></property>
<!-- 配置sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
Services层
目标:
Service交给spring管理
spring对service进行事务管理
开发步骤:
a.配置applicationContext-service.xml文件
b.配置applicationContext-transaction.xml文件
c.编写Service接口和相应的ServiceImpl实现类
- a.配置applicationContext-service.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置相关的service,也可以不需要配置,直接通过注解的形式进行管理 -->
</beans>
- b.配置applicationContext-transaction.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
事务管理配置:
对mybatis操作数据库的事务进行控制,spring使用jdbc的事务控制类
org.springframework.jdbc.datasource.DataSourceTransactionManager
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
a.可以使用默认的配置
<tx:annotation-driven/>
-->
<!--
b.手动配置
-->
<!-- 定义通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为定义 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 定义切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution (* com.guigu.ssm.service.impl.*.*(..))"/>
</aop:config>
</beans>
- c.编写Service接口和相应的ServiceImpl实现类
此处为了测试商品管理相关的内容,以商品管理相关进行测试
ItemsService.java:
public interface ItemsService {
// 查找所有项目信息
public List<Items> findItemsList()throws Exception;
// 根据id查找详细信息
public Items findItemsById(Integer id)throws Exception;
// 根据主键id修改信息
public void updateItemsById(Integer id,Items item)throws Exception;
}
ItemsServiceImpl.java:
@Service("itemsService")
public class ItemsServiceImpl implements ItemsService{
@Autowired
private ItemsMapper itemsMapper;
@Override
public List<Items> findItemsList() throws Exception {
List<Items> list = itemsMapper.selectByExampleWithBLOBs(null);
// 打印数据进行验证
System.out.println(list);
return list;
}
@Override
public Items findItemsById(Integer id) throws Exception {
return itemsMapper.selectByPrimaryKey(id);
}
@Override
public void updateItemsById(Integer id, Items item) throws Exception {
// 一般是通过主键id进行修改,此处明确指定主键id
item.setId(id);
// 执行修改操作
itemsMapper.updateByPrimaryKeyWithBLOBs(item);
}
}
Action(Controller)
目的:
完成controller层的开发
完成相关配置文件的导入并编写测试代码
开发步骤:
a.创建springmvc.xml进行配置(完成扫描包路径配置和视图解析器的配置)
b.配置web.xml文件(加载spring容器、配置springMVC的前端控制器)
c.创建xxxController.java完成控制层代码编写
d.编写测试页面进行相关测试
- a.创建springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 定义扫描包的路径 -->
<context:component-scan base-package="com.guigu.ssm.controller,com.guigu.ssm.service.impl"></context:component-scan>
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置视图的前缀 -->
<property name="prefix" value="WEB-INF/jsp/"></property>
<!-- 配置视图的后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- b.配置web.xml文件(加载spring容器、配置springMVC的前端控制器)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC的前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
- c.创建xxxController.java完成控制层代码编写
@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception {
List<Items> list = itemsService.findItemsList();
ModelAndView modelAndView = new ModelAndView();
// 转发数据到指定页面
modelAndView.addObject("itemList", list);
// 在配置文件中指定了前缀和后缀,此处可以省略部分内容等价于WEB-INF/jsp/xxx/xxx.jsp
modelAndView.setViewName("items/itemList");
return modelAndView;
}
}
- d.编写测试页面进行相关测试
itemList.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查阅所有商品信息</title>
</head>
<body>
<table border="1">
<tr>商品信息一览</tr>
<tr>
<th>商品id</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品描述</th>
<th>生产日期</th>
<th>操作</th>
</tr>
<c:forEach var="item" items="${itemList }">
<tr>
<td>${item.id }</td>
<td>${item.name }</td>
<td>${item.price }</td>
<td>${item.detail }</td>
<td>${item.createtime }</td>
<td><a href="${pageContext.request.contextPath }/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
测试结果:
访问链接:http://localhost:8080/SpringMVC_MyBatis/queryItems.action,测试结果如下所示