MyBatisPlus快速入门

MyBatisPlus快速入门

参考链接官网:https://baomidou.com/

首先准备好数据库和表

MyBatisPlus快速入门_第1张图片

CREATE TABLE `song` (
  `id` varchar(64) NOT NULL COMMENT '主键id',
  `create_user` varchar(64) DEFAULT NULL COMMENT '创建人',
  `create_time` date DEFAULT NULL COMMENT '创建时间',
  `name` varchar(128) DEFAULT NULL COMMENT '歌曲名',
  `author` varchar(64) DEFAULT NULL COMMENT '作者',
  `duration` varchar(64) DEFAULT NULL COMMENT '时长',
  `lyric` varchar(2048) DEFAULT NULL COMMENT '歌词',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `song`(`id`, `create_user`, `create_time`, `name`, `author`, `duration`, `lyric`) VALUES ('1', '1', '2022-08-03', '暗号', '周杰伦', '1,2;3,4;5,6', '你!才!会!收!到!暗!号!!!');
INSERT INTO `song`(`id`, `create_user`, `create_time`, `name`, `author`, `duration`, `lyric`) VALUES ('2', '1', '2022-08-03', '你听得到', '周杰伦', '1,3;4;8,6', '有谁能比我知道,你的温柔像羽毛···');
INSERT INTO `song`(`id`, `create_user`, `create_time`, `name`, `author`, `duration`, `lyric`) VALUES ('3', '1', '2022-08-03', '断了的弦', '周杰伦', '3,5;8,16', '我了解离开树的叶,属于地上的世界调~谢~~~ye~~~');
INSERT INTO `song`(`id`, `create_user`, `create_time`, `name`, `author`, `duration`, `lyric`) VALUES ('4', '1', '2022-08-03', '粉色海洋', '周杰伦', '4,5;8,15', '粉色海洋 你回头望 笑得像天空里的棉花糖');
INSERT INTO `song`(`id`, `create_user`, `create_time`, `name`, `author`, `duration`, `lyric`) VALUES ('5', '1', '2022-08-03', '红颜如霜', '周杰伦', '1,11', '一句甚安勿念 你说落笔太难');
1.新建一个springboot项目
  • 图文教程:idea新建SpringBoot项目
  • 使用 Spring Initializer 快速初始化一个 Spring Boot 工程

贴一下完整的项目结构(绿色箭头是后面要新建的):

MyBatisPlus快速入门_第2张图片

2.添加依赖

pom.xml文件中添加如下依赖:


<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.3.4.RELEASEversion>
    <relativePath/>
parent>


<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starterartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
    
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-boot-starterartifactId>
        <version>3.5.2version>
    dependency>

    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <scope>runtimescope>
        <version>8.0.25version>
    dependency>

    
    
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>1.18.12version>
        <scope>providedscope>
    dependency>
dependencies>
3.配置

application.yml中添加mysql数据库配置(如果是application.properties更改后缀为yml即可)

# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mp_db?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 1234
4.编写实体类和接口

如图,在src\main\java\com\example\mp_demo下新建文件夹

entitymapper并在entity新建实体类Song.java、在mapper下新建接口SongMapper.java

MyBatisPlus快速入门_第3张图片

Song.java

package com.example.mp_demo.entity;

import lombok.Data;

/**
 * @author qingchen
 * @date 8/8/2022 下午 3:37
 */
@Data
public class Song {
    private String id;
    private String createUser;
    private String createTime;
    private String name;
    private String author;
    private String duration;
    private String lyric;
}

SongMapper.java

package com.example.mp_demo.mapper;

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

/**
 * @author qingchen
 * @date 8/8/2022 下午 3:40
 */
@Mapper
public interface SongMapper extends BaseMapper<Song> {
}
5.测试

src\test\java\com\example\mp_demo\MpDemoApplicationTests下编写测试代码

@SpringBootTest
class MpDemoApplicationTests {

    @Autowired
    private SongMapper songMapper;

    @Test
    void contextLoads() {
        List<Song> songList = songMapper.selectList(null);
        songList.forEach(System.out::println);
    }

}

控制台输出:

MyBatisPlus快速入门_第4张图片

END

到此就是对MP的快速入门使用了,但是实际开发中酱紫用的很少

还要敲代码太累了,那么就不得不学习一下MP的代码生成器了

MyBatisPlus入门学习:利用MP代码生成器快速开发

你可能感兴趣的:(MyBatis-Plus,spring,boot,mybatis,java)