page contents

Spring Boot神操作-多个数据源Service层封装

当 JVM 内存严重不足时,就会抛出 java.lang.OutOfMemoryError 错误。本文总结了常见的 OOM 原因及其解决方法,如下图所示。如有遗漏或错误,欢迎补充指正。
  • Spring Boot - 多个数据源Service层封装

  • 类关系图

  • 封装的一些配置

    • application.yml

    • pom.xml

  • 封装后使用

    • MySQL 动态数据访问

  • Mongo 动态数据访问

  • ElasticSearch 动态数据访问(单个index+type)

  • ElasticSearch 动态数据访问(多个index+type)

  • 源代码托管

类关系图

  • 对多个数据源连接获取数据进行统一封装

  • ES spring-data方式不支持多个Index和Type的查找功能,添加了DynamicESDao支持

  • 大大简化封装之后的调用, 调用方式如下

attachments-2020-03-xl40rL0o5e6c3a0354a70.jpg

封装的一些配置

application.yml

  1. banner:

  2. charset: UTF-8

  3. location: classpath:banner.txt

  4. server:

  5. port:5555

  6. contextPath:/

  7. session:

  8. timeout:0

  9. spring:

  10. application:

  11. name:'spring-boot-datasource-demo'

  12. output:

  13. ansi:

  14. enabled: DETECT

  15. messages:

  16. basename: i18n/messages

  17. thymeleaf:

  18. cache:false

  19. profiles:

  20. active: dev


  21. #MySQL data source settings

  22. datasource:

  23. url: jdbc:mysql://localhost:3306/cdc_standalone?useSSL=false

  24. username: root

  25. password: bfXa4Pt2lUUScy8jakXf

  26. #MySQL JPA settings

  27. jpa:

  28. generate-ddl:true

  29. show-sql:true

  30. properties:

  31. hibernate:

  32. dialect: org.hibernate.dialect.MySQLDialect

  33. format_sql:true

  34. #NoSQL data source settings

  35. data:

  36. #MongoDB2.2+ settings

  37. mongodb:

  38. uri: mongodb://standalone:fhY1tPt1lpUSbS7jwkTf@10.11.60.4:27017/standalone

  39. #ElasticSearch settings

  40. elasticsearch:

  41. cluster-name: es-logs-01

  42. cluster-nodes:10.11.60.5:9300

pom.xml

  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3. <modelVersion>4.0.0</modelVersion>

  4. <groupId>spring-boot-datasource-demo</groupId>

  5. <artifactId>spring-boot-datasource-demo</artifactId>

  6. <version>0.0.1-SNAPSHOT</version>

  7. <name>spring-boot-datasource-demo</name>

  8. <description>spring-boot-datasource-demo</description>


  9. <parent>

  10. <groupId>org.springframework.boot</groupId>

  11. <artifactId>spring-boot-starter-parent</artifactId>

  12. <version>1.4.1.RELEASE</version>

  13. </parent>


  14. <properties>

  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  16. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  17. <java.version>1.8</java.version>

  18. <commons.lang.version>3.3.2</commons.lang.version>

  19. <springfox.version>2.7.0</springfox.version>

  20. </properties>


  21. <dependencies>

  22. <dependency>

  23. <groupId>org.springframework.boot</groupId>

  24. <artifactId>spring-boot-starter</artifactId>

  25. </dependency>

  26. <dependency>

  27. <groupId>org.springframework.boot</groupId>

  28. <artifactId>spring-boot-starter-data-mongodb</artifactId>

  29. </dependency>

  30. <dependency>

  31. <groupId>org.springframework.boot</groupId>

  32. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>

  33. </dependency>

  34. <dependency>

  35. <groupId>org.springframework.boot</groupId>

  36. <artifactId>spring-boot-starter-data-jpa</artifactId>

  37. </dependency>

  38. <dependency>

  39. <groupId>mysql</groupId>

  40. <artifactId>mysql-connector-java</artifactId>

  41. <scope>runtime</scope>

  42. </dependency>

  43. <dependency>

  44. <groupId>org.springframework.boot</groupId>

  45. <artifactId>spring-boot-starter-web</artifactId>

  46. </dependency>


  47. <dependency>

  48. <groupId>io.springfox</groupId>

  49. <artifactId>springfox-swagger2</artifactId>

  50. <version>${springfox.version}</version>

  51. </dependency>

  52. <dependency>

  53. <groupId>io.springfox</groupId>

  54. <artifactId>springfox-swagger-ui</artifactId>

  55. <version>${springfox.version}</version>

  56. </dependency>


  57. <dependency>

  58. <groupId>com.alibaba</groupId>

  59. <artifactId>fastjson</artifactId>

  60. <version>1.2.15</version>

  61. </dependency>

  62. <dependency>

  63. <groupId>org.apache.commons</groupId>

  64. <artifactId>commons-lang3</artifactId>

  65. <version>${commons.lang.version}</version>

  66. </dependency>

  67. <dependency>

  68. <groupId>org.apache.commons</groupId>

  69. <artifactId>commons-collections4</artifactId>

  70. <version>4.1</version>

  71. </dependency>


  72. <dependency>

  73. <groupId>com.github.wenhao</groupId>

  74. <artifactId>jpa-spec</artifactId>

  75. <version>3.2.3</version>

  76. </dependency>


  77. </dependencies>


  78. <build>

  79. <sourceDirectory>src</sourceDirectory>

  80. <plugins>

  81. <plugin>

  82. <artifactId>maven-compiler-plugin</artifactId>

  83. <configuration>

  84. <source>1.8</source>

  85. <target>1.8</target>

  86. </configuration>

  87. </plugin>

  88. </plugins>

  89. </build>

  90. </project>

