springboot使用国产数据库达梦项目搭建

本项目源码:这里查看

1.准备工作:

1.1 达梦数据库下载并正确安装

1.2 在SYSDBA下创建表TESTTABLE并添加两条数据。如图

springboot使用国产数据库达梦项目搭建_第1张图片

2.springboot项目搭建开始,先浏览下项目目录:

springboot使用国产数据库达梦项目搭建_第2张图片

2.1 创建maven项目 项目名称这里为bootdmtest

2.2 pom文件内容


    4.0.0

    com.budi.bootDM
    bootdmtest
    1.0-SNAPSHOT

    
        
            org.springframework.boot
            spring-boot-starter-web
            2.1.9.RELEASE
        
        
        
            com.dm
            jdbc
            system
            ${project.basedir}/src/main/resources/lib/DmJdbcDriver18.jar
        
        
            com.dm
            hibernate5
            system
            ${project.basedir}/src/main/resources/lib/DmDialect-for-hibernate5.3.jar
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
            2.1.6.RELEASE
        
        
            org.projectlombok
            lombok
            1.16.18
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                    true
                
            
        
    

 

pom文件里面的两个jar包(达梦数据库驱动包和hibernate方言包)在达梦数据库安装包中找到

2.3yaml文件内容

server:
  port: 9001
spring:
  datasource:
    url: jdbc:dm://127.0.0.1:5236
    username: SYSDBA
    password: SYSDBA
    driver-class-name: dm.jdbc.driver.DmDriver
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.DmDialect
    show-sql: true

2.4 创建与表对应的实体类TestTable

package com.budi.boot.domain;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;

@Data
@Entity
@Table(name = "TESTTABLE")
public class TestTable {
    @Id
    @Column(name = "ID")
    private Integer id;
    @Column(name = "NAME")
    private String name;
    @Column(name="SCOPE")
    private BigDecimal scope;
}

2.5 创建接口 TestTableRepository 并继承 JpaRepository

package com.budi.boot.repository;

import com.budi.boot.domain.TestTable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TestTableRepository extends JpaRepository {
}

2.6 创建TestTableService及其实现类TestTableServiceImpl

package com.budi.boot.service;

import com.budi.boot.domain.TestTable;

import java.util.List;

public interface TestTableService {
    List getAllData();
    TestTable getTestTableById(Integer id);
}
package com.budi.boot.service.impl;

import com.budi.boot.domain.TestTable;
import com.budi.boot.repository.TestTableRepository;
import com.budi.boot.service.TestTableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class TestTableServiceImpl implements TestTableService {
    @Resource
    private TestTableRepository repository;
    public List getAllData() {
        return repository.findAll();
    }

    public TestTable getTestTableById(Integer id) {
        TestTable table = repository.findById(id).get();
        return table;
    }
}

2.7 创建controller类 DMController

package com.budi.boot.controller;

import com.budi.boot.domain.TestTable;
import com.budi.boot.service.TestTableService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DMController {
    @Resource
    private TestTableService service;
    @RequestMapping("/testA")
    @ResponseBody
    public String testA(){
        return "testA";
    }

    @GetMapping("/getAll")
    public List getAll(){
       return service.getAllData();
    }

    @GetMapping(value = "/getById/{id}")
    public TestTable getTestTableById(@PathVariable("id")Integer id){
        return service.getTestTableById(id);
    }
}

2.8 最后一步,主启动类 DmMain

package com.budi.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

3 测试

启动主启动类

3.1 访问http://localhost:9001/getAll

springboot使用国产数据库达梦项目搭建_第3张图片

3.2 访问http://localhost:9001/getById/1

springboot使用国产数据库达梦项目搭建_第4张图片

测试成功,连接达梦数据库成功。

ps自我督促:关于项目中hibernate和jpa的知识,需要有待提高。

你可能感兴趣的:(达梦数据库,SpringBoot,spring,boot,数据库)