服务器之家

服务器之家 > 正文

springboot 实现mqtt物联网的示例代码

时间:2021-08-30 10:45     来源/作者:尽力漂亮

Springboot整合mybatisPlus+mysql+druid+swaggerUI+ mqtt 整合mqtt整合druid整合mybatis-plus完整pom完整yml整合swaggerUi整合log4j MQTT 物联网系统基本架构本物联网系列
mqtt)

整合mqtt

  1. <!--mqtt依赖-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-integration</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.integration</groupId>
  8. <artifactId>spring-integration-stream</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.integration</groupId>
  12. <artifactId>spring-integration-mqtt</artifactId>
  13. </dependency>

yml

  1. iot:
  2. mqtt:
  3. clientId: ${random.value}
  4. defaultTopic: topic
  5. shbykjTopic: shbykj_topic
  6. url: tcp://127.0.0.1:1883
  7. username: admin
  8. password: admin
  9. completionTimeout: 3000
  1. package com.shbykj.handle.mqtt;
  2.  
  3. import lombok.Getter;
  4. import lombok.Setter;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.integration.annotation.IntegrationComponentScan;
  7. import org.springframework.stereotype.Component;
  8.  
  9. /**
  10. * @Author: wxm
  11. * @Description: mqtt基础配置类
  12. */
  13.  
  14. @Getter
  15. @Setter
  16. @Component
  17. @IntegrationComponentScan
  18. @ConfigurationProperties(prefix = "iot.mqtt")
  19. public class BykjMqttConfig {
  20. /*
  21. *
  22. * 服务地址
  23. */
  24.  
  25. private String url;
  26.  
  27. /**
  28. * 客户端id
  29. */
  30.  
  31. private String clientId;
  32. /*
  33. *
  34. * 默认主题
  35. */
  36.  
  37. private String defaultTopic;
  38. /*
  39. *
  40. * 用户名和密码*/
  41.  
  42. private String username;
  43.  
  44. private String password;
  45.  
  46. /**
  47. * 超时时间
  48. */
  49. private int completionTimeout;
  50. /**
  51. * shbykj自定义主题
  52. */
  53. private String shbykjTopic;
  54.  
  55. }
  1. package com.shbykj.handle.mqtt.producer;
  2.  
  3. import org.springframework.integration.annotation.MessagingGateway;
  4. import org.springframework.integration.mqtt.support.MqttHeaders;
  5. import org.springframework.messaging.handler.annotation.Header;
  6.  
  7. /**
  8. * @description rabbitmq mqtt协议网关接口
  9. * @date 2020/6/8 18:26
  10. */
  11. @MessagingGateway(defaultRequestChannel = "iotMqttInputChannel")
  12. public interface IotMqttGateway {
  13.  
  14. void sendMessage2Mqtt(String data);
  15.  
  16. void sendMessage2Mqtt(String data, @Header(MqttHeaders.TOPIC) String topic);
  17.  
  18. void sendMessage2Mqtt(@Header(MqttHeaders.TOPIC) String topic, @Header(MqttHeaders.QOS) int qos, String payload);
  19. }
  1. package com.shbykj.handle.mqtt;
  2.  
  3. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.integration.annotation.ServiceActivator;
  10. import org.springframework.integration.channel.DirectChannel;
  11. import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
  12. import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
  13. import org.springframework.messaging.MessageChannel;
  14. import org.springframework.messaging.MessageHandler;
  15. import org.springframework.messaging.MessagingException;
  16.  
  17. @Configuration
  18. public class IotMqttProducerConfig {
  19. public final Logger logger = LoggerFactory.getLogger(this.getClass());
  20.  
  21. @Autowired
  22. private BykjMqttConfig mqttConfig;
  23. /*
  24. *
  25. * MQTT连接器选项
  26. * *
  27. */
  28.  
  29. @Bean(value = "getMqttConnectOptions")
  30. public MqttConnectOptions getMqttConnectOptions1() {
  31. MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
  32. // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
  33. mqttConnectOptions.setCleanSession(true);
  34. // 设置超时时间 单位为秒
  35. mqttConnectOptions.setConnectionTimeout(mqttConfig.getCompletionTimeout());
  36. mqttConnectOptions.setAutomaticReconnect(true);
  37. mqttConnectOptions.setUserName(mqttConfig.getUsername());
  38. mqttConnectOptions.setPassword(mqttConfig.getPassword().toCharArray());
  39. mqttConnectOptions.setServerURIs(new String[]{mqttConfig.getUrl()});
  40. // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送心跳判断客户端是否在线,但这个方法并没有重连的机制
  41. mqttConnectOptions.setKeepAliveInterval(10);
  42. // 设置“遗嘱”消息的话题,若客户端与服务器之间的连接意外中断,服务器将发布客户端的“遗嘱”消息。
  43. //mqttConnectOptions.setWill("willTopic", WILL_DATA, 2, false);
  44. return mqttConnectOptions;
  45. }
  46.  
  47. /**
  48. * mqtt工厂
  49. *
  50. * @return
  51. */
  52.  
  53. @Bean
  54. public MqttPahoClientFactory mqttClientFactory() {
  55. DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
  56. // factory.setServerURIs(mqttConfig.getServers());
  57. factory.setConnectionOptions(getMqttConnectOptions1());
  58. return factory;
  59.  
  60. }
  61.  
  62. @Bean
  63. public MessageChannel iotMqttInputChannel() {
  64. return new DirectChannel();
  65. }
  66.  
  67. // @Bean
  68. // @ServiceActivator(inputChannel = "iotMqttInputChannel")
  69. // public MessageHandler mqttOutbound() {
  70. // MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(mqttConfig.getClientId(), mqttClientFactory());
  71. // messageHandler.setAsync(false);
  72. // messageHandler.setDefaultQos(2);
  73. // messageHandler.setDefaultTopic(mqttConfig.getDefaultTopic());
  74. // return messageHandler;
  75. // }
  76. @Bean
  77. @ServiceActivator(inputChannel = "iotMqttInputChannel")
  78. public MessageHandler handlerTest() {
  79.  
  80. return message -> {
  81. try {
  82. String string = message.getPayload().toString();
  83.  
  84. System.out.println(string);
  85. } catch (MessagingException ex) {
  86. ex.printStackTrace();
  87. logger.info(ex.getMessage());
  88. }
  89. };
  90. }
  91. }
  1. package com.shbykj.handle.mqtt;
  2.  
  3. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.integration.annotation.ServiceActivator;
  10. import org.springframework.integration.channel.DirectChannel;
  11. import org.springframework.integration.core.MessageProducer;
  12. import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
  13. import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
  14. import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
  15. import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
  16. import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
  17. import org.springframework.messaging.Message;
  18. import org.springframework.messaging.MessageChannel;
  19. import org.springframework.messaging.MessageHandler;
  20. import org.springframework.messaging.MessagingException;
  21.  
  22. /**
  23. * @Author: xiaofu
  24. * @Description: 消息订阅配置
  25. * @date 2020/6/8 18:24
  26. */
  27. @Configuration
  28. public class IotMqttSubscriberConfig {
  29. public final Logger logger = LoggerFactory.getLogger(this.getClass());
  30.  
  31. @Autowired
  32. private MqttReceiveHandle mqttReceiveHandle;
  33. @Autowired
  34. private BykjMqttConfig mqttConfig;
  35. /*
  36. *
  37. * MQTT连接器选项
  38. * *
  39. */
  40.  
  41. @Bean(value = "getMqttConnectOptions")
  42. public MqttConnectOptions getMqttConnectOptions1() {
  43. MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
  44. // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
  45. mqttConnectOptions.setCleanSession(true);
  46. // 设置超时时间 单位为秒
  47. mqttConnectOptions.setConnectionTimeout(10);
  48. mqttConnectOptions.setAutomaticReconnect(true);
  49. // mqttConnectOptions.setUserName(mqttConfig.getUsername());
  50. // mqttConnectOptions.setPassword(mqttConfig.getPassword().toCharArray());
  51. mqttConnectOptions.setServerURIs(new String[]{mqttConfig.getUrl()});
  52. // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送心跳判断客户端是否在线,但这个方法并没有重连的机制
  53. mqttConnectOptions.setKeepAliveInterval(10);
  54. // 设置“遗嘱”消息的话题,若客户端与服务器之间的连接意外中断,服务器将发布客户端的“遗嘱”消息。
  55. //mqttConnectOptions.setWill("willTopic", WILL_DATA, 2, false);
  56. return mqttConnectOptions;
  57. }
  58. /*
  59. *
  60. *MQTT信息通道(生产者)
  61. **
  62. */
  63.  
  64. @Bean
  65. public MessageChannel iotMqttOutboundChannel() {
  66. return new DirectChannel();
  67. }
  68. /*
  69.  
  70. *
  71. *MQTT消息处理器(生产者)
  72. **
  73. */
  74.  
  75. @Bean
  76. @ServiceActivator(inputChannel = "iotMqttOutboundChannel")
  77. public MessageHandler mqttOutbound() {
  78. MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(mqttConfig.getClientId(), mqttClientFactory());
  79. messageHandler.setAsync(true);
  80. messageHandler.setDefaultTopic(mqttConfig.getDefaultTopic());
  81. return messageHandler;
  82. }
  83.  
  84. /*
  85. *
  86. *MQTT工厂
  87. **
  88. */
  89. @Bean
  90. public MqttPahoClientFactory mqttClientFactory() {
  91. DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
  92. // factory.setServerURIs(mqttConfig.getServers());
  93. factory.setConnectionOptions(getMqttConnectOptions1());
  94. return factory;
  95. }
  96.  
  97. /*
  98. *
  99. *MQTT信息通道(消费者)
  100. **
  101. */
  102. @Bean
  103. public MessageChannel iotMqttInputChannel() {
  104. return new DirectChannel();
  105. }
  106.  
  107. /**
  108. * 配置client,监听的topic
  109. * MQTT消息订阅绑定(消费者)
  110. ***/
  111.  
  112. @Bean
  113. public MessageProducer inbound() {
  114. MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(mqttConfig.getClientId(), mqttClientFactory(), mqttConfig.getDefaultTopic(), mqttConfig.getShbykjTopic());
  115. adapter.setCompletionTimeout(mqttConfig.getCompletionTimeout());
  116. adapter.setConverter(new DefaultPahoMessageConverter());
  117. adapter.setQos(2);
  118. adapter.setOutputChannel(iotMqttInputChannel());
  119. return adapter;
  120. }
  121.  
  122. /**
  123. * @author wxm
  124. * @description 消息订阅
  125. * @date 2020/6/8 18:20
  126. */
  127. @Bean
  128. @ServiceActivator(inputChannel = "iotMqttInputChannel")
  129.  
  130. public MessageHandler handler() {
  131. return new MessageHandler() {
  132. @Override
  133. public void handleMessage(Message<?> message) throws MessagingException {
  134. //处理接收消息
  135. try {
  136. mqttReceiveHandle.handle(message);
  137. } catch (Exception e) {
  138. logger.warn("消息处理异常"+e.getMessage());
  139. e.printStackTrace();
  140.  
  141. }
  142. }
  143. };
  144. }
  145. }
  1. package com.shbykj.handle.mqtt;
  2.  
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.shbykj.handle.common.DataCheck;
  5. import com.shbykj.handle.common.RedisKey;
  6. import com.shbykj.handle.common.RedisUtils;
  7. import com.shbykj.handle.common.constants.Constants;
  8. import com.shbykj.handle.common.model.ShbyCSDeviceEntity;
  9. import com.shbykj.handle.common.model.sys.SysInstrument;
  10. import com.shbykj.handle.resolve.mapper.SysInstrumentMapper;
  11. import com.shbykj.handle.resolve.util.DateUtils;
  12. import com.shbykj.handle.resolve.util.ShbyCSDeviceUtils;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.commons.collections.BidiMap;
  15. import org.apache.commons.collections.bidimap.DualHashBidiMap;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  18. import org.eclipse.paho.client.mqttv3.MqttCallback;
  19. import org.eclipse.paho.client.mqttv3.MqttMessage;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.integration.mqtt.support.MqttHeaders;
  25. import org.springframework.messaging.Message;
  26. import org.springframework.stereotype.Component;
  27. import org.springframework.transaction.annotation.Transactional;
  28.  
  29. import java.text.SimpleDateFormat;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33.  
  34. /*
  35. *
  36. * mqtt客户端消息处理类
  37. * **/
  38. @Component
  39. @Slf4j
  40. @Transactional
  41. public class MqttReceiveHandle implements MqttCallback {
  42. private static final Logger logger = LoggerFactory.getLogger(MqttReceiveHandle.class);
  43. @Value("${shbykj.checkCrc}")
  44. private boolean checkcrc;
  45. @Autowired
  46. private SysInstrumentMapper sysInstrumentMapper;
  47. @Autowired
  48. private RedisUtils redisUtils;
  49.  
  50. public static BidiMap bidiMap = new DualHashBidiMap();
  51. //记录bykj协议内容
  52. public static Map<String, Map<String, Object>> devMap = new HashMap();
  53.  
  54. //记录上限数量
  55. // public static Map<String, ChannelHandlerContext> ctxMap = new HashMap();
  56. public void handle(Message<?> message) {
  57.  
  58. try {
  59. logger.info("{},客户端号:{},主题:{},QOS:{},消息接收到的数据:{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), message.getHeaders().get(MqttHeaders.ID), message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC), message.getHeaders().get(MqttHeaders.RECEIVED_QOS), message.getPayload());
  60. //处理mqtt数据
  61. this.handle(message.getPayload().toString());
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. log.error("处理错误" + e.getMessage());
  65. }
  66.  
  67. }
  68.  
  69. private void handle(String str) throws Exception {
  70.  
  71. boolean flag = this.dataCheck(str);
  72. if (flag) {
  73.  
  74. ShbyCSDeviceEntity shbyCSDeviceEntity = ShbyCSDeviceUtils.convertToSysInstrumentEntity(str);
  75. String deviceNumber = shbyCSDeviceEntity.getPN();
  76. String smpId = shbyCSDeviceEntity.getSMP_ID();
  77. String smpName = shbyCSDeviceEntity.getSMP_NAME();
  78. String smpWt = shbyCSDeviceEntity.getSMP_WT();
  79. if (StringUtils.isEmpty(smpId) || StringUtils.isEmpty(smpName) || StringUtils.isEmpty(smpWt)) {
  80. log.error("过滤无实际作用报文信息", str);
  81. logger.error("过滤无实际作用报文信息", str);
  82. return;
  83. }
  84. //判断设备id是否存在数据库中,存在才进行数据部分处理
  85. //不存在就提醒需要添加设备:
  86. QueryWrapper<SysInstrument> wrapper = new QueryWrapper();
  87.  
  88. wrapper.eq("number", deviceNumber);
  89. wrapper.eq("is_deleted", Constants.NO);
  90. SysInstrument sysInstrument = sysInstrumentMapper.selectOne(wrapper);
  91. if (null == sysInstrument) {
  92. log.error("碳氧仪不存在或已删除,设备号:{}", deviceNumber);
  93. logger.error("碳氧仪不存在或已删除,设备号:{}", deviceNumber);
  94. return;
  95. }
  96.  
  97. try {
  98. //增加实时数据
  99. String instrumentId = sysInstrument.getId().toString();
  100. String realDataKey = RedisKey.CSdevice_DATA_KEY + instrumentId;
  101.  
  102. this.redisUtils.set(realDataKey, shbyCSDeviceEntity);
  103. System.out.println(shbyCSDeviceEntity);
  104. //通讯时间
  105. String onlineTime = "shbykj_mqtt:onlines:" + instrumentId;
  106. this.redisUtils.set(onlineTime, shbyCSDeviceEntity.getDataTime(), (long) Constants.RedisTimeOut.REAL_TIME_OUT);
  107. log.info("实时数据已经更新:设备主键id" + instrumentId);
  108. logger.info("{} 实时数据已经更新:设备主键id:{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),instrumentId);
  109.  
  110. } catch (Exception var1) {
  111. log.error("redis处理实时报文数据逻辑异常 :" + var1.getMessage());
  112. logger.error("redis处理实时报文数据逻辑异常 :" + var1.getMessage());
  113.  
  114. }
  115.  
  116. }
  117. }
  118.  
  119. private boolean dataCheck(String message) {
  120. boolean flag = DataCheck.receiverCheck(message);
  121. if (!flag) {
  122. return false;
  123. } else {
  124. int i = message.indexOf("QN=");
  125. if (i < 0) {
  126. log.warn("数据包中没有QN号码: " + message);
  127. logger.warn("数据包中没有QN号码: " + message);
  128. return false;
  129. } else {
  130. i = message.indexOf("PN=");
  131. if (i < 0) {
  132. log.warn("数据包中没有PN号码: " + message);
  133. logger.warn("数据包中没有PN号码: " + message);
  134. return false;
  135. } else {
  136. if (this.checkcrc) {
  137. flag = DataCheck.checkCrc(message);
  138. if (!flag) {
  139. log.warn("crc校验失败: " + message);
  140. logger.warn("数据包中没有PN号码: " + message);
  141. return false;
  142. }
  143. }
  144.  
  145. return true;
  146. }
  147. }
  148. }
  149. }
  150.  
  151. /**
  152. * 连接丢失
  153. *
  154. * @param throwable
  155. */
  156. @Override
  157. public void connectionLost(Throwable throwable) {
  158. logger.warn("连接丢失-客户端:{},原因:{}", throwable.getMessage());
  159.  
  160. }
  161.  
  162. /**
  163. * 消息已到达
  164. *
  165. * @param s
  166. * @param mqttMessage
  167. * @throws Exception
  168. */
  169. @Override
  170. public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
  171.  
  172. }
  173.  
  174. /**
  175. * 完成消息回调
  176. *
  177. * @param iMqttDeliveryToken
  178. */
  179. @Override
  180. public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
  181.  
  182. }
  183. }

