参考链接官网:https://baomidou.com/
首先准备好数据库和表
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', '一句甚安勿念 你说落笔太难');
贴一下完整的项目结构(绿色箭头是后面要新建的):
在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>
在 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
如图,在src\main\java\com\example\mp_demo
下新建文件夹
entity
、mapper
并在entity
新建实体类Song.java
、在mapper
下新建接口SongMapper.java
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> {
}
在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);
}
}
控制台输出:
到此就是对MP的快速入门使用了,但是实际开发中酱紫用的很少
还要敲代码太累了,那么就不得不学习一下MP的代码生成器了
MyBatisPlus入门学习:利用MP代码生成器快速开发