Browse Source

----定时设备扫描状态

main
mengff 11 months ago
commit
0058180cc3
  1. 8
      .idea/.gitignore
  2. 13
      .idea/active-tab-highlighter.xml
  3. 18
      .idea/compiler.xml
  4. 6
      .idea/encodings.xml
  5. 20
      .idea/jarRepositories.xml
  6. 12
      .idea/misc.xml
  7. 6
      .idea/vcs.xml
  8. 3
      README.md
  9. 103
      device-status.iml
  10. 61
      device.sql
  11. 63
      pom.xml
  12. 22
      src/main/java/com/example/device/DeviceApplication.java
  13. 55
      src/main/java/com/example/device/controller/DeviceStatus.java
  14. 55
      src/main/java/com/example/device/entity/Device.java
  15. 74
      src/main/java/com/example/device/entity/Status.java
  16. 17
      src/main/java/com/example/device/mapper/DeviceMapper.java
  17. 18
      src/main/java/com/example/device/mapper/StatusMapper.java
  18. 23
      src/main/java/com/example/device/service/DeviceService.java
  19. 28
      src/main/java/com/example/device/service/StatusService.java
  20. 19
      src/main/resources/application.yml
  21. 16
      src/main/resources/mapping/DeviceMapper.xml
  22. 46
      src/main/resources/mapping/StatusMapper.xml

8
.idea/.gitignore

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

13
.idea/active-tab-highlighter.xml

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ActiveTabHighlighterConfiguration">
<option name="background">
<PersistentColor>
<option name="enabled" value="true" />
<option name="red" value="173" />
<option name="green" value="46" />
<option name="blue" value="156" />
</PersistentColor>
</option>
</component>
</project>

18
.idea/compiler.xml

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="device-status" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="device-status" options="-parameters" />
</option>
</component>
</project>

6
.idea/encodings.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>

20
.idea/jarRepositories.xml

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

12
.idea/misc.xml

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="18" project-jdk-type="JavaSDK" />
</project>

6
.idea/vcs.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

3
README.md

@ -0,0 +1,3 @@
# device-status
## **用于济菏高速设备定时获取其状态信息**

103
device-status.iml

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
<facet type="web" name="Web">
<configuration>
<webroots />
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.12.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.12.1" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.30" level="project" />
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.25" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.2" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.10.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.10.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.10.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.10.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.10.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.31" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.31" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.31" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-validation:2.2.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: jakarta.validation:jakarta.validation-api:2.0.2" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.validator:hibernate-validator:6.0.18.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.4.1.Final" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.5.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:2.1.1" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.5.3" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:2.0.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:8.0.19" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.2.5.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.2.5.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.2.5.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: jakarta.xml.bind:jakarta.xml.bind-api:2.3.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: jakarta.activation:jakarta.activation-api:1.2.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.13.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest:2.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.2.4.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.2.4.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.6.3" level="project" />
</component>
</module>

61
device.sql

@ -0,0 +1,61 @@
/*
Navicat Premium Data Transfer
Source Server : mysql8
Source Server Type : MySQL
Source Server Version : 80027
Source Host : 39.106.31.193:3307
Source Schema : device
Target Server Type : MySQL
Target Server Version : 80027
File Encoding : 65001
Date: 05/01/2024 09:34:02
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for device
-- ----------------------------
DROP TABLE IF EXISTS `device`;
CREATE TABLE `device` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '设备ID',
`device_no` varchar(30) COLLATE utf8_bin DEFAULT NULL COMMENT '设备编号',
`device_name` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '设备名称',
`device_ip` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '设备IP',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin;
-- ----------------------------
-- Records of device
-- ----------------------------
BEGIN;
INSERT INTO `device` VALUES (1, '1001', '千里眼', '192.168.1');
INSERT INTO `device` VALUES (2, '1002', '智慧眼', '192.168.2');
INSERT INTO `device` VALUES (3, '1003', '摄像机', '192.168.3');
INSERT INTO `device` VALUES (4, '1004', '随身行', '10.168.77.88');
COMMIT;
-- ----------------------------
-- Table structure for status
-- ----------------------------
DROP TABLE IF EXISTS `status`;
CREATE TABLE `status` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '设备状态ID',
`device_no` varchar(30) COLLATE utf8_bin DEFAULT NULL COMMENT '设备编号',
`device_name` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '设备名称',
`device_status` int DEFAULT NULL COMMENT '设备状态',
`time` datetime DEFAULT NULL COMMENT '插入时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=48 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin;
-- ----------------------------
-- Records of status
-- ----------------------------
BEGIN;
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;

63
pom.xml

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>device-status</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

22
src/main/java/com/example/device/DeviceApplication.java

@ -0,0 +1,22 @@
package com.example.device;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author mengff
* @Date 2024/01/04
*/
@EnableScheduling
@MapperScan("com.example.device.mapper") //扫描的mapper
@SpringBootApplication
public class DeviceApplication {
public static void main(String[] args) {
SpringApplication.run(DeviceApplication.class, args);
}
}