整合druid

pom

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid-spring-boot-starter</artifactId>
  4. <version>1.1.10</version>
  5. </dependency>

druid-bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop.xsd">
  9.  
  10. <!-- 配置_Druid和Spring关联监控配置 -->
  11. <bean id="druid-stat-interceptor"
  12. class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>
  13.  
  14. <!-- 方法名正则匹配拦截配置 -->
  15. <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
  16. scope="prototype">
  17. <property name="patterns">
  18. <list>
  19. <value>com.shbykj.*.service.*.impl.*</value>
  20. </list>
  21. </property>
  22. </bean>
  23.  
  24. <aop:config proxy-target-class="true">
  25. <aop:advisor advice-ref="druid-stat-interceptor"
  26. pointcut-ref="druid-stat-pointcut" />
  27. </aop:config>
  28.  
  29. </beans>

yml

  1. #spring
  2. spring:
  3. main:
  4. allow-bean-definition-overriding: true
  5. # mysql DATABASE CONFIG
  6. datasource:
  7. druid:
  8. filters: stat,wall,log4j2
  9. continueOnError: true
  10. type: com.alibaba.druid.pool.DruidDataSource
  11. url: jdbc:mysql://localhost:3306/mqttdb?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowPublicKeyRetrieval=true
  12. username: root
  13. password: 123456
  14. driver-class-name: com.mysql.jdbc.Driver
  15. # see https://github.com/alibaba/druid
  16. initialSize: 15
  17. minIdle: 10
  18. maxActive: 200
  19. maxWait: 60000
  20. timeBetweenEvictionRunsMillis: 60000
  21. validationQuery: SELECT 1
  22. testWhileIdle: true
  23. testOnBorrow: false
  24. testOnReturn: false
  25. poolPreparedStatements: true
  26. keepAlive: true
  27. maxPoolPreparedStatementPerConnectionSize: 50
  28. connectionProperties:
  29. druid.stat.mergeSql: true
  30. druid.stat.slowSqlMillis: 5000

