java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Mybatis-plus结构构建

快速上手Mybatis-plus结构构建过程

作者:qwecxzz

这篇文章主要介绍了快速上手Mybatis-plus结构构建过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

前言

如何快速上手使用一次Mybatis-plus?

话不多说 直接开始

官方文档

配置实体类(和自己数据库表对应)

package com.example.jsr303.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;

/**
 * 学习测试JSR303
 */

@Data
@TableName("test")
public class TestEntity implements Serializable {

    //忽略掉 设置序列化和反序列化的serialVersionUID
    //想了解一下可以去这篇博客 https://blog.csdn.net/qq_45503106/article/details/107950914
    private static final long serialVersionUID = 1L;

    /**
     * test表主键ID
     */
    @TableId
    private Integer id;
    /**
     *  姓名
     */
    private String name;

    /**
     * 邮箱
     */
    private String email;

}

一定记得使用@TableName 绑定你这个数据库的表

否则按照 数据库名+实体类 名去找你的表

一般都找不到

一、导入依赖

<dependencies>
<!--        web mvc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--        mysql链接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
<!--        lombok简化插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
<!--        test测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--        myabtis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>

二、配置配置文件yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/jsr303?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
server:
  port: 8082
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

三 配置Mapper Service Controller三层架构

Mapper层

package com.example.jsr303.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.jsr303.entity.TestEntity;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TestDao extends BaseMapper<TestEntity> {
}

Service层

package com.example.jsr303.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.jsr303.entity.TestEntity;
import org.springframework.stereotype.Service;


public interface TestService extends IService<TestEntity> {
}

ServiceImpl层

package com.example.jsr303.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jsr303.dao.TestDao;
import com.example.jsr303.entity.TestEntity;
import com.example.jsr303.service.TestService;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl extends ServiceImpl<TestDao, TestEntity> implements TestService {
}

Controller层

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    TestService testService;

    @RequestMapping("/list")
    public List<TestEntity> selectAll(){
     return testService.list();
    }
}

常用的Service接口官方文档

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文