mysql8
CREATE DATABASE test;
USE test;
CREATE TABLE eric_user(
`id` INT AUTO_INCREMENT,
`name` VARCHAR(255) COMMENT '名称',
PRIMARY KEY (`id`)
);
INNER INTO eric_user(`name`) VALUES('Eric'),('blue');
INSERT INTO `eric_user`(`id`,`name`) VALUES
(1,'Eric'),
(2,'Jack'),
(3,'Lucy');
docker-compose.yml
version: '3.3'
services:
mysql-db:
# 指定容器的名称
container_name: mysql
# 指定镜像和版本
image: mysql:8.0
ports:
- "3306:3306"
restart: always
environment:
# 配置root密码
MYSQL_ROOT_PASSWORD: root
volumes:
# 挂载数据目录
- "./data:/var/lib/mysql"
application.yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8
username: root
password: root
#开启日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
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.3.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demoplus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demoplus</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!--mybatis-plus-->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
service
package com.example.demoplus.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demoplus.entity.EricUser;
import com.example.demoplus.mapper.EricUserMapper;
import com.example.demoplus.service.EricUserService;
import org.springframework.stereotype.Service;
@Service
public class EricUserServiceImpl extends ServiceImpl<EricUserMapper, EricUser> implements EricUserService {
}
package com.example.demoplus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demoplus.entity.EricUser;
public interface EricUserService extends IService<EricUser> {
}
mapper
package com.example.demoplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import com.example.demoplus.entity.EricUser;
@Mapper
public interface EricUserMapper extends BaseMapper<EricUser> {
}
entity
package com.example.demoplus.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "eric_user")
public class EricUser {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField(value = "name")
private String name;
}
controller
package com.example.demoplus.controller;
import com.example.demoplus.entity.EricUser;
import com.example.demoplus.service.EricUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/eric")
public class EricUserController {
@Autowired
private EricUserService userService;
@GetMapping("")
public List<EricUser> getEricUserList() {
List<EricUser> list = userService.list();
return list;
}
}
test

image.png
源码下载
ref
网友评论