启动类加上注解@ImportResource( locations = {"classpath:druid-bean.xml"} )

springboot 实现mqtt物联网的示例代码

整合mybatis-plus

pom

  1. <!--mybatis-plus-->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>spring-wind</artifactId>
  5. <version>1.1.5</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>com.baomidou</groupId>
  9. <artifactId>mybatis-plus</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13.  
  14. <dependency>
  15. <groupId>com.baomidou</groupId>
  16. <version>3.1.2</version>
  17. <artifactId>mybatis-plus-boot-starter</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>mysql</groupId>
  21. <artifactId>mysql-connector-java</artifactId>
  22. <version>5.1.44</version>
  23. </dependency>
  24. <!--PageHelper分页插件-->
  25. <dependency>
  26. <groupId>com.github.pagehelper</groupId>
  27. <artifactId>pagehelper-spring-boot-starter</artifactId>
  28. <version>1.2.12</version>
  29. </dependency>

yml

  1. #mybatis
  2. mybatis-plus:
  3. mapper-locations: classpath:/mapper/*.xml
  4. typeAliasesPackage: org.spring.springboot.entity
  5. global-config:
  6. #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
  7. id-type: 3
  8. #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
  9. field-strategy: 2
  10. #驼峰下划线转换
  11. db-column-underline: true
  12. #刷新mapper 调试神器
  13. refresh-mapper: true
  14. configuration:
  15. map-underscore-to-camel-case: true
  16. cache-enabled: false

启动类注解@MapperScan({"com.shbykj.handle.resolve.mapper"})

完整pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.1</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.shbykj</groupId>
  12. <artifactId>handle</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>handle</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <!--注意: <scope>compile</scope> 这里是正式环境,解决启动报错-->
  23. <!--idea springboot启动报SLF4J:Failed to load class “org.slf4j.impl.StaticLoggerBinder-->
  24. <!--参考:https://blog.csdn.net/u010696630/article/details/84991116-->
  25. <dependency>
  26. <groupId>org.slf4j</groupId>
  27. <artifactId>slf4j-simple</artifactId>
  28. <version>1.7.25</version>
  29. <scope>compile</scope>
  30. </dependency>
  31. <!-- Log4j2 -->
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-log4j2</artifactId>
  35. </dependency>
  36.  
  37. <!--开启日志注解-->
  38. <dependency>
  39. <groupId>org.projectlombok</groupId>
  40. <artifactId>lombok</artifactId>
  41. </dependency>
  42. <!-- 排除 Spring-boot-starter 默认的日志配置 -->
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter</artifactId>
  46. <exclusions>
  47. <exclusion>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-starter-logging</artifactId>
  50. </exclusion>
  51. </exclusions>
  52. </dependency>
  53. <!--swagger api接口生成-->
  54. <dependency>
  55. <groupId>io.springfox</groupId>
  56. <artifactId>springfox-swagger-ui</artifactId>
  57. <version>2.9.2</version>
  58. </dependency>
  59.  
  60. <!-- 代码生成器的依赖 -->
  61. <dependency>
  62. <groupId>io.springfox</groupId>
  63. <artifactId>springfox-swagger2</artifactId>
  64. <version>2.9.2</version>
  65. <exclusions>
  66. <exclusion>
  67. <groupId>com.google.guava</groupId>
  68. <artifactId>guava</artifactId>
  69. </exclusion>
  70. </exclusions>
  71. </dependency>
  72. <!--mybatis-plus-->
  73. <dependency>
  74. <groupId>com.baomidou</groupId>
  75. <artifactId>spring-wind</artifactId>
  76. <version>1.1.5</version>
  77. <exclusions>
  78. <exclusion>
  79. <groupId>com.baomidou</groupId>
  80. <artifactId>mybatis-plus</artifactId>
  81. </exclusion>
  82. </exclusions>
  83. </dependency>
  84.  
  85. <dependency>
  86. <groupId>com.baomidou</groupId>
  87. <version>3.1.2</version>
  88. <artifactId>mybatis-plus-boot-starter</artifactId>
  89. </dependency>
  90. <dependency>
  91. <groupId>mysql</groupId>
  92. <artifactId>mysql-connector-java</artifactId>
  93. <version>5.1.44</version>
  94. </dependency>
  95. <dependency>
  96. <groupId>com.alibaba</groupId>
  97. <artifactId>druid-spring-boot-starter</artifactId>
  98. <version>1.1.10</version>
  99. </dependency>
  100. <!--PageHelper分页插件-->
  101. <dependency>
  102. <groupId>com.github.pagehelper</groupId>
  103. <artifactId>pagehelper-spring-boot-starter</artifactId>
  104. <version>1.2.12</version>
  105. </dependency>
  106. <!--devtools热部署-->
  107. <dependency>
  108. <groupId>org.springframework.boot</groupId>
  109. <artifactId>spring-boot-devtools</artifactId>
  110. <optional>true</optional>
  111. <scope>runtime</scope>
  112. </dependency>
  113. <!--json转换工具-->
  114. <dependency>
  115. <groupId>com.google.code.gson</groupId>
  116. <artifactId>gson</artifactId>
  117. </dependency>
  118. <!-- redis -->
  119. <dependency>
  120. <groupId>org.springframework.boot</groupId>
  121. <artifactId>spring-boot-starter-data-redis</artifactId>
  122. </dependency>
  123. <!--工具类-->
  124. <dependency>
  125. <groupId>org.apache.commons</groupId>
  126. <artifactId>commons-lang3</artifactId>
  127. <version>3.8.1</version>
  128. </dependency>
  129. <!--google-->
  130. <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
  131. <dependency>
  132. <groupId>com.google.guava</groupId>
  133. <artifactId>guava</artifactId>
  134. <version>30.0-jre</version>
  135. </dependency>
  136. <!-- 工具类库 -->
  137. <dependency>
  138. <groupId>cn.hutool</groupId>
  139. <artifactId>hutool-core</artifactId>
  140. <version>5.5.0</version>
  141. </dependency>
  142.  
  143. <!--lombok-->
  144. <dependency>
  145. <groupId>org.projectlombok</groupId>
  146. <artifactId>lombok</artifactId>
  147. <optional>true</optional>
  148. </dependency>
  149.  
  150. <dependency>
  151. <groupId>org.springframework.boot</groupId>
  152. <artifactId>spring-boot-starter-test</artifactId>
  153. <scope>test</scope>
  154. </dependency>
  155. </dependencies>
  156.  
  157. <build>
  158. <plugins>
  159. <plugin>
  160. <groupId>org.springframework.boot</groupId>
  161. <artifactId>spring-boot-maven-plugin</artifactId>
  162. </plugin>
  163. </plugins>
  164. </build>
  165.  
  166. </project>

完整yml

  1. server:
  2. port: 8082
  3. #spring
  4. spring:
  5. devtools:
  6. restart:
  7. enabled: true
  8. main:
  9. allow-bean-definition-overriding: true
  10. # mysql DATABASE CONFIG
  11. datasource:
  12. druid:
  13. filters: stat,wall,log4j2
  14. continueOnError: true
  15. type: com.alibaba.druid.pool.DruidDataSource
  16. url: jdbc:mysql://localhost:3306/mqttdb?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowPublicKeyRetrieval=true
  17. username: root
  18. password: 123456
  19. driver-class-name: com.mysql.jdbc.Driver
  20. # see https://github.com/alibaba/druid
  21. initialSize: 15
  22. minIdle: 10
  23. maxActive: 200
  24. maxWait: 60000
  25. timeBetweenEvictionRunsMillis: 60000
  26. validationQuery: SELECT 1
  27. testWhileIdle: true
  28. testOnBorrow: false
  29. testOnReturn: false
  30. poolPreparedStatements: true
  31. keepAlive: true
  32. maxPoolPreparedStatementPerConnectionSize: 50
  33. connectionProperties:
  34. druid.stat.mergeSql: true
  35. druid.stat.slowSqlMillis: 5000
  36. shbykj:
  37. checkCrc: false
  38. #mybatis
  39. mybatis-plus:
  40. mapper-locations: classpath:/mapper/*.xml
  41. typeAliasesPackage: org.spring.springboot.entity
  42. global-config:
  43. #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
  44. id-type: 3
  45. #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
  46. field-strategy: 2
  47. #驼峰下划线转换
  48. db-column-underline: true
  49. #刷新mapper 调试神器
  50. refresh-mapper: true
  51. configuration:
  52. map-underscore-to-camel-case: true
  53. cache-enabled: false
  54. #logging
  55. logging:
  56. config: classpath:log4j2-demo.xml

整合swaggerUi

pom

  1. <!--swagger api接口生成-->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger-ui</artifactId>
  5. <version>2.9.2</version>
  6. </dependency>
  7. <!--解决报错:swagger:Illegal DefaultValue null for parameter type integer. java.lang.NumberFormatException: For input string: "".-->
  8. <!--1.5.21的AbstractSerializableParameter.getExample()方法增加了对空字符串的判断-->
  9. <dependency>
  10. <groupId>io.swagger</groupId>
  11. <artifactId>swagger-models</artifactId>
  12. <version>1.5.21</version>
  13. </dependency>
  14. <!-- 代码生成器的依赖 -->
  15. <dependency>
  16. <groupId>io.springfox</groupId>
  17. <artifactId>springfox-swagger2</artifactId>
  18. <version>2.9.2</version>
  19. <exclusions>
  20. <exclusion>
  21. <groupId>com.google.guava</groupId>
  22. <artifactId>guava</artifactId>
  23. </exclusion>
  24. </exclusions>
  25. </dependency>

使用

  1. package com.shbykj.handle.web.wx;
  2.  
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.shbykj.handle.common.RetMsgData;
  5. import com.shbykj.handle.common.State;
  6. import com.shbykj.handle.common.model.sys.SysInstrument;
  7. import com.shbykj.handle.h.service.ISysInstrumentService;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiImplicitParam;
  10. import io.swagger.annotations.ApiImplicitParams;
  11. import io.swagger.annotations.ApiOperation;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.bind.annotation.RestController;
  17.  
  18. /**
  19. * 监测点接口
  20. *
  21. * @author
  22. * @date 2021-01-15 16:49
  23. */
  24. @RestController
  25. @RequestMapping({"/api/wxapoint"})
  26. @Api(
  27. tags = {"小程序 监测点接口"}
  28. )
  29. public class CSDevicesController extends BaseController {
  30. @Autowired
  31. private ISysInstrumentService sysInstrumentService;
  32.  
  33. public CSDevicesController() {
  34. }
  35.  
  36. @ApiOperation(
  37. value = "分页查询",
  38. notes = "分页查询站点信息"
  39. )
  40. @ApiImplicitParams({@ApiImplicitParam(
  41. name = "number",
  42. value = "设备编号",
  43. paramType = "query",
  44. dataType = "String"
  45. ), @ApiImplicitParam(
  46. name = "page",
  47. value = "页码 从1开始",
  48. required = false,
  49. dataType = "long",
  50. paramType = "query"
  51. ), @ApiImplicitParam(
  52. name = "size",
  53. value = "页数",
  54. required = false,
  55. dataType = "long",
  56. paramType = "query"
  57. )})
  58. @GetMapping({"/pageByNumber"})
  59. public RetMsgData<IPage<SysInstrument>> pageByNumber(@RequestParam(required = false) String number) {
  60. RetMsgData msg = new RetMsgData();
  61.  
  62. try {
  63. IPage<SysInstrument> page1 = this.getPage();
  64. page1 = sysInstrumentService.pageByNumber(number, page1);
  65.  
  66. msg.setData(page1);
  67. } catch (Exception var5) {
  68. msg.setState(State.RET_STATE_SYSTEM_ERROR);
  69. this.logger.error(var5.getMessage());
  70. }
  71.  
  72. return msg;
  73. }
  74. }
  1. package com.shbykj.handle.common.model.sys;
  2.  
  3. import com.baomidou.mybatisplus.annotation.IdType;
  4. import com.baomidou.mybatisplus.annotation.TableField;
  5. import com.baomidou.mybatisplus.annotation.TableId;
  6. import com.baomidou.mybatisplus.annotation.TableName;
  7. import io.swagger.annotations.ApiModel;
  8. import io.swagger.annotations.ApiModelProperty;
  9. import java.io.Serializable;
  10. import java.util.Date;
  11.  
  12. @TableName("instrument")
  13. @ApiModel("仪器配置表字段信息")
  14. public class SysInstrument implements Serializable {
  15. private static final long serialVersionUID = 1L;
  16. @TableId(
  17. value = "id",
  18. type = IdType.AUTO
  19. )
  20. @ApiModelProperty(
  21. value = "id",
  22. name = "id",
  23. required = true
  24. )
  25. private Long id;
  26. @TableField("name")
  27. @ApiModelProperty(
  28. value = "名称 仪器名称",
  29. name = "name"
  30. )
  31. private String name;
  32. @TableField("number")
  33. @ApiModelProperty(
  34. value = "编号 仪器编号(PN)",
  35. name = "number"
  36. )
  37. private String number;
  38. @TableField("manufacturer")
  39. @ApiModelProperty(
  40. value = "生产厂商 生产厂商",
  41. name = "manufacturer"
  42. )
  43. private String manufacturer;
  44. @TableField("gmt_create")
  45. @ApiModelProperty(
  46. value = "创建时间",
  47. name = "gmt_create"
  48. )
  49. private Date gmtCreate;
  50. @TableField("gmt_modified")
  51. @ApiModelProperty(
  52. value = "更新时间",
  53. name = "gmt_modified"
  54. )
  55. private Date gmtModified;
  56. @TableField("is_deleted")
  57. @ApiModelProperty(
  58. value = "表示删除,0 表示未删除 默认0",
  59. name = "is_deleted"
  60. )
  61. private Integer isDeleted;
  62. @TableField("device_type")
  63. @ApiModelProperty(
  64. value = "设备类型(PT)",
  65. name = "device_type"
  66. )
  67. private String deviceType;
  68.  
  69. public SysInstrument() {
  70. }
  71.  
  72. public Long getId() {
  73. return this.id;
  74. }
  75.  
  76. public String getName() {
  77. return this.name;
  78. }
  79.  
  80. public String getNumber() {
  81. return this.number;
  82. }
  83.  
  84. public String getManufacturer() {
  85. return this.manufacturer;
  86. }
  87.  
  88. public Date getGmtCreate() {
  89. return this.gmtCreate;
  90. }
  91.  
  92. public Date getGmtModified() {
  93. return this.gmtModified;
  94. }
  95.  
  96. public Integer getIsDeleted() {
  97. return this.isDeleted;
  98. }
  99.  
  100. public String getDeviceType() {
  101. return this.deviceType;
  102. }
  103.  
  104. public void setId(final Long id) {
  105. this.id = id;
  106. }
  107.  
  108. public void setName(final String name) {
  109. this.name = name;
  110. }
  111.  
  112. public void setNumber(final String number) {
  113. this.number = number;
  114. }
  115.  
  116. public void setManufacturer(final String manufacturer) {
  117. this.manufacturer = manufacturer;
  118. }
  119.  
  120. public void setGmtCreate(final Date gmtCreate) {
  121. this.gmtCreate = gmtCreate;
  122. }
  123.  
  124. public void setGmtModified(final Date gmtModified) {
  125. this.gmtModified = gmtModified;
  126. }
  127.  
  128. public void setIsDeleted(final Integer isDeleted) {
  129. this.isDeleted = isDeleted;
  130. }
  131.  
  132. public void setDeviceType(final String deviceType) {
  133. this.deviceType = deviceType;
  134. }
  135.  
  136. public String toString() {
  137. return "SysInstrument(id=" + this.getId() + ", name=" + this.getName() + ", number=" + this.getNumber() + ", manufacturer=" + this.getManufacturer() + ", gmtCreate=" + this.getGmtCreate() + ", gmtModified=" + this.getGmtModified() + ", isDeleted=" + this.getIsDeleted() + ", deviceType=" + this.getDeviceType() + ")";
  138. }
  139. }

