我是靠谱客的博主 爱笑小虾米,最近开发中收集的这篇文章主要介绍@manytomany 保存后数据被删除_Spring Boot骚操作多数据源Service层封装,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

原文:https://www.pdai.tech/md/spring/springboot-data-multi.html

mysql, es, mongodb 三个数据源用配置文件方式连接,JPA只是正对dao做了封装,本文主要介绍如何对service层进行封装。

  • Spring Boot - 多个数据源Service层封装

  • 类关系图

  • 封装的一些配置

    • application.yml

    • pom.xml

  • 封装后使用

    • MySQL 动态数据访问

  • Mongo 动态数据访问

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

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

  • 源代码托管

类关系图

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

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

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

5d069ebf992272fe2776a877960efe33.png

封装的一些配置

application.yml

banner:

charset: UTF-8

location: classpath:banner.txt

server:

port: 5555

contextPath: /

session:

timeout: 0

spring:

application:

name: 'spring-boot-datasource-demo'

output:

ansi:

enabled: DETECT

messages:

basename: i18n/messages

thymeleaf:

cache: false

profiles:

active: dev

# MySQL data source settings

datasource:

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

username: root

password: bfXa4Pt2lUUScy8jakXf

# MySQL JPA settings

jpa:

generate-ddl: true

show-sql: true

properties:

hibernate:

dialect: org.hibernate.dialect.MySQLDialect

format_sql: true

# NoSQL data source settings

data:

# MongoDB 2.2+ settings

mongodb:

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

# ElasticSearch settings

elasticsearch:

cluster-name: es-logs-01

cluster-nodes: 10.11.60.5:9300

pom.xml

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

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

4.0.0

spring-boot-datasource-demo

spring-boot-datasource-demo

0.0.1-SNAPSHOT

spring-boot-datasource-demo

spring-boot-datasource-demo

org.springframework.boot

spring-boot-starter-parent

1.4.1.RELEASE

UTF-8

UTF-8

1.8

3.3.2

2.7.0

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-data-mongodb

org.springframework.boot

spring-boot-starter-data-elasticsearch

org.springframework.boot

spring-boot-starter-data-jpa

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-web

io.springfox

springfox-swagger2

${springfox.version}

io.springfox

springfox-swagger-ui

${springfox.version}

com.alibaba

fastjson

1.2.15

org.apache.commons

commons-lang3

${commons.lang.version}

org.apache.commons

commons-collections4

4.1

com.github.wenhao

jpa-spec

3.2.3

src

maven-compiler-plugin

1.8

1.8

封装后使用

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

MySQL 动态数据访问

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

  • User

@Entity

@Table(name = "tb_user")

public class User extends BaseEntity {

private static final long serialVersionUID = 1L;

/**

* 用户id

*/

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

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

private Integer id;

private String userName;

private String nickName;

private String password;

private Integer sex;

private String telephone;

private String email;

private String address;

private Integer deleteStatus;

private Integer locked = 0;

private String description;

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

private Date createTime;

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

private Date updateTime;

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

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

@JoinColumn(name = "role_id") })

private java.util.Set<Role> roles;

// getter & setter

}

  • Dao

@Repository

public interface IUserDao extends IBaseJpaDao<User, Integer>{

}

  • Service

public interface IUserService extends IBaseJpaService<User, Integer> {

}

@Service

public class UserServiceImpl extends BaseJpaServiceImpl<User, Integer> implements IUserService {

@Autowired

private IUserDao userDao;

@Override

public IBaseJpaDao<User, Integer> getBaseDao() {

return this.userDao;

}

@Override

public void delete(Integer id) {

User user = find(id);

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

super.delete(id);

}

}

  • Controller

@RestController

@RequestMapping("/admin/user")

public class UserController {

/**

* user service

*/

@Autowired

private IUserService userService;

/**

* @param searchText

* @param user

* @param model

* @return

*/

@GetMapping(value = "/list")

public List<User> list(@RequestParam(value = "searchText", required = false, defaultValue="a") String searchText) {

return userService.findAll(Specifications.<User>and().like("userName", "%" + searchText + "%").build());

}

}

Mongo 动态数据访问

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

  • Entity

@Document(collection = "security_alert_rules")

public class EventRule extends BaseEntity {

private static final long serialVersionUID = -2013673868028645757L;

public static final int EVENT_STATUS_ENABLE = 0, EVENT_STATUS_DISABLE = 1;

private String component;

private String name;

private String eventId;

private String ciaLevel;

private String remarks;

private String script;

private long threshold;

private long timeWindow;

private String parseEsResultKeys;

private String nameCN;

private String remarkCN;

private String ruleType;

private String redisEventKey;

private int status = EVENT_STATUS_ENABLE;

// getter & setter

}

  • Dao

@Repository

public interface IEventRuleDao extends IBaseMongoDao<EventRule, String> {

}

  • Service

public interface IEventRuleService extends IBaseMongoService<EventRule, String> {

}

@Service

public class EventRuleServiceImpl extends BaseMongoServiceImpl<EventRule, String> implements IEventRuleService {

@Autowired

IEventRuleDao eventRuleDao;

@Override

public IBaseMongoDao<EventRule, String> getBaseDao() {

return eventRuleDao;

}

}

  • Controller

@RestController

@RequestMapping("/admin/eventRule")

