Springboot + jpa + PG

spring: datasource: url: jdbc:postgresql://localhost:5432/testdb username: pg password: pg driver-class-name: org.postgresql.Driver

jpa: hibernate: ddl-auto: update show-sql: true

  1. <a name="uda8h"></a>
  2. # 多数据源
  3. ```yaml
  4. spring:
  5. datasource:
  6. primary:
  7. jdbc-url: jdbc:mysql://localhost:3306/jpa?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
  8. username: root
  9. password: root
  10. driver-class-name: com.mysql.cj.jdbc.Driver
  11. secondary:
  12. jdbc-url: jdbc:postgresql://localhost:5432/testdb
  13. username: pg
  14. password: pg
  15. driver-class-name: org.postgresql.Driver
  16. jpa:
  17. hibernate:
  18. ddl-auto: update
  19. show-sql: true
  20. properties:
  21. hibernate:
  22. # 暂时都用mysql方言,理论上应分配配置
  23. dialect: org.hibernate.dialect.MySQLDialect

DynamicDataSource, AOP切换数据源

常规Spring分别配置dataSource、sqlSessionFactory、transactionManger

注意:所谓的“在提交user事务的时候出错”,是指commit出错,比如数据库断电。而业务抛出异常、数据库操作异常(例如字段缺失),也是可以全部回滚的。

  1. -- 如果在表名前指定了 ONLY,则只会扫描该表。如果没有指定ONLY,该表及其所有后代表(如果有)都会被扫描。
  2. -- 可选地,可以在表名后指定*来显式地指示包括后代表。
  3. SELECT COUNT(1) FROM only native_emp;

填坑之路!IDEA提示Cannot resolve table/column