没安装的小伙伴建议参考我的这篇文章 : 点我
<!-- 达梦数据库 --><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>
# 数据库相关
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
@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 ;
}}
这里我们需要用到切面:
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);}}}
}
好了今天为大家带来的就是这些了,有什么不会的记得私信我!
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态