55
src/main/java/com/example/device/controller/DeviceStatus.java

@ -0,0 +1,55 @@
package com.example.device.controller;
import com.example.device.entity.Device;
import com.example.device.entity.Status;
import com.example.device.service.DeviceService;
import com.example.device.service.StatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.InetAddress;
import java.time.LocalDateTime;
import java.util.List;
@Component
@EnableScheduling
public class DeviceStatus {
@Autowired
private DeviceService deviceService;
@Autowired
private StatusService statusService;
//每天凌晨开始执行,每4小时执行一次
@Scheduled(cron = "0 0 4/0 * * ?")
public void generateDeviceStatus(){
deviceStatus();
}
public void deviceStatus() {
Status status=new Status();
List<Device> deviceList = deviceService.SelectList();
for (Device device : deviceList) {
try {
InetAddress address = InetAddress.getByName(device.getDeviceIp());
boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
status.setDeviceNo(device.getDeviceNo());
status.setDeviceName(device.getDeviceName());
LocalDateTime localDateTime = LocalDateTime.now();
status.setTime(localDateTime);
//1-在线 0-离线
if (reachable) {
status.setDeviceStatus(1);
} else {
status.setDeviceStatus(0);
}
statusService.Add(status);
} catch (IOException e) {
System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage());
}
}
}
}

55
src/main/java/com/example/device/entity/Device.java

