springboot学习(6)springboot整合Mybatis例子

上一篇介绍了使用spring-data-jpa来实现数据的持久化及展示,现在使用比较流行的Mybatis来整合springboot,这里通过一个完整的例子来展示。实现数据的录入,展示以及按条件查询,同样,在上一篇的基础上进行,springboot基础的配置搭建不做介绍。


首先引入Mybatis的依赖包

[html] view plain copy
  1. <dependency>  
  2.             <groupId>org.mybatis.spring.boot</groupId>  
  3.             <artifactId>mybatis-spring-boot-starter</artifactId>  
  4.             <version>1.0.0</version>  
  5.         </dependency>  

接着在application.properties中(多用更简洁的yml,大家随意)配置mapper及映射路径等信息

[html] view plain copy
  1. mybatis.typeAliasesPackage=org.amuxia.entity  
  2. mybatis.mapperLocations=classpath:mapper/*.xml  

配置之后定义实体类,基本前几篇的无异,把为spring-data-jpa写的注解信息去掉即可,这里不做展示。

接下来定义接口,代码如下:

[java] view plain copy
  1. public interface ItemsDAO{  
  2.   
  3.     public List<Items> findList();  
  4.     public void save(Items items);  
  5.     public List<Items> findItemByName(@Param("name") String name);  
  6. }  

Mybatis是基于接口编程的,我们写好接口,对应编写映射文件中的sql语句即可,非常灵活。我们在resources文件夹下新建一个mapper文件,存放XML映射文件,这里做简单的例子学习使用,mapper文件不按功能具体细分,实际应该把映射文件归类的,映射文件代码如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
  3. <mapper namespace="org.amuxia.dao.ItemsDAO">  
  4.     <resultMap id="BaseResultMap" type="org.amuxia.entity.Items">  
  5.         <result column="id" property="id" />  
  6.         <result column="title" property="title" />  
  7.         <result column="name" property="name" />  
  8.         <result column="detail" property="detail" />  
  9.     </resultMap>  
  10.   
  11.     <parameterMap id="Items" type="org.amuxia.entity.Items"/>  
  12.   
  13.     <sql id="Base_Column_List">  
  14.         id, title,name,detail  
  15.     </sql>  
  16.   
  17.     <select id="findList" resultMap="BaseResultMap">  
  18.         select  
  19.         <include refid="Base_Column_List" />  
  20.         from items  
  21.     </select>  
  22.   
  23.     <select id="findItemByName" resultMap="BaseResultMap" parameterType="java.lang.String">  
  24.         select  
  25.         <include refid="Base_Column_List" />  
  26.         from items  
  27.         where name = #{name}  
  28.     </select>  
  29.       
  30.     <insert id="save" parameterType="Items">  
  31.         insert into items   
  32.         (title,name,detail)   
  33.         values   
  34.         (#{title},#{name},#{detail})  
  35.     </insert>  
  36.       
  37. </mapper>  

控制器类调用接口,即可对数据进行操作,但是我们一般很少直接多DAO类进行直接调用,通常包一层Service类,代码如下:

[java] view plain copy
  1. package org.amuxia.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.amuxia.dao.ItemsDAO;  
  6. import org.amuxia.entity.Items;  
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.stereotype.Service;  
  9.   
  10. @Service   
  11. public class ItemsService implements ItemsDAO{  
  12.   
  13.     @Autowired  
  14.     private ItemsDAO dao;  
  15.   
  16.     @Override  
  17.     public List<Items> findList() {  
  18.         // TODO Auto-generated method stub  
  19.         return dao.findList();  
  20.     }  
  21.   
  22.     @Override  
  23.     public void save(Items items) {  
  24.         // TODO Auto-generated method stub  
  25.         dao.save(items);  
  26.     }  
  27.   
  28.     @Override  
  29.     public List<Items> findItemByName(String name) {  
  30.         // TODO Auto-generated method stub  
  31.         return dao.findItemByName(name);  
  32.     }  
  33. }  

接着定义Controller,实现具体的业务功能:

[java] view plain copy
  1. package org.amuxia.controller;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.amuxia.entity.Items;  
  6. import org.amuxia.service.ItemsService;  
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.context.annotation.ComponentScan;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.bind.annotation.RequestParam;  
  11. import org.springframework.web.bind.annotation.ResponseBody;  
  12. import org.springframework.web.bind.annotation.RestController;  
  13. import org.springframework.web.servlet.ModelAndView;  
  14.   
  15. @ComponentScan  
  16. @RestController  
  17. @RequestMapping("/items1")  
  18. public class ItemsController1 {  
  19.   
  20.     @Autowired  
  21.     private ItemsService service;  
  22.       
  23.     /** 
  24.      * @return 
  25.      * 查询全部信息 
  26.      */  
  27.     @RequestMapping("/list")  
  28.     public ModelAndView  itemsList() {  
  29.         List<Items> list = service.findList();  
  30.         ModelAndView mav = new ModelAndView("items");  
  31.         mav.addObject("list", list);  
  32.         return mav;  
  33.     }  
  34.       
  35.     @RequestMapping("/findItemByName")  
  36.     public ModelAndView  item(@RequestParam(value = "name", required = true) String name) {  
  37.         List<Items> list = service.findItemByName(name);  
  38.         ModelAndView mav = new ModelAndView("item");  
  39.         mav.addObject("item", list);  
  40.         return mav;  
  41.     }  
  42.       
  43.     /** 
  44.      * @return 
  45.      * 跳转到新增接界面 
  46.      */  
  47.     @RequestMapping("/toAdd")  
  48.     public ModelAndView  toAdd() {  
  49.         ModelAndView mav = new ModelAndView("add");  
  50.         return mav;  
  51.     }  
  52.       
  53.   
  54.     /** 
  55.      * 新增数据 
  56.      * @param items 
  57.      * @return 
  58.      */  
  59.     @RequestMapping("/add")  
  60.     public @ResponseBody boolean  addItems(Items items) {  
  61.         try {  
  62.             service.save(items);  
  63.             return true;  
  64.         }catch (Exception e) {  
  65.             // TODO: handle exception  
  66.             e.printStackTrace();  
  67.             return false;  
  68.         }  
  69.     }  
  70. }  


