spring boot+jpa接入达梦数据库
文章目录
- 前言
- 依赖
- 配置
- 对应的domain类和repository
前言
最近有一个新项目,由于信息安全等要求只能使用达梦数据库(dm8),之前从来没用过,特此开一个笔记记录一下spring boot+jpa如何使用达梦数据库完成开发。
依赖
pom文件需要
<!-- https://mvnrepository.com/artifact/com.dameng/Dm8JdbcDriver18 -->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>Dm8JdbcDriver18</artifactId>
<version>8.1.1.49</version>
</dependency>
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmDialect-for-hibernate5.3</artifactId>
<version>8.1.1.49</version>
</dependency>
配置
spring:
devtools:
restart:
enabled: false
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: "dm.jdbc.driver.DmDriver"
url: "jdbc:dm://${DM_HOST}:${DM_PORT}/${DM_DATABASE}"
username: ${DM_USERNAME}
password: ${DM_PASSWORD}
druid:
initial-size: 5
min-idle: 20
maxActive: 100
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: "SELECT 1"
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 10
filters: "stat,slf4j"
web-stat-filter:
enabled: false
jpa:
database-platform: "org.hibernate.dialect.DmDialect"
show-sql: true
open-in-view: false
hibernate:
default_schema: suos
ddl-auto: none
properties:
javax:
persistence:
sharedCache:
mode: ENABLE_SELECTIVE
hibernate:
default_schema: ${DM_DATABASE}"
use_jdbc_metadata_defaults: false
enable_lazy_load_no_trans: true
format_sql: true
connection:
provider_disables_autocommit: false
generate_statistics: false
id:
new_generator_mappings: true
jdbc:
batch_size: 25
order_inserts: true
order_updates: true
query:
fail_on_pagination_over_collection_fetch: true
in_clause_parameter_padding: true
show_sql: true
dialect: org.hibernate.dialect.DmDialect
对应的domain类和repository
domain
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;
@NoArgsConstructor
@SuperBuilder
@Getter
@Setter
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(schema = "SUOS")
public class Class extends AbstractModel{
@Schema(description = "校区id")
private Integer campusId;
@Schema(description = "校区名称")
private String campusName;
@Schema(description = "专业id")
private Integer majorId;
@Schema(description = "专业代码")
private String majorCode;
@Schema(description = "专业名称")
private String majorName;
@Schema(description = "方向")
private String field;
@Schema(description = "界别")
private String grade;
@Schema(description = "班级名称")
private String name;
@Schema(description = "班号")
private String number;
}
注意这个schema最好是带上,不然可能会给你写到默认database里去
repository
import com.timerchina.suos.domain.Class;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface ClassRepository extends JpaRepository<Class, Integer>, JpaSpecificationExecutor<Class> {
}