Sprinboot+Hibernate+DM8(达梦8)整合实例

1. 创建数据库

CREATE TABLE "SYSDBA"."ADDRESS"(ADDRESSID INT NOT NULL PRIMARY KEY,ADDRESS VARCHAR(60) NOT NULL,CITY VARCHAR(30));

INSERT INTO "SYSDBA"."ADDRESS"(ID,ADDRESS,CITY) VALUES(1,'sihui','beijing');
INSERT INTO "SYSDBA"."ADDRESS"(ID,ADDRESS,CITY) VALUES(2,'chayangqu','beijing');
INSERT INTO "SYSDBA"."ADDRESS"(ID,ADDRESS,CITY) VALUES(3,'guomao','beijing');

select * from "SYSDBA"."ADDRESS";

drop table  "SYSDBA"."ADDRESS";

遇到的坑:在insert之后需要 commit,否则查询不出来数据

2. 添加依赖

2.1 项目目录结构

Sprinboot+Hibernate+DM8(达梦8)整合实例_第1张图片

2.2 添加依赖

DmJdbcDriver18.jar (…\安装目录\drivers\jdbc)、DmDialect-for-hibernate5.3.jar(…\安装目录\drivers\jdbc\dialect)

pom.xml

<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.1.9.RELEASEversion>
parent>
<groupId>com.javagroupId>
<artifactId>TestDMartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<name>componentsname>

<properties>
    <java.version>1.8java.version>
    <swagger.version>2.9.2swagger.version>
properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
        <groupId>com.dmgroupId>
        <artifactId>Dm8JdbcDriverartifactId>
        <version>1.7version>
        <scope>systemscope>
        <systemPath>${project.basedir}/src/main/resources/lib/DmJdbcDriver18.jarsystemPath>
    dependency>

    <dependency>
        <groupId>com.dmgroupId>
        <artifactId>hibernate5artifactId>
        <version>5.0version>
        <scope>systemscope>
        <systemPath>${project.basedir}/src/main/resources/lib/DmDialect-for-hibernate5.3.jarsystemPath>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-jpaartifactId>
        <version>2.1.6.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>1.16.18version>
    dependency>

    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-swagger-uiartifactId>
        <version>${swagger.version}version>
    dependency>
    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-swagger2artifactId>
        <version>${swagger.version}version>
    dependency>

dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
        plugin>
    plugins>
build>
##### 2.3 代码

application.yml(配置)

server:
  port: 8120
  address: localhost
spring:
  datasource:
    driver-class-name: dm.jdbc.driver.DmDriver
    url: jdbc:dm://192.168.18.160:5236
    username: SYSDBA
    password: 123456789
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.DmDialect
    hibernate:
      ddl-auto: none
    show-sql: true
启动类
package com.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
public class ComponentsApplication  {

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

}

controller层
import com.java.service.AddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/addr")
public class AddrController {

    @Autowired
    private AddrService userService;

    @RequestMapping("/queryAll")
    public List
queryAll(){ List
addresses = userService.queryAll(); System.out.println(addresses); return addresses; } @GetMapping(value = "/queryById/{id}") public Address queryById(@PathVariable("id")Integer id){ return userService.queryById(id); } }

entity(实体类)

import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

@Data
@Entity
@Table(name = "ADDRESS")
public class Address implements Serializable {

    @Id
    @Column(name = "ID")
    private int id;

    @Column(name = "ADDRESS")
    private String address;

    @Column(name = "CITY")
    private String city;
}

service层

//接口类
import com.java.entity.Address;
import java.util.List;

public interface AddrService {

    public List<Address> queryAll();

    public Address queryById(Integer id);
}

// 实现类
import com.java.entity.Address;
import com.java.repository.AddrRepository;
import com.java.service.AddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class AddrServiceImpl implements AddrService {

    @Autowired
    private AddrRepository addrRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Address> queryAll() {
        return addrRepository.findAll();
    }

    @Override
    public Address queryById(Integer id) {
        Optional<Address> optional = addrRepository.findById(id);
        Address address = null;
        if (optional!=null && optional.isPresent()){
            address = optional.get();
        }
        return address;
    }
}

Repository 层

import com.java.entity.Address;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface AddrRepository extends JpaRepository<Address,Integer> {

}

访问

http://localhost:8120/addr/queryAll

你可能感兴趣的:(学习java之路)