整合完毕后的地址:https://gitee.com/gy297879328/dm_jpa_demo/tree/master 项目结构
一、达梦数据库的建表sql
CREATE TABLE "T_USER"
(
"ID" INT IDENTITY(1, 1) NOT NULL,
"USERNAME" VARCHAR(50),
"PHONE" VARCHAR(50),
UNIQUE("ID"));
create sequence user_seq
increment by 1
start with 1
nomaxvalue
minvalue 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文件。
三、引入依赖
在项目中我把数据库的驱动包以及方言包放到了lib目录下单独加载了,所以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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.22.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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:
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();
}
}
|