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

[帮你们填过坑了]针对国产化数据库''达梦'' 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: root
    password: 123
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: dm.jdbc.driver.DmDriver
# jpa相关配置
 jpa:
    show-sql: false
    hibernate:
      ddl-auto: update
    open-in-view: false
    properties:
      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接口注入
   @Autowired
   private 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 "";
}

4.2 创建切面

/**
 * @author tongJie
 * @date 2021/7/16 11:58
 *  达梦数据库切面
 * <p>
 * description
 */
@Aspect
@Component
public class DmSystemLogAspect {

    @Autowired
    private DmSQLMapper dmSQLMapper;


    @Pointcut("@annotation(自定义注解文件的位置)")
    public void controllerAspect(){

    }

    @Before("controllerAspect()")
    public void on(JoinPoint point){


        // 获取注解中的参数值
        MethodSignature methodSignature = (MethodSignature)point.getSignature();
        Method method = methodSignature.getMethod();

        // 获取注解Action
        DmSave 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();

        // 获取注解Action
        DmSave annotation = method.getAnnotation(DmSave.class);

        // 获取了参数类名
        String[] strings = annotation.className();

        if (strings.length > 0){
            // 进行达梦数据库on操作
            for (String tableName : strings) {
                dmSQLMapper.on(tableName);
            }
        }
    }
}

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

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值