MybatisPlus入门案例

参考视频:MyBatisPlus教程,一套玩转mybatisplus框架,mybatis-plus轻松上手 点击观看

文章目录


建库建表

sql 复制代码
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
CREATE TABLE `user` (
                        `id` bigint(20) NOT NULL COMMENT '主键ID',
                        `name` varchar(30) DEFAULT NULL COMMENT '姓名',
                        `age` int(11) DEFAULT NULL COMMENT '年龄',
                        `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
                        PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO user (id, name, age, email) VALUES
                                            (1, 'Jone', 18, 'test1@baomidou.com'),
                                            (2, 'Jack', 20, 'test2@baomidou.com'),
                                            (3, 'Tom', 28, 'test3@baomidou.com'),
                                            (4, 'Sandy', 21, 'test4@baomidou.com'),
                                            (5, 'Billie', 24, 'test5@baomidou.com');

导入依赖

xml 复制代码
<!-- MySQL8 驱动 -->
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- MyBatis-Plus -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
</dependency>

<!-- Lombok -->
<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>
</dependency>

配置application.yaml文件

yaml 复制代码
#配置端口
server:
  port: 80

spring:
  #配置数据源
  datasource:
    #配置数据源类型
    type: com.zaxxer.hikari.HikariDataSource
    # MySQL 8.0 驱动类(标准配置)
    driver-class-name: com.mysql.cj.jdbc.Driver
    # MySQL 8.0 连接地址(增加时区配置,解决8.0必报的时区错误)
    url: jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
    # 数据库用户名(请替换为你的真实用户名)
    username: root
    # 数据库密码(请替换为你的真实密码)
    password: root

#MyBatis-Plus相关配置
mybatis-plus:
  configuration:
    #配置日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

在SpringBoot启动类中添加@MapperScan注解

java 复制代码
package com.findx.mybatisplus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
// 扫描 mapper 接口(指定Mapper接口所在的包)
@MapperScan("com.findx.mybatisplus.mapper")
public class MybatisPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }
}

编写实体类User(使用了Lombok简化代码)

java 复制代码
package com.findx.mybatisplus.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

编写UserMapper接口

java 复制代码
package com.findx.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.findx.mybatisplus.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}

编写测试类

java 复制代码
package com.findx.mybatisplus;

import com.findx.mybatisplus.mapper.UserMapper;
import com.findx.mybatisplus.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;

@SpringBootTest
public class MybatisPlusTest {

    // 自动注入
    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        List<User> list = userMapper.selectList(null);
        list.forEach(System.out::println);
    }
}
相关推荐
代码不停13 小时前
MyBatis操作数据库
数据库·mybatis
wno70414 小时前
Spring Boot中JdbcTemplate与Mybatis使用PostgreSQL
spring boot·postgresql·mybatis
lzhdim1 天前
100条常用SQL语句
数据库·sql·mysql·mybatis
景熙55231 天前
2.什么是业务后台系统
java·spring·tomcat·mybatis·jdbc
2601_962885722 天前
AkShare 和 Tushare 哪个好?区别、场景与选型
mybatis
_Evan_Yao3 天前
Redis 缓存穿透、击穿、雪崩,一次讲清!
java·spring·mybatis
paopaokaka_luck3 天前
宠物门店经营管理系统(销售与库存联动、进货自动记账、 退换与退款关联、员工个人业绩、ECharts 经营分析、滞销识别与客户分层、宠物档案年龄计算)
前端·javascript·spring boot·mysql·echarts·mybatis·1024程序员节
2301_32241428043 天前
springboot农产品交易平台的设计与实现08055-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·spring boot·redis·mysql·tomcat·maven·mybatis
乐茵lin3 天前
Redis 持久化机制详解:RDB 与 AOF
redis·bootstrap·mybatis
bksczm4 天前
Redis数据结构之string
java·redis·mybatis