封装后使用

封装之后使用将非常简单,公共的Service行为将被封装处理

MySQL 动态数据访问

通过几行代码即可实现对MySQL的访问,同时支持动态的条件查询;

User

  1. @Entity

  2. @Table(name ="tb_user")

  3. publicclassUserextendsBaseEntity{


  4. privatestaticfinallong serialVersionUID =1L;


  5. /**

  6. * 用户id

  7. */

  8. @Id

  9. @GeneratedValue(strategy =GenerationType.AUTO)

  10. @Column(name ="id", nullable =false)

  11. privateInteger id;

  12. privateString userName;

  13. privateString nickName;

  14. privateString password;

  15. privateInteger sex;

  16. privateString telephone;

  17. privateString email;

  18. privateString address;

  19. privateInteger deleteStatus;

  20. privateInteger locked =0;

  21. privateString description;


  22. @JSONField(format ="yyyy-MM-dd HH:mm:ss")

  23. privateDate createTime;

  24. @JSONField(format ="yyyy-MM-dd HH:mm:ss")

  25. privateDate updateTime;


  26. @ManyToMany(cascade ={CascadeType.REFRESH }, fetch =FetchType.LAZY)

  27. @JoinTable(name ="tb_user_role", joinColumns ={@JoinColumn(name ="user_id")}, inverseJoinColumns ={

  28. @JoinColumn(name ="role_id")})

  29. private java.util.Set<Role> roles;


  30. // getter & setter

  31. }


Dao


  1. @Repository

  2. publicinterfaceIUserDaoextendsIBaseJpaDao<User,Integer>{

  3. }

Service

  1. publicinterfaceIUserServiceextendsIBaseJpaService<User,Integer>{

  2. }

  3. @Service

  4. publicclassUserServiceImplextendsBaseJpaServiceImpl<User,Integer>implementsIUserService{

  5. @Autowired

  6. privateIUserDao userDao;

  7. @Override

  8. publicIBaseJpaDao<User,Integer> getBaseDao(){

  9. returnthis.userDao;

  10. }

  11. @Override

  12. publicvoiddelete(Integer id){

  13. User user = find(id);

  14. Assert.state(!"admin".equals(user.getUserName()),"超级管理员用户不能删除");

  15. super.delete(id);

  16. }

  17. }

Controller

  1. @RestController

  2. @RequestMapping("/admin/user")

  3. publicclassUserController{


  4. /**

  5. * user service

  6. */

  7. @Autowired

  8. privateIUserService userService;


  9. /**

  10. * @param searchText

    • 发表于 2020-03-14 09:56
    • 阅读 ( 1091 )
    • 分类:Java开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1135 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1474 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章