1. 前言
从《Spring Boot 快速入门系列》数据操作篇前两篇中(Spring Boot 快速入门系列(II)—— 数据操作篇之 Spring Data JPA,Spring Boot 快速入门系列(III)—— 数据操作篇之 JdbcTemplate),我们已经学习和了解如何通过Spring Data JPA 和 Spring JdbcTemplate 完成基础的数据库(CRUD)持久化操作,今天我们就来演示第三种数据库持久化操作的方式,即通过 SpringBoot MyBatis 完成基础的数据库 CRUD 操作。
2. MyBatis 使用演示
下面通过一个简单的图书管理页面演示 Spring Boot 下 MyBatis 操作数据库的基本方法。
1)紧接着上一篇(数据操作篇之 JdbcTemplate)项目工程继续,通过 Maven 项目的 pom.xml 文件引入 SpringBoot MyBatis 数据持久化操作依赖的Jar 包,具体的引入方式如下:
<!-- Spring JdbcTemplate -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2)下面我们开始使用 SpringBoot MyBatis 来实现数据库的 CRUD 持久化操作,视图层采用 Freemarker 模板实现。
MyBatis 持久化相关代码实现如下:
Mapper 层
在 mapper 包下创建接口 IBookMapper,采用注解方式代码如下:
package cn.giserway.helloworld.mapper;
import cn.giserway.helloworld.domain.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
/**
* @program: helloworld
*
* @author: giserway
*
**/
@Mapper
public interface IBookMapper {
/**
* 查询所有图书
* @return
*/
@Select("select id, book_name as bookName, money from t_book")
List<Book> findAll();
/**
* 通过id查询图书
* @param id
* @return
*/
@Select("select id, book_name as bookName, money from t_book where id = #{id}")
Book findById(@Param("id") int id);
/**
* 新增图书
* @param bookName
* @param money
* @return
*/
@Insert("insert into t_book(book_name, money) values(#{bookName}, #{money})")
int add(@Param("bookName") String bookName, @Param("money") double money);
/**
* 更新图书
* @param bookName
* @param money
* @param id
* @return
*/
@Update("update t_book set book_name = #{bookName}, money = #{money} where id = #{id}")
int update(@Param("bookName") String bookName, @Param("money") double money, @Param("id") int id);
/**
* 通过id删除图书
* @param id
* @return
*/
@Delete("delete from t_book where id = #{id}")
int deleteById(int id);
}
service 层
在 service 包下新建接口 IBookService2,代码如下:
package cn.giserway.helloworld.service;
import cn.giserway.helloworld.domain.Book;
import java.util.List;
/**
* @program: helloworld
*
* @author: giserway
*
**/
public interface IBookService2 {
/**
* 查询所有图书
*/
List<Book> findAll();
/**
* 通过id查询图书
* @param id
* @return
*/
Book findById(Integer id);
/**
* 新增图书
* @param bookName
* @param money
* @return
*/
int add(String bookName, double money);
/**
* 更新图书
* @param bookName
* @param money
* @param id
* @return
*/
int update(String bookName, double money, Integer id);
/**
* 通过id删除图书
* @param id
* @return
*/
int deleteById(Integer id);
}
在 serviceImpl 包下新增 BookServiceImpl2 实现 IBookService2 接口,代码如下:
package cn.giserway.helloworld.service.serviceImpl;
import cn.giserway.helloworld.domain.Book;
import cn.giserway.helloworld.mapper.IBookMapper;
import cn.giserway.helloworld.service.IBookService2;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @program: helloworld
*
* @author: giserway
*
**/
@Service
public class BookServiceImpl2 implements IBookService2 {
@Resource
IBookMapper bookMapper;
@Override
public List<Book> findAll() {
return bookMapper.findAll();
}
@Override
public Book findById(Integer id) {
return bookMapper.findById(id);
}
@Override
public int add(String bookName, double money) {
return bookMapper.add(bookName,money);
}
@Override
public int update(String bookName, double money, Integer id) {
return bookMapper.update(bookName,money,id);
}
@Override
public int deleteById(Integer id) {
return bookMapper.deleteById(id);
}
}
controller 层
在 controller 包下创建 BookController2 类,代码如下:
package cn.giserway.helloworld.controller;
import cn.giserway.helloworld.domain.Book;
import cn.giserway.helloworld.service.IBookService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* @program: helloworld
*
* @author: giserway
*
**/
@Controller
@RequestMapping("/bookManage")
public class BookController2 {
@Autowired
IBookService2 bookService;
/**
* 查询所有图书
* @return
*/
@RequestMapping(value="/list")
public ModelAndView list(){
ModelAndView mav = new ModelAndView();
mav.addObject("bookList", bookService.findAll());
mav.setViewName("bookList2");
return mav;
}
/**
* 添加图书
* @param book
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
public String add(Book book){
bookService.add(book.getBookName(),book.getMoney());
return "forward:/bookManage/list";
}
/**
* 跳转修改页面
* @param id
* @return
*/
@GetMapping(value="/preUpdate/{id}")
public ModelAndView preUpdate(@PathVariable("id") Integer id){
ModelAndView mav = new ModelAndView();
mav.addObject("book", bookService.findById(id));
mav.setViewName("bookUpdate2");
return mav;
}
/**
* 更新图书
* @param book
* @return
*/
@PostMapping(value="/update")
public String update(Book book){
bookService.update(book.getBookName(),book.getMoney(),book.getId());
return "forward:/bookManage/list";
}
/**
* 删除图书
* @param id
* @return
*/
@RequestMapping(value="/delete",method= RequestMethod.GET)
public String delete(Integer id){
bookService.deleteById(id);
return "forward:/bookManage/list";
}
}
3)在 templates (模板文件存放位置)文件夹(src/main/resources/templates)下新建一个 bookList2.ftl(图书列表)、bookUpdate2.ftl (图书更新),在 static (静态页面存放位置)文件夹下新建 bookAdd2.html(图书添加)文件。
文件结构如下:
页面代码如下所示:
bookList2.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<a href="/bookAdd2.html">添加图书</a>
<table>
<tr>
<th>编号</th>
<th>图书名称</th>
<th>图书价格</th>
<th>操作</th>
</tr>
<#list bookList as book>
<tr>
<td>${book.id}</td>
<td>${book.bookName}</td>
<td>${book.money}</td>
<td>
<a href="/bookManage/preUpdate/${book.id}">修改</a>
<a href="/bookManage/delete?id=${book.id}">删除</a>
</td>
</tr>
</#list>
</table>
</body>
</html>
bookUpdate2.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书更新页面</title>
</head>
<body>
<form action="/bookManage/update" method="post">
<input type="hidden" name="id" value="${book.id}"/>
图书名称:<input type="text" name="bookName" value="${book.bookName}"/><br/>
图书价格:<input type="text" name="money" value="${book.money}"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
bookAdd2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书添加页面</title>
</head>
<body>
<form action="bookManage/add" method="post">
图书名称:<input type="text" name="bookName"/><br/>
图书价格:<input type="text" name="money"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
4)重新启动 Spring Boot 项目,MyBatis 持久化演示动图如下:
浏览器输入:http://localhost:9999/bookManage/list
3. 小结
今天我们通过 SpringBoot MyBatis 学会了简单的数据库的持久化操作。开发人员通过项目的 pom.xml 文件添加相关依赖的Jar,新增 Mapper 接口并通过注解实现如上演示,就是这么简单!还不快试一试哈……
下一篇文章我们将会演示 Spring Boot 快速入门系列(V)—— 事务管理篇之 @Transactional。
# 精彩推荐 #
Spring Boot 快速入门系列(先导篇) —— 从 Hello World 开始
Spring Boot 快速入门系列(I) —— 属性配置篇
Spring Boot 快速入门系列(II)—— 数据操作篇之 Spring Data JPA
Spring Boot 快速入门系列(III)—— 数据操作篇之 JdbcTemplate