create database david
;
use david
;
创建tbuser表
CREATE TABLE `tbuser` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`password` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`userroles` varchar(2) COLLATE utf8_bin DEFAULT NULL,
`nickname` varchar(50) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
INSERT INTO tbuser (username,PASSWORD,userroles,nickname) VALUES
('admin','1234','04','管理员'),('lagou','1234','03','拉勾教育')
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.4.RELEASEversion>
<relativePath/>
parent>
<groupId>com.lagougroupId>
<artifactId>dockerdemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dockerdemoname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<mybatispluins.version>3.3.2mybatispluins.version>
<mysql.version>5.1.47mysql.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>${mybatispluins.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<finalName>${project.name}finalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<configuration>
<skip>trueskip>
configuration>
plugin>
plugins>
build>
project>
为防止与其他项目端口号冲突,将项目端口号更改为8082
server:
port: 8082
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: david
url: jdbc:mysql://192.168.198.110:3306/lagou?characterEncoding=utf- 8&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8
mybatis-plus:
type-aliases-package: com.lagou.dockerdemo.entity
mapper-locations: mapper/*.xml
#配置日志打印方式。不使用 mybatis的日志信息。使用mp的日志配置
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
父类
@SpringBootTest(classes = DockerdemoApplication.class)
@ExtendWith(SpringExtension.class)
public abstract class DockerdemoApplicationTests {
}
TestConn类
public class TestConnection extends DockerdemoApplicationTests {
@Resource
private DataSource dataSource;
@Test
public void testConn() throws SQLException {
Connection connection = this.dataSource.getConnection();
System.out.println("connection = " + connection);
}
}
实体类
@TableName(value = "tbuser")
public class Tbuser {
@TableId(type = IdType.AUTO)
private Integer userid;
private String username,password,userroles,nickname;
......省略getter和setter、构造方法、toString等方法
}
UserMapper接口
package com.lagou.dockerdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lagou.dockerdemo.entity.Tbuser;
public interface UserMapper extends BaseMapper {
}
启动类
@SpringBootApplication
@MapperScan(basePackages = "com.lagou.dockerdemo.mapper")
public class DockerdemoApplication {
public static void main(String[] args) {
SpringApplication.run(DockerdemoApplication.class, args);
}
}
UserService接口
public interface UserService {
List<Tbuser> queryUsers();
}
UserServiceImpl实现类
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override public List<Tbuser> queryUsers() {
return this.userMapper.selectList(null);
}
}
TestUser测试类
public class TestUser extends DockerdemoApplicationTests {
@Resource
private UserService userService;
@Test
public void testQueryUsers() {
List<Tbuser> tbusers = this.userService.queryUsers();
}
}
控制器
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("/users")
public List<Tbuser> queryUsers() {
return this.userService.queryUsers();
}
}
本地测试项目
http://localhost:8082/users
docker run -itd --name mysql --restart always -p 3306:3306 -e
MYSQL_ROOT_PASSWORD=admin mysql:5.7.31
使用客户端工具导入lagou.sql文件
http://localhost:8082/users
脱离开发环境,打包测试项目。
mvn clean package
使用dos命令启动项目
java -jar dockerdemo.jar
制作mysql镜像,优化官网镜像
1)优化镜像时区问题
2)在容器启动时,直接导入lagou.sql数据库
mkdir -p /data/initsql
cd /data/initsql
FROM mysql:5.7.31
# 作者信息 MAINTAINER mysql from date UTC by Asia/Shanghai "laosiji@lagou.com"
ENV TZ Asia/Shanghai
COPY lagou.sql /docker-entrypoint-initdb.d
docker build --rm -t 192.168.198.101:5000/lagouedu/mysql:5.7.1 .
docker images
docker run -itd --name mysql --restart always --privileged=true -p 3306:3306 -e
MYSQL_ROOT_PASSWORD=admin -v /data/mysql:/var/lib/mysql
192.168.198.101:5000/lagouedu/mysql:5.7.1 --character-set-server=utf8 -- collation-server=utf8_general_ci
docker push 192.168.198.101:5000/lagouedu/mysql:5.7.1
docker pull openjdk:8-alpine3.9
docker save openjdk:8-alpine3.9 -o jdk8.tar
docker load -i jdk8.tar
idea安装docker插件。Dockerfile、docker-compose.yml文件大部分内容会有提示信息。方便开发人员编写配置文件。高版本的idea中已经集成docker插件(idea2019、idea2020)
官网地址: https://plugins.jetbrains.com/plugin/7724-docker/versions
/data/dockerdemo/Dockerfile
mkdir -p /data/dockerdemo
cd /data/dockerdemo
FROM openjdk:8-alpine3.9
# 作者信息
MAINTAINER laosiji Docker springboot "laosiji@lagou.com"
# 修改源
RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories && \
echo "http://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories
# 安装需要的软件,解决时区问题
RUN apk --update add curl bash tzdata && \
rm -rf /var/cache/apk/*
# 修改镜像为东八区时间
ENV TZ Asia/Shanghai
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
EXPOSE 8082
ENTRYPOINT ["java","-jar","/app.jar"]
docker build --rm -t lagou/dockerdemo:v1 --build-arg JAR_FILE=dockerdemo.jar .
docker run -itd --name dockerdemo -p 8080:8080 lagou/dockerdemo:v1
docker logs -f dockerdemo
http://192.168.198.100:8082
docker stop dockerdemo d
ocker rm dockerdemo