1.整合思路
- 第一步:整合dao层
- mybatis和spring整合,通过spring管理mapper接口
- 使用mapper的扫描自动扫描mapper接口在spring中进行注册
- 第二步:整合service层
- 通过spring管理service接口
- 使用配置方式将service接口配置在spring配置文件中
- 实现事务管理
- 第三步:整合Spring MVC
- 由于Spring MVC是spring的模块,不需要整合
2.工程结构
添加依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>club.krislin</groupId>
<artifactId>SpringMVCAndMybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringMVCAndMybatis Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<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版本号 -->
<spring.version>5.2.6.RELEASE</spring.version>
<!-- mysql connector版本号-->
<mysql.version>5.1.47</mysql.version>
<!-- mybatis版本号 -->
<mybatis.version>3.5.1</mybatis.version>
<!-- mybatis-spring版本号 -->
<mybatis-spring.version>2.0.1</mybatis-spring.version>
</properties>
<dependencies>
<!-- spring框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!--日志依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.18</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.18</version>
</dependency>
<!-- JSP tag -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVCAndMybatis</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
项目目录
3.整合步骤
1.dao层
目录结构
数据库配置文件和日志配置文件
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
log4j.properties
### set log levels ###
log4j.rootLogger = INFO , C , D , E
### console ###
log4j.appender.C = org.apache.log4j.ConsoleAppender
log4j.appender.C.Target = System.out
log4j.appender.C.layout = org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern = [aopExample][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n
### log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ../logs/springmvc_hibernate_demo.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = [aopExample][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n
### exception ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = ../logs/aopExample_error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern =[aopExample][%p] [%-d{yyyy-MM-dd HH\:mm\:ss}] %C.%M(%L) | %m%n
mybatis配置文件
sqlMapConfig.xml
<?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>
<!--配置别名-->
<typeAliases>
<!--批量扫描别名-->
<package name="club.krislin.po"/>
</typeAliases>
<!-- 配置mapper
由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
必须遵循:mapper.xml和mapper.java文件同名且在一个目录
-->
</configuration>
spring配置文件
配置:
- 数据源
- SqlSessionFactory
- mapper扫描器
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: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">
<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据源dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="30"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- 从整合包里找,org.mybatis:mybatis-spring -->
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis的全局配置文件-->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
</bean>
<!--mapper扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开-->
<property name="basePackage" value="club.krislin.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
逆向工程生成po类及mapper(dao层)
手动定义商品查询mapper
针对综合查询mapper,一般情况会有关联查询,建议自定义mapper
-
ItemsMapperCustom.xml
<?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="club.krislin.mapper.ItemsMapperCustom"> <!-- 定义商品查询的sql片段,就是商品查询条件 --> <sql id="query_items_where"> <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 --> <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 --> <if test="itemsCustom!=null"> <if test="itemsCustom.name!=null and itemCustom.name!=''"> items.name LIKE '%${itemsCustom.name}%' </if> </if> </sql> <!-- 商品列表查询 --> <!-- parameterType传入包装对象(包装了查询条件) resultType建议使用扩展对象 --> <select id="findItemsList" parameterType="club.krislin.po.ItemsQueryVo" resultType="club.krislin.po.ItemsCustom"> SELECT items.* FROM items <where> <include refid="query_items_where"></include> </where> </select> </mapper>
-
ItemsMapperCustom.java
package club.krislin.mapper; import club.krislin.po.ItemsCustom; import club.krislin.po.ItemsQueryVo; import java.util.List; /** * @Package club.krislin.mapper * @ClassName ItemsMapperCustom * @Description TODO * @Date 19/12/25 10:07 * @Author LIM * @Version V1.0 */ public interface ItemsMapperCustom { /** * 商品列表查询 * @param itemsQueryVo * @return * @throws Exception */ List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception; }
-
po类
ItemsCustom
package club.krislin.po; /** * @Package club.krislin.po * @ClassName ItemsCustom * @Description TODO * @Date 19/12/25 10:03 * @Author LIM * @Version V1.0 */ public class ItemsCustom extends Items{ //添加商品信息的扩展属性 }
-
输入pojo的包装类
package club.krislin.po; /** * @Package club.krislin.po * @ClassName ItemsQueryVo * @Description TODO * @Date 19/12/25 9:58 * @Author LIM * @Version V1.0 */ public class ItemsQueryVo { /** * 商品信息 */ private Items items; /** * 为了系统 可扩展性,对原始生成的po进行扩展 */ private ItemsCustom itemsCustom; public Items getItems() { return items; } public void setItems(Items items) { this.items = items; } public ItemsCustom getItemsCustom() { return itemsCustom; } public void setItemsCustom(ItemsCustom itemsCustom) { this.itemsCustom = itemsCustom; } }
2.service层
目录结构
定义service接口
-
ItemsService
package club.krislin.service; import club.krislin.po.ItemsCustom; import club.krislin.po.ItemsQueryVo; import java.util.List; /** * @Package club.krislin.service * @InterfaceName ItemsService * @Description TODO * @Date 19/12/25 10:12 * @Author LIM * @Version V1.0 */ public interface ItemsService { /** * 商品查询列表 * @param itemsQueryVo * @return * @throws Exception */ List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; }
-
ItemsServiceImpl
public class ItemsServiceImpl implements ItemsService { @Autowired private ItemsMapperCustom itemsMapperCustom; public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { return itemsMapperCustom.findItemsList(itemsQueryVo); } }
在spring容器配置service
applicatrionContext-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--商品管理的service-->
<bean id="itemService" class="club.krislin.service.impl.ItemsServiceImpl"/>
</beans>
事务控制
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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 事务管理器
对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源
dataSource在applicationContext-dao.xml中配置了
-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知 -->
<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-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* club.krislin.service.impl.*.*(..))"/>
</aop:config>
</beans>
3.controller层
目录结构
springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.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">
<!-- 对于注解的Handler 可以单个配置
实际开发中加你使用组件扫描
-->
<!-- 可以扫描controller、service、...
这里让扫描controller,指定controller的包
-->
<context:component-scan base-package="club.krislin.controller"/>
<!-- 使用mvc:annotation-driven代替上面两个注解映射器和注解适配的配置
mvc:annotation-driven默认加载很多的参数绑定方法,
比如json转换解析器默认加载了,如果使用mvc:annotation-driven则不用配置上面的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器
解析jsp,默认使用jstl,classpath下要有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Spring And Mybatis</display-name>
<!--加载spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--编码过滤器-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
若不配置,默认加载WEB-INF/servlet名称-servlet(springmvc-servlet.xml)
-->
<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>
<!--
第一种:*.action,访问以.action三结尾,由DispatcherServlet进行解析
第二种:/,所有访问的地址由DispatcherServlet进行解析,对静态文件的解析需要配置不让DispatcherServlet进行解析,
使用此种方式和实现RESTful风格的url
第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址,
不能根据jsp页面找到handler,会报错
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
编写Controller(就是Hnadler)
- ItemsController
package club.krislin.controller;
import club.krislin.po.ItemsCustom;
import club.krislin.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
/**
* @Package club.krislin.controller
* @ClassName ItemsController
* @Description TODO
* @Date 19/12/25 10:40
* @Author LIM
* @Version V1.0
*/
@Controller
@RequestMapping("/items")
public class ItemsController {
@Autowired
private ItemsService itemsService;
/**
* 商品查询列表
* @return
* @throws Exception
*/
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
//调用service查询数据库,查找商品列表
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute方法,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList",itemsList);
//指定视图
//下边的路径,如果在视图解析器中配置jsp的路径前缀和后缀,修改为items/itemsList
//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
//下边的路径配置就可以不在程序中指定jsp路径的前缀和后缀
modelAndView.setViewName("/items/itemsList");
return modelAndView;
}
/**
* 商品信息修改页面显示
* @return
*/
@RequestMapping(value = "/editItems?id={id}",method={RequestMethod.POST, RequestMethod.GET})
public ModelAndView editItems(@PathVariable String id) throws Exception {
// 调用service根据商品id查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(Integer.parseInt(id));
// 放回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//将商品信息放在modelAndView中
modelAndView.addObject("itemsCustom",itemsCustom);
// 商品修改页面
modelAndView.setViewName("items/editItems");
return modelAndView;
}
/**
* 商品信息修改提交
* @param id
* @param itemsCustom
* @return
* @throws Exception
*/
@RequestMapping("/editItemsSubmit")
public ModelAndView editItemsSubmit(Integer id, ItemsCustom itemsCustom) throws Exception {
//调用service更新商品信息,页面需要将商品信息传到此方法
itemsService.updateItems(id,itemsCustom);
ModelAndView modelAndView = new ModelAndView();
// 返回一个成功的界面
modelAndView.setViewName("success");
return modelAndView;
}
}
编写jsp
- itemList.jsp
<%--
Created by IntelliJ IDEA.
User: LIM
Date: 19/12/25
Time: 10:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/items/queryItems.action" method="post">
查询条件:
<table width="100%" border="1">
<tr>
<td><input type="submit" value="查询"></td>
</tr>
</table>
商品列表:
<table width="100%" border="1">
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList}" var="item">
<tr>
<td>${item.name}</td>
<td>${item.price}</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail}</td>
<td><a href="${pageContext.request.contextPath}/items/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>