@ -0,0 +1,55 @@
package com.example.device.entity;
/**
* @author mengff
* @Date 2020/03/03
*/
public class Device {
private long id;
public String getDeviceNo() {
return deviceNo;
}
public void setDeviceNo(String deviceNo) {
this.deviceNo = deviceNo;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getDeviceIp() {
return deviceIp;
}
@Override
public String toString() {
return "Device{" +
"id=" + id +
", deviceNo='" + deviceNo + '\'' +
", deviceName='" + deviceName + '\'' +
", deviceIp='" + deviceIp + '\'' +
'}';
}
public Device(long id, String deviceNo, String deviceName, String deviceIp) {
this.id = id;
this.deviceNo = deviceNo;
this.deviceName = deviceName;
this.deviceIp = deviceIp;
}
public void setDeviceIp(String deviceIp) {
this.deviceIp = deviceIp;
}
private String deviceNo;
private String deviceName;
private String deviceIp;
}

74
src/main/java/com/example/device/entity/Status.java

@ -0,0 +1,74 @@
package com.example.device.entity;
import java.time.LocalDateTime;
/**
* @author mengff
* @Date 2024/01/04
*/
public class Status {
public LocalDateTime getTime() {
return time;
}
public void setTime(LocalDateTime time) {
this.time = time;
}
public String getDeviceNo() {
return deviceNo;
}
public Status(long id, String deviceNo, String deviceName, int deviceStatus) {
this.id = id;
this.deviceNo = deviceNo;
this.deviceName = deviceName;
this.deviceStatus = deviceStatus;
}
public Status() {
}
public void setDeviceNo(String deviceNo) {
this.deviceNo = deviceNo;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public int getDeviceStatus() {
return deviceStatus;
}
public void setDeviceStatus(int deviceStatus) {
this.deviceStatus = deviceStatus;
}
private long id;
private String deviceNo;
@Override
public String toString() {
return "Status{" +
"id=" + id +
", deviceNo='" + deviceNo + '\'' +
", deviceName='" + deviceName + '\'' +
", deviceStatus=" + deviceStatus +
", time=" + time +
'}';
}
private String deviceName;
private int deviceStatus;
private LocalDateTime time;
}

17
src/main/java/com/example/device/mapper/DeviceMapper.java

@ -0,0 +1,17 @@
package com.example.device.mapper;
import com.example.device.entity.Device;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author mengff
* @Date 2024/01/04
*/
@Repository
public interface DeviceMapper {
List<Device> SelectList();
}

18
src/main/java/com/example/device/mapper/StatusMapper.java

@ -0,0 +1,18 @@
package com.example.device.mapper;
import com.example.device.entity.Status;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
* @author mengff
* @Date 2024/01/04
*/
@Repository
public interface StatusMapper {
int Add(@Param("status")Status status);
}

23
src/main/java/com/example/device/service/DeviceService.java

@ -0,0 +1,23 @@
package com.example.device.service;
import com.example.device.entity.Device;
import com.example.device.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author mengff
* @Date 2024/01/04
*/
@Service
public class DeviceService {
@Autowired
DeviceMapper deviceMapper;
public List<Device> SelectList() {
return deviceMapper.SelectList();
}
}

28
src/main/java/com/example/device/service/StatusService.java

@ -0,0 +1,28 @@
package com.example.device.service;
import com.example.device.entity.Status;
import com.example.device.mapper.StatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author mengff
* @Date 2024/01/04
*/
@Service
public class StatusService {
@Autowired
StatusMapper statusMapper;
public String Add(Status status) {
int a = statusMapper.Add(status);
if (a == 1) {
return "添加成功";
} else {
return "添加失败";
}
}
}

19
src/main/resources/application.yml

@ -0,0 +1,19 @@
server:
port: 8089
spring:
datasource:
username: root
password: root
url: jdbc:mysql://39.106.31.193:3307/device?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.example.entity.demo
logging:
level:
com:
example:
mapper : debug

16
src/main/resources/mapping/DeviceMapper.xml

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.device.mapper.DeviceMapper">
<resultMap id="BaseResultMap" type="com.example.device.entity.Device">
<result column="id" jdbcType="BIGINT" property="id"/>
<result column="device_no" jdbcType="VARCHAR" property="deviceNo"/>
<result column="device_name" jdbcType="VARCHAR" property="deviceName"/>
<result column="device_ip" jdbcType="VARCHAR" property="deviceIp"/>
</resultMap>
<select id="SelectList" resultType="com.example.device.entity.Device">
select * from device
</select>
</mapper>

46
src/main/resources/mapping/StatusMapper.xml

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.device.mapper.StatusMapper">
<resultMap id="BaseResultMap" type="com.example.device.entity.Status">
<result column="id" jdbcType="BIGINT" property="id"/>
<result column="device_no" jdbcType="VARCHAR" property="deviceNo"/>
<result column="device_name" jdbcType="VARCHAR" property="deviceName"/>
<result column="device_status" jdbcType="INTEGER" property="deviceStatus"/>
</resultMap>
<insert id="Add" parameterType="com.example.device.entity.Status">
INSERT INTO status
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="status.deviceNo != null">
device_no,
</if>
<if test="status.deviceName != null">
device_name,
</if>
<if test="status.deviceStatus != null">
device_status,
</if>
<if test="status.time != null">
time,
</if>
</trim>
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="status.deviceNo != null">
#{status.deviceNo,jdbcType=VARCHAR},
</if>
<if test="status.deviceName != null">
#{status.deviceName,jdbcType=VARCHAR},
</if>
<if test="status.deviceStatus != null">
#{status.deviceStatus,jdbcType=INTEGER},
</if>
<if test="status.time != null">
#{status.time,jdbcType=DATE},
</if>
</trim>
</insert>
</mapper>
Loading…
Cancel
Save