前言:
Spring、SpringMvc、MyBatis各框架都学了一段时间了,现在综合起来做一个小案例。案例内容:查询所有账户列表以及通过表单保存账户。通过此案例,进一步理解各框架的应用以及熟悉整合过程中的一些列配置。
步骤:
-
搭建整合环境
-
整合说明:
-
此案例选择XML + 注解的方式
-
整合的思路:
-
1. 先搭建整合的环境
-
2. 再使用Spring整合SpringMVC框架
-
3. 最后使用Spring整合MyBatis框架
一、创建account数据库表
-
create
table
account(
-
id
int primary
key auto_increment,
-
name
varchar(
100),
-
money
double(
7,
2),
-
);
二、创建一个maven项目
此处可以参考之前博客:springMvc项目创建
导入maven坐标及相关的依赖
-
<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>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>
三、创建相应的实体类、表现层、服务层、持久层
实体类 Account
-
package com.xiaojie.pojo;
-
-
import java.io.Serializable;
-
-
/**
-
* @program: springmvc01_start
-
* @description: 账户实体类
-
* @author: Mr.Li
-
* @create: 2020-04-21 22:52
-
**/
-
public
class Account implements Serializable {
-
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;
-
}
-
-
@Override
-
public String toString() {
-
return
"Account{" +
-
"id=" + id +
-
", name='" + name +
'\'' +
-
", money=" + money +
-
'}';
-
}
-
}
表现层:AccountController
-
package com.xiaojie.controller;
-
-
import com.xiaojie.pojo.Account;
-
import com.xiaojie.service.AccountService;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Controller;
-
import org.springframework.ui.Model;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import java.io.IOException;
-
import java.util.List;
-
-
/**
-
* @program: springmvc01_start
-
* @description: 控制层
-
* @author: Mr.Li
-
* @create: 2020-04-21 23:28
-
**/
-
@Controller
-
@RequestMapping(
"/account")
-
public
class AccountController {
-
@Autowired
-
AccountService accountService;
-
/**
-
* 查询所有的数据
-
* @return
-
*/
-
@RequestMapping(
"/findAll")
-
public String findAll(Model model) {
-
System.out.println(
"表现层:查询所有账户...");
-
List<Account> accounts = accountService.findAll();
-
-
accounts.stream().forEach(account -> {
-
System.out.println(account);
-
});
-
-
model.addAttribute(
"accounts",accounts);
-
return
"list";
-
}
-
-
@RequestMapping(
"/saveAccount")
-
public void saveAccount(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException {
-
System.out.println(
"表现层:查询所有账户...");
-
accountService.saveAccount(account);
-
response.sendRedirect(request.getContextPath() +
"/account/findAll");
-
}
-
}
持久层:AccountDao
基于注解方式实现
-
package com.xiaojie.dao;
-
-
import com.xiaojie.pojo.Account;
-
import org.apache.ibatis.annotations.Insert;
-
import org.apache.ibatis.annotations.Select;
-
import org.springframework.stereotype.Repository;
-
-
import java.util.List;
-
-
@Repository
-
public
interface AccountDao {
-
/**
-
* 保存账户
-
* @param account
-
*/
-
@Insert(
"insert into account (name,money) values(#{name},#{money})")
-
public void saveAccount(Account account);
-
-
/**
-
* 获取账户列表
-
* @return
-
*/
-
@Select(
"select * from account")
-
public List<Account> findAll();
-
}
服务层:接口AccountService
实现类 AccountServiceImpl
-
package com.xiaojie.service;
-
-
import com.xiaojie.pojo.Account;
-
-
import java.util.List;
-
-
public
interface AccountService {
-
/**
-
* 保存账户
-
* @param account
-
*/
-
public void saveAccount(Account account);
-
-
/**
-
* 获取账户列表
-
* @return
-
*/
-
public List<Account> findAll();
-
}
-
package com.xiaojie.service.impl;
-
-
import com.xiaojie.dao.AccountDao;
-
import com.xiaojie.pojo.Account;
-
import com.xiaojie.service.AccountService;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Service;
-
-
import java.util.List;
-
-
/**
-
* @program: springmvc01_start
-
* @description: 账户服务层
-
* @author: Mr.Li
-
* @create: 2020-04-21 22:57
-
**/
-
@Service(
"accountService")
-
public
class AccountServiceImpl implements AccountService {
-
@Autowired
-
AccountDao accountDao;
-
-
@Override
-
public void saveAccount(Account account) {
-
System.out.println(
"业务层:查询所有账户...");
-
accountDao.saveAccount(account);
-
}
-
-
@Override
-
public List<Account> findAll() {
-
System.out.println(
"业务层:查询所有账户...");
-
return accountDao.findAll();
-
}
-
}
四、spring整合SpringMVC、MyBatis框架
1、在resources文件中创建applicationContext.xml文件
在里面配置:注解扫描、连接池、SqlSession工厂、spring声明事务管理、aop
-
<?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">
-
-
<!-- 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,
-
因为web层让SpringMVC框架去管理 -->
-
<context:component-scan base-package=
"com.xiaojie">
-
<!--配置要忽略的注解-->
-
<context:exclude-filter
type=
"annotation" expression=
"org.springframework.stereotype.Controller"></context:exclude-filter>
-
</context:component-scan>
-
-
<!--配置c3p0连接池对象-->
-
<bean id=
"dateSource" class=
"org.springframework.jdbc.datasource.DriverManagerDataSource">
-
<property name=
"driverClassName" value=
"com.mysql.jdbc.Driver" />
-
<property name=
"url" value=
"jdbc:mysql:///springtest" />
-
<property name=
"username" value=
"root" />
-
<property name=
"password" value=
"123456" />
-
</bean>
-
-
<!--配置SqlSession的工厂-->
-
<bean id=
"SqlSession" class=
"org.mybatis.spring.SqlSessionFactoryBean">
-
<property name=
"dataSource" ref=
"dateSource"></property>
-
</bean>
-
-
<!--配置扫描的dao包-->
-
<bean id=
"mapperScanner" class=
"org.mybatis.spring.mapper.MapperScannerConfigurer">
-
<property name=
"basePackage" value=
"com.xiaojie.dao"></property>
-
</bean>
-
-
<!--配置spring声明式事务管理-->
-
<!--配置事务管理器-->
-
<bean id=
"transactionManager" class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager">
-
<property name=
"dataSource" ref=
"dateSource"></property>
-
</bean>
-
-
<!--配置事务通知-->
-
<tx:advice id=
"txAdvice" transaction-manager=
"transactionManager">
-
<tx:attributes>
-
<tx:method name=
"find*"
read-only=
"true"/>
-
<tx:method name=
"*" isolation=
"DEFAULT"></tx:method>
-
</tx:attributes>
-
</tx:advice>
-
-
<!--配置aop增强-->
-
<aop:config>
-
<aop:advisor advice-ref=
"txAdvice" pointcut=
"execution(* com.xiaojie.service.impl.*ServiceImpl.*(..))"></aop:advisor>
-
</aop:config>
-
</beans>
2、在resources文件中创建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.xiaojie">
-
<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>
3、web.xml中配置:
因为需要在项目启动的时候,就去加载applicationContext.xml、springmvc.xml的配置文件(上面配置的),所以需要在web.xml中配置ContextLoaderListener监听器、前端控制器、中文乱码过滤器等
-
<!
DOCTYPE web-app
PUBLIC
-
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
-
"http://java.sun.com/dtd/web-app_2_3.dtd" >
-
-
<web-app>
-
<display-name>
Archetype
Created
Web
Application</display-name>
-
-
<!--配置前端控制器:服务器启动必须加载,需要加载springmvc.xml配置文件-->
-
<servlet>
-
<servlet-name>dispatcherServlet</servlet-name>
-
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-
<!--配置初始化参数-->
-
<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>
-
-
<!-- 配置Spring的监听器 -->
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
-
</listener>
-
<!-- 配置加载类路径的配置文件 -->
-
<context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:applicationContext.xml</param-value>
-
</context-param>
-
-
<!--配置解决中文乱码的过滤器-->
-
<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>
-
五、编写jsp页面进行测试
webapp目录结构
1、index.jsp
-
<%--
-
Created
by
IntelliJ
IDEA.
-
User:
ASUS
-
Date:
2020/
4/
21
-
Time:
23:28
-
To
change
this
template
use
File |
Settings |
File
Templates.
-
--%>
-
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
<html>
-
<head>
-
<title>Title
</title>
-
</head>
-
<body>
-
<a href="account/findAll">testFindAll
</a>
-
-
<h3>测试保存a
</h3>
-
<form action="account/saveAccount" method="post">
-
姓名:
<input type="text" name="name"/>
<br>
-
金额:
<input type="text" name="money"/>
<br>
-
<input type="submit" value="保存"/>
<br>
-
</form>
-
</body>
-
</html>
lsit.jsp
-
<%--
-
Created
by
IntelliJ
IDEA.
-
User:
ASUS
-
Date:
2020/
4/
21
-
Time:
23:29
-
To
change
this
template
use
File |
Settings |
File
Templates.
-
--%>
-
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
-
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
-
<html>
-
<head>
-
<title>Title
</title>
-
</head>
-
<body>
-
查询成功!
-
<table border="1" width="300px">
-
<tr>
-
<th>编号
</th>
-
<th>账户名称
</th>
-
<th>金额
</th>
-
</tr>
-
<c:forEach items="${accounts}" var="account">
-
<tr>
-
<td>${account.id}
</td>
-
<td>${account.name}
</td>
-
<td>${account.money}
</td>
-
</tr>
-
</c:forEach>
-
</table>
-
</body>
-
</html>
六、测试运行结果:
1、配置tomcat服务器,然后添加当前项目的war包
2、选择后点击应用,并启动服务
浏览器会看到首页内容
点击testFindAll后的页面
通过表单添加:
点击保存自动跳转查询页:
终于更到最后了。虽然案例简单,但还是记录一下自己的学习的一个过程吧,毕竟整合的过程还是很容易出错的。记录只是便于以后的一个回忆吧,希望大家养成一个记录的习惯,至少给正在变强路上的你留些沙雕记忆。最后祝愿大家生活愉快……
转载:https://blog.csdn.net/qq_41216743/article/details/105681668