达梦数据库项目 SpringBoot + jpa + DM8

 2023-09-09 阅读 49 评论 0

摘要:[帮你们填过坑了]针对国产化数据库''达梦'' Srpingboot + jpa + DM8实现增删改查安装达梦数据库(已安装的小伙伴可以忽略这一步)开始进入正题: 前言 : 对于要做zf行业项目的人来说,有些项目似乎需要数据库国产化,但是呢而我们日常所做的项目都是基于

[帮你们填过坑了]针对国产化数据库''达梦'' Srpingboot + jpa + DM8实现增删改查

      • 安装达梦数据库(已安装的小伙伴可以忽略这一步)
      • 开始进入正题:

前言 : 对于要做zf行业项目的人来说,有些项目似乎需要数据库国产化,但是呢而我们日常所做的项目都是基于SSM框架运行的,数据库则是像主流的mysql,oracle,sqlServer等,今天就为大家带来不一样的新操作,本人踩过很长一段的坑,这里为大家填坑直接踩着我过去,今天为大家带来Springboot兼容达梦数据库,只能说希望国产化数据库越做越好吧!

安装达梦数据库(已安装的小伙伴可以忽略这一步)

没安装的小伙伴建议参考我的这篇文章 : 点我

开始进入正题:

  1. 首先添加依赖 :
<!--   达梦数据库     --><dependency><groupId>com.dameng</groupId><artifactId>Dm8JdbcDriver18</artifactId><version>8.1.1.49</version></dependency><dependency><groupId>com.dameng</groupId><artifactId>DmDialect-for-hibernate5.0</artifactId><version>8.1.1.49</version></dependency><dependency><!--注意:只有这个版本的hibernate兼容达梦数据库 --><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>5.3.18.Final</version></dependency>
  1. 填写配置文件:
# 数据库相关
spring:datasource:url: jdbc:dm://localhost:5236?schema=你要连接的库名(这里在数据库叫模式)username: rootpassword: 123type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: dm.jdbc.driver.DmDriver
# jpa相关配置jpa:show-sql: falsehibernate:ddl-auto: updateopen-in-view: falseproperties:hibernate:dialect: org.hibernate.dialect.DmDialect
  1. 开始我们的代码
    3.1 创建实体类
@Data
@Entity
@Table(name = "p_student")
public class StudentEntity implements Serializable{private static final long serialVersionUID = 1L;@Id@GeneratedValue(strategy = GenerationType.SEQUENCE)private int id;/**学生姓名*/private String name;/**学生年龄*/private int age;
}

这里会抛出一个一异常 : 仅当指定列列表,且SET IDENTITY_INSERT为ON时,才能对自增列赋值

// A code block
va```java
@RestController
@RequestMapping(value = "test")
public class TestController {// 将学生Jpa接口注入@Autowiredprivate StudentRepository studentRepository;// 这里为了不浪费大家时间我直接把代码写在conntroller层里面
@PostMapping("/save")
@DmSave(className = {"p_student"}) // 这里多个save实体,直接逗号分隔填入表名
public Response save(@RequestBody StudentEntity studentEntiy){*****看这里!!!!
// 同学们注意看这里,好好听好好学,这里肯定会报错,具体为什么报错,我也不知道啊,咱也不懂啊,好像是id自增的问题,需要自行百度List<StudentEntity> studentEntityList = studentRepository.save(studentEntiy);
return studentEntityList ;
}}
  1. 解决报错:
这里我们需要用到切面: 

4.1 创建自定义注解

 /*** @author tongJie* @date 2021/7/16 13:34* <p>* description*/
@Documented
@Target({ElementType.PARAMETER,ElementType.METHOD,ElementType.TYPE_USE,ElementType.TYPE_PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface DmSave {String[] className() default "";
}

达梦数据库和oracle数据库,4.2 创建切面

/*** @author tongJie* @date 2021/7/16 11:58*  达梦数据库切面* <p>* description*/
@Aspect
@Component
public class DmSystemLogAspect {@Autowiredprivate DmSQLMapper dmSQLMapper;@Pointcut("@annotation(自定义注解文件的位置)")public void controllerAspect(){}@Before("controllerAspect()")public void on(JoinPoint point){// 获取注解中的参数值MethodSignature methodSignature = (MethodSignature)point.getSignature();Method method = methodSignature.getMethod();// 获取注解ActionDmSave annotation = method.getAnnotation(DmSave.class);// 获取了参数类名String[] strings = annotation.className();if (strings.length > 0){// 进行达梦数据库on操作for (String tableName : strings) {dmSQLMapper.on(tableName);}}}@AfterReturning("controllerAspect()")public void off(JoinPoint point){// 获取注解中的参数值MethodSignature methodSignature = (MethodSignature)point.getSignature();Method method = methodSignature.getMethod();// 获取注解ActionDmSave annotation = method.getAnnotation(DmSave.class);// 获取了参数类名String[] strings = annotation.className();if (strings.length > 0){// 进行达梦数据库on操作for (String tableName : strings) {dmSQLMapper.on(tableName);}}}
}

好了今天为大家带来的就是这些了,有什么不会的记得私信我!

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/35486.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息