public class EventRuleController {

/**

*/

@Autowired

private IEventRuleService eventRuleService;

/**

* @param searchText

* @param user

* @param model

* @return

*/

@GetMapping(value = "/list")

public List<EventRule> list(@RequestParam(value = "searchText", required = false) String searchText) {

EventRule param = new EventRule();

param.setName(searchText);

// Example rule = Example.of(param,

// ExampleMatcher.matching().withMatcher("name", ExampleMatcher.GenericPropertyMatchers.exact()));

Example<EventRule> rule = Example.<EventRule>of(param, ExampleMatcher.matching().withIgnoreCase("name","nameCN"));

return eventRuleService.findAll(rule);

}

}

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

通过几行代码即可实现对ElasticSearch的访问,同时支持动态的条件查询;适合数据类型比较固定,且index和type独立的,比如强类型映射的实体类;

  • Entity

@Document(indexName="syslog", type="logs")

public class SysLog extends BaseEntity {

private static final long serialVersionUID = -4491916941883088972L;

@Id

private String _id;

private Set<String> phyPorts = new LinkedHashSet<>();

private Set<String> ports = new LinkedHashSet<>();

private String sensor;

private int vlan;

private Set<String> ip = new LinkedHashSet<>();

private Set<String> mac = new LinkedHashSet<>();

private String description;

private String type;

private String vendor;

private long timestamp;

private String name;

private String chassisId;

// getter & setter

}

  • Dao

@Repository

public interface ISysLogDao extends IBaseESDao<SysLog, String> {

}

  • Service

public interface ILogService extends IBaseESService<SysLog, String> {

}

@Service

public class LogServiceImpl extends BaseESServiceImpl<SysLog, String> implements ILogService {

@Autowired

ISysLogDao sysLogDao;

@Override

public IBaseESDao<SysLog, String> getBaseDao() {

return sysLogDao;

}

}

  • Controller

@RestController

@RequestMapping("/admin/log")

public class LogController {

/**

* user service

*/

@Autowired

private ILogService logService;

/**

* @param searchText

* @param user

* @param model

* @return

*/

@GetMapping(value = "/list")

public Page<SysLog> list(@RequestParam(value = "searchText", required = false) String searchText) {

return logService.search(QueryBuilders.matchQuery("_all", searchText), new PageRequest(0, 100));

}

}

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

通过几行代码即可实现对ElasticSearch的访问,同时支持动态的条件查询;适合数据类型不固定,且index和type有多个,这些index具备相同结构类型,比如syslog-EVERY-DATE(由于日志量大,将每天的日志单独存放在一个Index中);

  • Entity

@Document(indexName="syslog", type="logs")

public class SysLog extends BaseEntity {

private static final long serialVersionUID = -4491916941883088972L;

@Id

private String _id;

private Set<String> phyPorts = new LinkedHashSet<>();

private Set<String> ports = new LinkedHashSet<>();

private String sensor;

private int vlan;

private Set<String> ip = new LinkedHashSet<>();

private Set<String> mac = new LinkedHashSet<>();

private String description;

private String type;

private String vendor;

private long timestamp;

private String name;

private String chassisId;

// getter & setter

}

  • Dao

public interface IDymLogDao extends IDynamicEsDao<SysLog, String> {

}

@Repository

public class DymLogDaoImpl extends SimpleDynamicEsDaoImpl<SysLog, String> implements IDymLogDao {

@Autowired

protected ElasticsearchTemplate elasticsearchTemplate;

@Override

public ElasticsearchOperations getElasticsearchOperations() {

return elasticsearchTemplate;

}

}

  • Service

public interface IDymLogService extends IDynamicESService<SysLog, String> {

}

@Service

public class DymLogServiceImpl extends DynamicESServiceImpl<SysLog, String> implements IDymLogService {

@Autowired

IDymLogDao sysLogDao;

@Override

public IDynamicEsDao<SysLog, String> getBaseDao() {

return sysLogDao;

}

}

  • Controller

@RestController

@RequestMapping("/admin/dymLog")

public class DymLogController {

/**

* logService

*/

@Autowired

private IDymLogService logService;

/**

* @param searchText

* @param user

* @param model

* @return

*/

@GetMapping(value = "/list")

public Page<SysLog> list(

@RequestParam(value = "searchText", required = false, defaultValue = "Siemens") String searchText) {

QueryBuilder queryBuilder = QueryBuilders.matchQuery("vendor", searchText);

return logService.search(new NativeSearchQueryBuilder().withIndices("syslog-2018-12-17").withTypes("logs")

.withQuery(queryBuilder).build());

}

}

源代码托管

https://github.com/realpdai/springboot-data-multidatasource-demo

6368100a55f1ea3b2f2bad2212e528a5.gif

Java8 的 Stream API 的确牛X,但性能究竟如何呢?一个接口查询关联了十几张表,响应速度太慢?那就提前把它们整合到一起请不要在 JDK 7+ 中使用这个 JSON 包了优秀!高级Java都这样优雅处理空值还在担心写的一手烂SQL,送你4款工具

推荐关注:Java老鸟Python学习日记

9141724ca8015ffaecb41c9c3de520b5.png

代码之外,亦是人生,从事区块链、java、大数据开发工程师的后花园笔记。记录学习python的每一步!

最后

以上就是爱笑小虾米为你收集整理的@manytomany 保存后数据被删除_Spring Boot骚操作多数据源Service层封装的全部内容,希望文章能够帮你解决@manytomany 保存后数据被删除_Spring Boot骚操作多数据源Service层封装所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部