Springboot mybatis-plus配置及用法详解

本节内容扩展介绍下针对mybatis的增强工具mybatis-plus,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

二话不多说,我们先写编写个简单的例子,让大家先初步的了解下mybatis-plus。

1.mybatis-plus初步实例

(1)创建一个spring boot web工程(具体创建过程就不再演示了,还不会的同学去看看spring boot专题第一节内容)

(2)引入依赖

<!--web项目依赖-->
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <!--validation表单校验-->
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-validation</artifactId>
 </dependency>
 <!--mybatis-plus-->
 <dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.4.0</version>
 </dependency>
 <!--mysql驱动-->
 <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
 </dependency>
 <!--thymeleaf依赖包-->
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <!--lombok-->
 <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
 </dependency>

(3)配置文件application.yml

# DataSource Config
 spring:
  datasource:
   username: root
   password: tx@mysql@2020
   url: jdbc:mysql://188.131.233.55:3306/spring_boot_topic
   driver-class-name: com.mysql.cj.jdbc.Driver(4)

(4)实体类User

package com.kinglead.demo.domain;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
 ​
 @Data
 @TableName(value = "http://www.cppcns.com/ruanjian/java/t_user") //指明数据库表名
 public class User {
   private Long id;
   private String name;
   private Integer age;
   private String email;
 }

(5)创建Mapper接口

package com.kinglead.demo.mapper;
 ​
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.kinglead.demo.domain.User;
 ​
 //未来使用mybatis-plus的公共接口,必须继承BaseMapper
 public interface UserMapper extends BaseMapper<User> {
 }

(6)创建Service接口

UserService

package com.kinglead.demo.service;
 import com.kinglead.demo.domain.User;
 import java.util.List;
 public interface UserService {
   List<User> queryUserList();
 }

UserServiceImpl

package com.kinglead.demo.service.impl;
 ​
 import com.kinglead.demo.domain.User;
 import com.kinglead.demo.mapper.UserMapper;
 import com.kinglead.demo.service.UserService;
 import org.springframework.stereotype.Service;
 ​
 import javax.annotation.Resource;
 import java.util.List;
 ​
 @Service
 public class UserServiceImpl implements UserService {
   @Resource
   private UserMapper userMapper;
   @Override
   public List<User> queryUserList() {
     //使用mybatis-plus公共查询接口完成列表查询
     return userMapper.selectList(null);
   }
 }

(7)创建controller

package com.kinglead.demo.controller;
 ​
 import com.kinglead.demo.domain.User;
 import com.kinglead.demo.service.UserService;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.servlet.ModelAndView;
 ​
 import javax.annotation.Resource;
 import java.util.List;
 ​
 @Controller
 @RequestMapping("/user")
 public class UserController {
 ​
   @Resource
   private UserService userService;
 ​
   @RequestMapping("/userList")
   public ModelAndView queryUserList(ModelAndView modelAndView){
     List<User> userList = userService.queryUserList();
     modelAndView.addObject("userList", userList);
     modelAndView.setViewName("userList");
     return modelAndView;
   }
 ​
 }

(8)用户列表页面

<!DOCTYPE html>
 <html xmlns:th="http://www.thymeleaf.org">
   <head>
     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
     <title>用户信息</title>
     <!--<link rel="stylesheet" type="text/css" href="http://www.cppcns.com/css/common.css" rel="external nofollow" />-->
     <style type="text/css">
       table {
         border: 1px solid black;
         text-align: center;
         border-collapse: collapse;
       }
       table thead th {
         border: 1px solid black;
       }
       table tbody td {
         border: 1px solid black;
       }
     </style>
   </head>
   <body>
     <div>
       <h2>用户列表</h2>
     </div>
     <table cellpadding="0" cellspacing="0">
       <thead>
         <th>序号</th>
         <th>编码</th>
         <th>用户名</th>
       </thead>
       <tbody>
       <tr th:each="entries,stat:${userList}" th:style="' color: rgb(17, 119, 0);">>
         <td th:text="${stat.count}"></td>
         <td th:text="${entries['id']}"></td>
         <td th:text="${entries['name']}"></td>
       </tr>
       </tbody>
     </table>
   </body>
 </html>

Springboot mybatis-plus配置及用法详解

扫一扫手机访问