飞道的博客

IDEA-SpringBoot整合快速MyBatisPlus(一)

305人阅读  评论(0)

简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
这是官方阐述,我就不废话了,直接上代码和干货

开始整合

GItHUB 项目地址:https://github.com/564580134/MyBatisPlus

创建项目

  • File ->new ->project -project initializr
  • 项目名称: mybatis_plus
  • 包路径: com.mybatis_plus

下面我们开始进行application.yml、model、mapper、service、serviceImpl、controller的创建维护工作

1、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mybatis_plus</groupId>
    <artifactId>mybatis_plus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis_plus</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!-- mybatis-plus SpringBoot整合 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、model

package com.mybatis_plus.model;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @Description: 用户信息
 * @Author: LiuRunYong
 * @Date: 2020/4/22
 **/


@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName(value = "user")
public class UserModel implements Serializable {

    /**
     * 用户主键
     */
    private Integer userId;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 用户账号
     */
    private String account;

    /**
     * 用户密码
     */
    private String password;

    /**
     * 用户电话
     */
    private String phone;

    /**
     * 用户性别(0:男,1:女)
     */
    private Integer sex;

    /**
     * 用户年龄
     */
    private Integer age;

    /**
     * 用户状态
     */
    private Integer status;
}

3、mapper

  • 此处需要赘述一下 如果要使用MyBatisPlus插件 那就在mapper 接口集成一下BaseMapper<实体Bean>
  • 这样的你mapper接口 就具备了CRUD功能
package com.mybatis_plus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mybatis_plus.model.UserModel;

/**
 * @Description:
 * @Author: LiuRunYong
 * @Date: 2020/4/22
 **/
public interface UserMapper extends BaseMapper<UserModel> {

}

4、service

package com.mybatis_plus.service;

import com.mybatis_plus.model.UserModel;

import java.util.List;

/**
 * @Description:
 * @Author: LiuRunYong
 * @Date: 2020/4/22
 **/
public interface UserService {

    /**
     * 查询所有用户信息
     *
     * @return list
     */
    List<UserModel> selectALlUserList();
}

5、serviceImpl

package com.mybatis_plus.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mybatis_plus.mapper.UserMapper;
import com.mybatis_plus.model.UserModel;
import com.mybatis_plus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: LiuRunYong
 * @Date: 2020/4/22
 **/

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<UserModel> selectALlUserList() {
        return userMapper.selectList(new QueryWrapper<>());
    }
}

6、controller

package com.mybatis_plus.controller;

import com.mybatis_plus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: LiuRunYong
 * @Date: 2020/4/22
 **/
@Slf4j
@RestController
@RequestMapping("/user/")
public class UserController {


    final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }


    @GetMapping("list")
    public Map<String, Object> listAll() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", userService.selectALlUserList());
        result.put("msg", "success");
        return result;

    }


}

7、application.yml

# 服务端口号
server:
  port: 10324

# 数据库相关配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:10224/mybatis_plus?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: 用户
    password: 密码

激动人心的测试


OK到此就算把MyBatisPlus和SpringBoot整合成功,下一篇我们及介绍怎么使用更牛的 CRUD

备注: 此系列以后周更新一次 讨论群聊


转载:https://blog.csdn.net/qq_36481052/article/details/105678069
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场