展示页面我们使用thymeleaf模版,新增页面不动,按之前文章中介绍的即可,列表展示(items)代码如下:

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html xmlns:th="http://www.thymeleaf.org">  
  3. <head>  
  4. <meta charset="utf-8" />  
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  6. <title>springboot学习</title>  
  7. </head>  
  8.   
  9. <body>  
  10.     <div>  
  11.         <form name="findByName" method="post" action="findItemByName">  
  12.             <input type="text" name="name" />  
  13.             <input type="submit" value="查询" />  
  14.         </form>  
  15.     </div>  
  16.     <div th:each="item : ${list}">  
  17.         <h1 th:text="${{item.title}}"></h1>  
  18.         <p>  
  19.             <a th:text="${{item.name}}"></a>  
  20.         </p>  
  21.         <p>  
  22.             <a th:text="${{item.detail}}"></a>  
  23.         </p>  
  24.     </div>  
  25. </body>  
  26. </html>  

效果如下:

springboot学习(6)springboot整合Mybatis例子


在输入框中输入阿木侠,搜索他的所有文章信息,效果如下:

springboot学习(6)springboot整合Mybatis例子

我们看到效果已经实现,展示条件搜索的页面代码如下:

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html xmlns:th="http://www.thymeleaf.org">  
  3. <head>  
  4. <meta charset="utf-8" />  
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  6. <title>springboot学习</title>  
  7. </head>  
  8.   
  9. <body>  
  10.     <div th:each="item : ${item}">  
  11.         <h1 th:text="${{item.title}}"></h1>  
  12.         <p>  
  13.             <a th:text="${{item.name}}"></a>  
  14.         </p>  
  15.         <p>  
  16.             <a th:text="${{item.detail}}"></a>  
  17.         </p>  
  18.     </div>  
  19. </body>  
  20. </html>  

当然,我们一般搜索展示页面和列表展示都一个页面,这里为了直观点理解,呵呵。

这样,一个简单的springboot整合Mybatis的demo就完成了,配置大大简化了,确实很方便,挺好的,也没有什么不好理解的地方,先搭起一个简单的demo,然后一步一步去完善,往复杂去做,边做边学,也是一种挺好的学习方法。