关于springboot:通过Docker启动DB2并在Spring-Boot整合DB2

44次阅读

共计 3107 个字符,预计需要花费 8 分钟才能阅读完成。

1 简介

DB2 是 IBM 的一款优良的关系型数据库,简略学习一下。

2 Docker 装置 DB2

为了疾速启动,间接应用 Docker 来装置 DB2。先下载镜像如下:

docker pull ibmcom/db2:11.5.0.0

启动数据库如下:

docker run -itd \
  --name mydb2 \
    --privileged=true \
    -p 50000:50000 \
    -e LICENSE=accept \
    -e DB2INST1_PASSWORD=pkslow \
    -e DBNAME=testdb \
    ibmcom/db2:11.5.0.0

这样取得的数据库,具体信息如下:

连贯 URL:jdbc:db2://localhost:50000/testdb

用户名:db2inst1

明码:pkslow

在 IDEA 上连贯如下:

默认的 Schema 为 DB2INST1,测试 SQL 如下:

创立表:

CREATE TABLE EMPLOYEE_SALARY
(DEPTNO   CHAR(3)      NOT NULL,
 DEPTNAME VARCHAR(36)  NOT NULL,
 EMPNO    CHAR(6)      NOT NULL,
 SALARY   DECIMAL(9,2) NOT NULL WITH DEFAULT);

插入数据:

INSERT INTO EMPLOYEE_SALARY (DEPTNO, DEPTNAME, EMPNO, SALARY) VALUES ('001', 'IT', '001322', 80000.00);
INSERT INTO EMPLOYEE_SALARY (DEPTNO, DEPTNAME, EMPNO, SALARY) VALUES ('001', 'IT', '001323', 20000.00);
INSERT INTO EMPLOYEE_SALARY (DEPTNO, DEPTNAME, EMPNO, SALARY) VALUES ('002', 'Architecture', '001324', 3220.00);
INSERT INTO EMPLOYEE_SALARY (DEPTNO, DEPTNAME, EMPNO, SALARY) VALUES ('002', 'Architecture', '001325', 8000.00);
INSERT INTO EMPLOYEE_SALARY (DEPTNO, DEPTNAME, EMPNO, SALARY) VALUES ('003', 'Front Office', '001326', 13320.00);
INSERT INTO EMPLOYEE_SALARY (DEPTNO, DEPTNAME, EMPNO, SALARY) VALUES ('001', 'IT', '001327', 5433.00);

查问:

SELECT * FROM DB2INST1.EMPLOYEE_SALARY;

Spring Boot 整合 DB2

增加相干依赖:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>com.ibm.db2</groupId>
    <artifactId>jcc</artifactId>
    <version>11.5.0.0</version>
  </dependency>
</dependencies>

配置相干属性:

server.port=8082
spring.datasource.url=jdbc:db2://localhost:50000/testdb
spring.datasource.username=db2inst1
spring.datasource.password=pkslow
spring.jpa.generate-ddl=true

创立 Entity:

package com.pkslow.springboot.jpa.db2.entity;

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

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "EMPLOYEE", schema = "DB2INST1")
public class Employee {
    @Id
    private Long id;
    private String name;
}

创立 Repository 类用于操作数据库:

public interface EmployeeRepository extends CrudRepository<Employee, Long> {}

最初,写个办法测试一下:

@Configuration
@Slf4j
public class TestConfig {
    @Autowired
    private EmployeeRepository repository;

    @PostConstruct
    public void init() {Employee employee1 = new Employee(1L, "Larry Deng");
        Employee employee2 = new Employee(2L, "LarryDpk");
        Employee employee3 = new Employee(3L, "Pkslow");

        repository.deleteAll();
        repository.save(employee1);
        repository.save(employee2);
        repository.save(employee3);

        List<Employee> employees = StreamSupport.stream(repository.findAll().spliterator(), false)
                .collect(Collectors.toList());

        employees.forEach(e -> log.info(e.toString()));

    }
}

启动后,日志打印失常如下:

2022-02-28 23:26:22.476  INFO 60847 --- [main] c.pkslow.springboot.jpa.db2.TestConfig   : Employee(id=1, name=Larry Deng)
2022-02-28 23:26:22.476  INFO 60847 --- [main] c.pkslow.springboot.jpa.db2.TestConfig   : Employee(id=2, name=LarryDpk)
2022-02-28 23:26:22.476  INFO 60847 --- [main] c.pkslow.springboot.jpa.db2.TestConfig   : Employee(id=3, name=Pkslow)

表数据如下:

代码

代码请看 GitHub: https://github.com/LarryDpk/p…


References:

Docker Image

Statements Insert

正文完
 0