SpringBoot集成openGauss

发布时间 2023-09-20 09:51:23作者: 代码吴彦祖

1.pom依赖

 <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>



        <!--openGuass-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>



    </dependencies>

2.yml文件配置

spring:
  datasource:
    # config mysql
    url: jdbc:postgresql://172.17.33.113:5432/dabu_db_demo
    username: dabu
    password: Free-Wi11
    driver-class-name: org.postgresql.Driver


# print logs
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: false

server:
  port: 8085

3.项目结构图

 

 dao层

package com.lv.stusy.gauss_boot.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lv.stusy.gauss_boot.entity.DabuDbDemo;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface DabuDbDemoDao extends BaseMapper<DabuDbDemo> {
}

 entity层

package com.lv.stusy.gauss_boot.entity;


import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("table01")
public class DabuDbDemo {


    private int firstcol;


}


/*
注意:下面的数据库字段和实体字段对应;
(1)如果,两个字段完全一样,则必须关闭application.yml中的map-underscore-to-camel-case属性;
    mybatis-plus:
        configuration:
            # 关闭数据库下划线自动转驼峰
            map-underscore-to-camel-case: false

(2)如果,数据库字段属性是下划线,实体字段是驼峰式,则必须开启application.yml中的map-underscore-to-camel-case属性(默认开启)
*/

  service层

package com.lv.stusy.gauss_boot.service;

import com.lv.stusy.gauss_boot.dao.DabuDbDemoDao;
import com.lv.stusy.gauss_boot.entity.DabuDbDemo;

import java.util.List;

public interface DabuDbDemoService {

    List<DabuDbDemo> query();

}

  impl层

package com.lv.stusy.gauss_boot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lv.stusy.gauss_boot.dao.DabuDbDemoDao;
import com.lv.stusy.gauss_boot.entity.DabuDbDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DabuDbDemoServiceImpl implements DabuDbDemoService {

    @Autowired
    private DabuDbDemoDao dabuDbDemoDao;

    @Override
    public List<DabuDbDemo> query(){
        QueryWrapper<DabuDbDemo> wrapper = new QueryWrapper<>();

        return this.dabuDbDemoDao.selectList(wrapper);
    }


}

  

Controller层
package com.lv.stusy.gauss_boot.controller;


import com.lv.stusy.gauss_boot.entity.DabuDbDemo;
import com.lv.stusy.gauss_boot.service.DabuDbDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/gadb")
public class dabuDbDemoController {


    @Autowired
    public DabuDbDemoService dabuDbDemoService;


    @GetMapping("/query")
    public List<DabuDbDemo> queryData(){
        return dabuDbDemoService.query();
    }

}

  启动请求访问

 

数据库验证