整合log4j

http://www.zzvips.com/article/152599.htm

MQTT 物联网系统基本架构

pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.shbykj</groupId>
  12. <artifactId>handle_mqtt</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>handle_mqtt</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <skipTests>true</skipTests>
  19. </properties>
  20. <dependencies>
  21. <!--mqtt依赖-->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-integration</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.integration</groupId>
  28. <artifactId>spring-integration-stream</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.integration</groupId>
  32. <artifactId>spring-integration-mqtt</artifactId>
  33. </dependency>
  34.  
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-web</artifactId>
  38. <exclusions>
  39. <exclusion>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-logging</artifactId>
  42. </exclusion>
  43. </exclusions>
  44. </dependency>
  45. <!--注意: <scope>compile</scope> 这里是正式环境,解决启动报错-->
  46. <!--idea springboot启动报SLF4J:Failed to load class “org.slf4j.impl.StaticLoggerBinder-->
  47. <!--参考:https://blog.csdn.net/u010696630/article/details/84991116-->
  48. <dependency>
  49. <groupId>org.slf4j</groupId>
  50. <artifactId>slf4j-simple</artifactId>
  51. <version>1.7.25</version>
  52. <scope>compile</scope>
  53. </dependency>
  54. <!-- Log4j2 -->
  55. <dependency>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-starter-log4j2</artifactId>
  58. </dependency>
  59.  
  60. <!-- 排除 Spring-boot-starter 默认的日志配置 -->
  61. <dependency>
  62. <groupId>org.springframework.boot</groupId>
  63. <artifactId>spring-boot-starter</artifactId>
  64. <exclusions>
  65. <exclusion>
  66. <groupId>org.springframework.boot</groupId>
  67. <artifactId>spring-boot-starter-logging</artifactId>
  68. </exclusion>
  69. </exclusions>
  70. </dependency>
  71. <!--swagger api接口生成-->
  72. <dependency>
  73. <groupId>io.springfox</groupId>
  74. <artifactId>springfox-swagger-ui</artifactId>
  75. <version>2.9.2</version>
  76. </dependency>
  77. <!--解决报错:swagger:Illegal DefaultValue null for parameter type integer. java.lang.NumberFormatException: For input string: "".-->
  78. <!--1.5.21的AbstractSerializableParameter.getExample()方法增加了对空字符串的判断-->
  79. <dependency>
  80. <groupId>io.swagger</groupId>
  81. <artifactId>swagger-models</artifactId>
  82. <version>1.5.21</version>
  83. </dependency>
  84. <!-- 代码生成器的依赖 -->
  85. <dependency>
  86. <groupId>io.springfox</groupId>
  87. <artifactId>springfox-swagger2</artifactId>
  88. <version>2.9.2</version>
  89. <exclusions>
  90. <exclusion>
  91. <groupId>com.google.guava</groupId>
  92. <artifactId>guava</artifactId>
  93. </exclusion>
  94. </exclusions>
  95. </dependency>
  96.  
  97. <!--其他工具-->
  98. <!--devtools热部署-->
  99. <dependency>
  100. <groupId>org.springframework.boot</groupId>
  101. <artifactId>spring-boot-devtools</artifactId>
  102. <optional>true</optional>
  103. <scope>runtime</scope>
  104. </dependency>
  105. <!--json转换工具-->
  106. <dependency>
  107. <groupId>com.google.code.gson</groupId>
  108. <artifactId>gson</artifactId>
  109. </dependency>
  110. <!-- redis -->
  111. <dependency>
  112. <groupId>org.springframework.boot</groupId>
  113. <artifactId>spring-boot-starter-data-redis</artifactId>
  114. </dependency>
  115. <!--工具类-->
  116. <dependency>
  117. <groupId>org.apache.commons</groupId>
  118. <artifactId>commons-lang3</artifactId>
  119. <version>3.8.1</version>
  120. </dependency>
  121. <!--google-->
  122. <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
  123. <dependency>
  124. <groupId>com.google.guava</groupId>
  125. <artifactId>guava</artifactId>
  126. <version>30.0-jre</version>
  127. </dependency>
  128. <!-- 工具类库 -->
  129. <dependency>
  130. <groupId>cn.hutool</groupId>
  131. <artifactId>hutool-core</artifactId>
  132. <version>5.5.0</version>
  133. </dependency>
  134.  
  135. <!--lombok-->
  136. <dependency>
  137. <groupId>org.projectlombok</groupId>
  138. <artifactId>lombok</artifactId>
  139. <optional>true</optional>
  140. </dependency>
  141. <!--工具类-->
  142. <dependency>
  143. <groupId>commons-collections</groupId>
  144. <artifactId>commons-collections</artifactId>
  145. <version>3.2</version>
  146. </dependency>
  147. <dependency>
  148. <groupId>com.baomidou</groupId>
  149. <artifactId>spring-wind</artifactId>
  150. <version>1.1.5</version>
  151. <exclusions>
  152. <exclusion>
  153. <groupId>com.baomidou</groupId>
  154. <artifactId>mybatis-plus</artifactId>
  155. </exclusion>
  156. </exclusions>
  157. </dependency>
  158.  
  159. <dependency>
  160. <groupId>com.baomidou</groupId>
  161. <version>3.1.2</version>
  162. <artifactId>mybatis-plus-boot-starter</artifactId>
  163. </dependency>
  164. <dependency>
  165. <groupId>mysql</groupId>
  166. <artifactId>mysql-connector-java</artifactId>
  167. <version>5.1.44</version>
  168. </dependency>
  169. <dependency>
  170. <groupId>com.alibaba</groupId>
  171. <artifactId>druid-spring-boot-starter</artifactId>
  172. <version>1.1.10</version>
  173. </dependency>
  174. <!--PageHelper分页插件-->
  175. <dependency>
  176. <groupId>com.github.pagehelper</groupId>
  177. <artifactId>pagehelper-spring-boot-starter</artifactId>
  178. <version>1.2.12</version>
  179. </dependency>
  180. <dependency>
  181. <groupId>org.springframework.boot</groupId>
  182. <artifactId>spring-boot-starter-test</artifactId>
  183. <scope>test</scope>
  184. </dependency>
  185.  
  186. </dependencies>
  187.  
  188. <build>
  189. <plugins>
  190. <plugin>
  191. <groupId>org.springframework.boot</groupId>
  192. <artifactId>spring-boot-maven-plugin</artifactId>
  193. <executions>
  194. <execution>
  195. <goals>
  196. <goal>repackage</goal>
  197. </goals>
  198. </execution>
  199. </executions>
  200. </plugin>
  201. </plugins>
  202.  
  203. </build>
  204.  
  205. </project>

