DM8整合java的jpa框架(附整合源码)

整合完毕后的地址:https://gitee.com/gy297879328/dm_jpa_demo/tree/master
项目结构
DM8整合java的jpa框架(附整合源码)_第1张图片

一、达梦数据库的建表sql

CREATE TABLE "T_USER"
(
"ID" INT IDENTITY(1, 1) NOT NULL,
"USERNAME" VARCHAR(50),
"PHONE" VARCHAR(50),
UNIQUE("ID"));


-- oracle
create sequence user_seq
    increment by 1    -- 每次递增1
    start with 1       -- 从1开始
    nomaxvalue      -- 没有最大值
    minvalue 1       -- 最小值=1
    NOCYCLE;      -- 不循环
    
 CREATE TRIGGER user_seq
       BEFORE INSERT ON T_USER
       FOR EACH ROW
       WHEN (new.id IS Null)
BEGIN
       SELECT user_seq.nextval INTO:new.id FROM sys.dual;
END;

二、选择hibernate框架配套的数据库驱动

数据库驱动在达梦数据库的安装目录\drivers\jdbc
hibernate框架所需的方言包在dialect目录。对应关系可以看同目录下的readme.txt文件。

DM8整合java的jpa框架(附整合源码)_第2张图片

DM8整合java的jpa框架(附整合源码)_第3张图片

三、引入依赖

在项目中我把数据库的驱动包以及方言包放到了lib目录下单独加载了,所以pom文件中没有。

    <dependencies>

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

        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
            <version>5.4.22.Finalversion>
        dependency>


        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

四、application.yml

server:
  port: 8080
spring:
  datasource:
    driver-class-name: dm.jdbc.driver.DmDriver
    url: jdbc:dm://127.0.0.1:5236
    username: SYSDBA
    password: SYSDBA
  # jpa相关配置
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    open-in-view: false
    properties:
      hibernate:
        dialect: org.hibernate.dialect.DmDialect

五、实体类


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "T_USER")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer ID;
    
    @Column(name = "USERNAME")
    private String username;
    @Column(name = "PHONE")
    private String phone;
}

六、Repository


import com.dameng.jpa_dm.mode.User;
import org.springframework.data.jpa.repository.JpaRepository;


public interface UserRepository extends JpaRepository<User, Integer> {
}

七、controller

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/user")
    public List<User> findAll() {
        userRepository.save(User.builder().username("xiaoqiang").phone("120").build());
        return userRepository.findAll();
    }
}

你可能感兴趣的:(达梦数据库,DM8,JPA)