三、springboot集成金仓数据库

系列导航

一、金仓数据库搭建(单机)

二、金仓数据库搭建(主从安装)

三、springboot集成金仓数据库

1、数据库中创建表

CREATE TABLE TEST_BLOCK_T   
(
  BLOCK_ID         varchar(10 char)  PRIMARY   KEY,    --编码
  BLOCK_NAME       varchar(200 char)                     --名称 
); 

Insert into TEST_BLOCK_T (BLOCK_ID, BLOCK_NAME)Values('1', '哈哈哈');

2、找到金仓数据库的驱动文件

金仓服务器的安装路径下找到驱动文件

 

 

 

3、引入驱动文件

引入驱动文件有两种方法

方法一

jar包放入工程里

 

 

 依赖文件这么写

<dependency>
    <groupId>kingbase</groupId>
    <artifactId>kingbase8</artifactId>
    <version>8.6.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/lib/kingbase8-8.6.0.jar</systemPath>
</dependency>

方法二

使用mvn的命令将jar包安装包本地仓库里

Mvn install:install-file -Dfile=D:\kingbase8-8.6.0.jar -DgroupId=kingbase -DartifactId=kingbase8 -Dversion=8.6.0 -Dpackaging=jar

 

 

 依赖文件这么写

<dependency>
    <groupId>com.dm</groupId>
    <artifactId>Dm8JdbcDriver</artifactId>
    <version>1.8</version>
</dependency>

4、pom.xml文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.3.0.RELEASE</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 集成mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!-- 集成金仓驱动jar包方式  -->
<!--        <dependency>-->
<!--            <groupId>kingbase</groupId>-->
<!--            <artifactId>kingbase8</artifactId>-->
<!--            <version>8.6.0</version>-->
<!--            <scope>system</scope>-->
<!--            <systemPath>${project.basedir}/src/lib/kingbase8-8.6.0.jar</systemPath>-->
<!--        </dependency>-->


        <!-- 集成金仓驱动 本地仓库生成pom文件方式 -->
        <dependency>
            <groupId>kingbase</groupId>
            <artifactId>kingbase8</artifactId>
            <version>8.6.0</version>
        </dependency>


    </dependencies>

5、配置文件

# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080


spring.datasource.driverClassName=com.kingbase8.Driver
spring.datasource.url=jdbc:kingbase8://192.168.0.100:54321/BASE_PLATEDB
spring.datasource.username=BASE_USER
spring.datasource.password=KING_PASSWORD

#mybatis-plus控制台打印sql
mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

6、工程结构

 

 

7、源码

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

 

package com.example.demo.controller;


import com.example.demo.domain.Block;
import com.example.demo.mapper.BlockMapper;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private BlockMapper blockMapper;


    @GetMapping("/list")
    @ResponseBody
    public List index() {

        //直接调用BaseMapper封装好的CRUD方法,就可实现无条件查询数据
        List<Block> list = blockMapper.selectList(null);

        //循环获取用户数据
        for (Block block:list){
            //获取用户名称
            System.out.println(block.getBlockName());
        }


        return list;
    }
}

 

package com.example.demo.domain;


import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;


@TableName(value = "TEST_BLOCK_T")
public class Block {
    private static final long serialVersionUID = 1L;

    @TableId
    private String blockId;

    private String blockName;

    public String getBlockId() {
        return blockId;
    }

    public void setBlockId(String blockId) {
        this.blockId = blockId;
    }

    public String getBlockName() {
        return blockName;
    }

    public void setBlockName(String blockName) {
        this.blockName = blockName;
    }

    @Override
    public String toString() {
        return "XyDicBlockT{" +
                "blockId='" + blockId + '\'' +
                ", blockName='" + blockName + '\'' +
                '}';
    }
}

 

package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.domain.Block;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface BlockMapper extends BaseMapper<Block> {

}

8、测试

 源码获取方式(免费):
(1)登录-注册:http://resources.kittytiger.cn/
(2)签到获取积分
(3)搜索:it白话-springboot集成金仓

 

资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!
posted @ 2022-06-10 16:26  万笑佛  阅读(2641)  评论(0编辑  收藏  举报