yml

  1. server:
  2. port: 8082
  3. iot:
  4. mqtt:
  5. clientId: ${random.value}
  6. defaultTopic: topic
  7. shbykjTopic: shbykj_topic
  8. url: tcp://127.0.0.1:1883
  9. username: admin
  10. password: admin
  11. completionTimeout: 3000
  12. #微信小程序相关参数
  13. shbykjWeixinAppid: wxae343ca8948f97c4
  14. shbykjSecret: 9e168c92702efc06cb12fa22680f049a
  15.  
  16. #spring
  17. spring:
  18. devtools:
  19. restart:
  20. enabled: true
  21. main:
  22. allow-bean-definition-overriding: true
  23. # mysql DATABASE CONFIG
  24. datasource:
  25. druid:
  26. filters: stat,wall,log4j2
  27. continueOnError: true
  28. type: com.alibaba.druid.pool.DruidDataSource
  29. url: jdbc:mysql://localhost:3306/mqttdb?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowPublicKeyRetrieval=true
  30. username: root
  31. password: 123456
  32. driver-class-name: com.mysql.jdbc.Driver
  33. # see https://github.com/alibaba/druid
  34. initialSize: 15
  35. minIdle: 10
  36. maxActive: 200
  37. maxWait: 60000
  38. timeBetweenEvictionRunsMillis: 60000
  39. validationQuery: SELECT 1
  40. testWhileIdle: true
  41. testOnBorrow: false
  42. testOnReturn: false
  43. poolPreparedStatements: true
  44. keepAlive: true
  45. maxPoolPreparedStatementPerConnectionSize: 50
  46. connectionProperties:
  47. druid.stat.mergeSql: true
  48. druid.stat.slowSqlMillis: 5000
  49.  
  50. shbykj:
  51. checkCrc: false
  52. #mybatis
  53. mybatis-plus:
  54. mapper-locations: classpath:/mapper/*.xml
  55. typeAliasesPackage: org.spring.springboot.entity
  56. global-config:
  57. #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
  58. id-type: 3
  59. #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
  60. field-strategy: 2
  61. #驼峰下划线转换
  62. db-column-underline: true
  63. #刷新mapper 调试神器
  64. refresh-mapper: true
  65. configuration:
  66. map-underscore-to-camel-case: true
  67. cache-enabled: false
  68. #log4j打印sql日志
  69. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  70. #logging
  71. logging:
  72. config: classpath:log4j2-demo.xml

到此这篇关于springboot 实现mqtt物联网的文章就介绍到这了,更多相关springboot 实现mqtt物联网内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_44106334/article/details/112658337

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部