springboot+mybatis+mysql+influxdb+thymeleaf_mybatis支持influxdb吗_qq_34665828的博客-程序员宅基地

技术标签: java  go  

项目目录

pom配置文件

<?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.4.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zzedu</groupId>
    <artifactId>go-fwd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>go-fwd</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-actuator</artifactId>
        </dependency>
        <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.4</version>
        </dependency>
        <!-- 阿里巴巴json包 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.69</version>
        </dependency>
        <dependency>
            <groupId>org.influxdb</groupId>
            <artifactId>influxdb-java</artifactId>
            <version>2.15</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.web.resources.chain.strategy.content.enabled=true
spring.web.resources.chain.strategy.content.paths=/**
#mySql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://10.102.110.100:3306/go-test
spring.datasource.username=root
spring.datasource.password=zzutscMYSQL
#influxdb
spring.influx.url=http://10.102.110.93:8086
spring.influx.user=lbw
spring.influx.password=123
spring.influx.database=test_db2
spring.influx.retention_policy=default
spring.influx.retention_policy_time=30d

model文件夹下的port

package com.zzedu.gofwd.model;

import lombok.Data;

@Data
public class Port {
    private int id;
    private String ip;
    private String port;
    private int flag;
    private String bz;
}

mapper文件夹下的PortMapper

package com.zzedu.gofwd.mapper;

import com.zzedu.gofwd.model.Port;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface PortMapper {
    @Select("select * from port where ip=#{ip} and flag=0 limit 1")
    Port getPortById(String ip);
    @Select("select * from port where port=#{port}")
    Port getPortInfo(String port);
    @Select("update port set flag=1 where id=#{id}")
    void updatePortById(long id);
    @Insert("INSERT INTO port (ip,port,flag,bz) VALUES (#{ip},#{port},#{flag},#{bz})")
    void add(Port port);
}

controller文件夹下的portController

package com.zzedu.gofwd.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zzedu.gofwd.common.InfluxDBConnect;
import com.zzedu.gofwd.mapper.PortMapper;
import com.zzedu.gofwd.model.Port;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller

public class PortController {
    @Resource
    InfluxDBConnect influxDBConnect;
    @Autowired
    private PortMapper portMapper;

    @RequestMapping("/insertPortShow")
    public String insertPortShow(){
        return "insertport";
    }
    @RequestMapping("/insertPort")
    public String insertPort(Port port){
        portMapper.add(port);
        return "insertport";
    }
    @RequestMapping("/getPort")
    @ResponseBody
    public Port getAllUsers(String ip){
        return portMapper.getPortById(ip);
    }
    @RequestMapping("/getPortInfo")
    @ResponseBody
    public Port getPortInfo(String port){
        return portMapper.getPortInfo(port);
    }
    @RequestMapping("/updatePort")
    @ResponseBody
    public String updateAllUsers(Long id){
        portMapper.updatePortById(id);
        return "success";
    }
    @RequestMapping("/inserPortInfo")
    @ResponseBody
    public void testInsert(@RequestBody String temp) {
        JSONObject obj=JSON.parseObject(temp);
        Map<String, String> tagsMap = new HashMap<>();
        Map<String, Object> fieldsMap = new HashMap<>();
        Iterator it =obj.getJSONObject("fields").entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            fieldsMap.put(entry.getKey(), entry.getValue());
        }
        Iterator it2 =obj.getJSONObject("tag").entrySet().iterator();
        while (it2.hasNext()) {
            Map.Entry<String, Object> entry2 = (Map.Entry<String, Object>) it2.next();
            tagsMap.put(entry2.getKey(), (String) entry2.getValue());
        }
        System.out.println("influxDB start time :" + System.currentTimeMillis());
        influxDBConnect.insert("usage", tagsMap, fieldsMap);
    }
    public void testQuery(){
        QueryResult result = influxDBConnect.query("select * from usage limit 2");
        System.out.println(result.getResults());

    }
}

common文件夹InfluxDBConnect

package com.zzedu.gofwd.common;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class InfluxDBConnect {
    /**
     * 用户名
     */
    private String userName;
    private String password;
    private String url;
    public String database;
    /**
     * 数据保存策略
     */
    private String retentionPolicy;
    /**
     * 数据保存策略中数据保存时间
     */
    private String retentionPolicyTime;

    /**
     * InfluxDB实例
     */
    private InfluxDB influxDB;
    public InfluxDBConnect(String userName, String password, String influxDBUrl, String database, String retentionPolicy, String retentionPolicyTime) {
        this.userName = userName;
        this.password = password;
        this.url = influxDBUrl;
        this.database = database;
        // autogen默认的数据保存策略
        this.retentionPolicy = retentionPolicy == null || "".equals(retentionPolicy) ? "autogen" : retentionPolicy;
        this.retentionPolicyTime = retentionPolicyTime == null || "".equals(retentionPolicy) ? "30d" : retentionPolicyTime;
        this.influxDB = influxDbBuild();
    }
    /**
     * 连接时序数据库;获得InfluxDB
     **/
    public InfluxDB influxDbBuild() {
        if (influxDB == null) {
            influxDB = InfluxDBFactory.connect(url, userName, password);
            influxDB.query(new Query("CREATE DATABASE " + database));
            influxDB.setDatabase(database);
        }
        return influxDB;
    }

    /**
     * 设置数据保存策略 defalut 策略名 /database 数据库名/ 30d 数据保存时限30/ 1 副本个数为1/ 结尾DEFAULT
     * 表示 设为默认的策略
     */
    public void createRetentionPolicy() {
        String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
                retentionPolicy, database, retentionPolicyTime, 1);
        this.query(command);
    }

    /**
     * 查询
     *
     * @param command 查询语句
     * @return
     */
    public QueryResult query(String command) {
        return influxDB.query(new Query(command, database));
    }

    /**
     * 插入
     *
     * @param measurement      * @param tags        标签
     * @param fields      字段
     */
    public void insert(String measurement, Map<String, String> tags, Map<String, Object> fields) {
        Point.Builder builder = Point.measurement(measurement);
        // 纳秒时会出现异常信息:partial write: points beyond retention policy dropped=1
        // builder.time(System.nanoTime(), TimeUnit.NANOSECONDS);
        builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        builder.tag(tags);
        builder.fields(fields);
        influxDB.write(database, "", builder.build());
    }

}

common文件夹InfluxdbConfig

package com.zzedu.gofwd.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class InfluxdbConfig {
    @Value("${spring.influx.url:''}")
    private String influxDBUrl;
    @Value("${spring.influx.user:''}")
    private String userName;
    @Value("${spring.influx.password:''}")
    private String password;
    @Value("${spring.influx.database:''}")
    private String database;
    @Value("${spring.influx.retention_policy:''}")
    private String retention_policy;
    @Value("${spring.influx.retention_policy_time:''}")
    private String retention_policy_time;

    @Bean
    public InfluxDBConnect getInfluxDBConnect() {
        InfluxDBConnect influxDB = new InfluxDBConnect(userName, password, influxDBUrl, database, retention_policy,retention_policy_time);
        influxDB.influxDbBuild();
        influxDB.createRetentionPolicy();
        return influxDB;
    }

}

resources/templates资源文件夹下的html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="insertPort">
    First ip:<input type="text" name="ip" value="10.102.110.36"><br>
    Last port: <input type="text" name="port" value="5656"><br>
    Last flag: <input type="number" name="flag" value="1"><br>
    Last bz: <input type="text" name="bz" value="这是测试"><br>
    <input type="submit">
</form>
</body>
</html>
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34665828/article/details/117370470

智能推荐

SQL优化--使用 EXISTS 代替 IN 和 inner join来选择正确的执行计划-程序员宅基地

为什么80%的码农都做不了架构师?>>> ..._sql server exists和inner join

openlayers结合百度地图API加载搜索定位功能_openlayers加载百度地图后可以使用百度地图api?-程序员宅基地

//每次请求之前清空上一次搜索的图层 searchPlaceLayer.getSource().clear() $.ajax({ //url中的参数含义参见百度地图官网webAPI文档 url: 'http://api.map.baidu.com/place/v2/search?query=大唐芙蓉园&amp;region=西安市&amp;p..._openlayers加载百度地图后可以使用百度地图api?

linux驱动学习笔记-程序员宅基地

说明笔记适用于Linux的2.6.10以后的内核。笔记以Linux Device Driver 3提供的scull程序(scull目录中的main.c和scull.h)为记录主线,并以该驱动程序中的各种系统调用和函数调用流程为记录顺序。比如,module_init( )和module_exit( )为相对应的一对系统调用,一般书籍中都会放在一起讨论,但是本笔记却不会这样,而是在需要调用的时

第十届蓝桥杯C语言A组-平方和_蓝桥杯平方和解释c语言_yuzhang_zy的博客-程序员宅基地

1. 问题描述:小明对数位中含有 2、 0、 1、 9 的数字很感兴趣,在 1 到 40 中这样的数包括 1、 2、 9、 10 至 32、 39 和 40,共 28 个,他们的和是 574,平方和是 14362。注意,平方和是指将每个数分别平方后求和。请问,在 1 到 2019 中,所有这样的数的平方和是多少?答案提交这是一道结果输出的题,你只需要算出结果后提交输出即可。本题的结果为一个整数,在输出答案时只输出这个整数,输出多余的内容将无法得分。提示:如果你编写程序计算,发现结果是负的,请仔细检查_蓝桥杯平方和解释c语言

大数据挖掘实践——K-Means聚类算法_kmeans聚类算法在数据挖掘中的应用介绍-程序员宅基地

大数据挖掘实践K-Means聚类算法引言:有n个数据D={X1,X2,…,Xn},我们想把这些数据分成K个类。这个问题的关键在于K为多大时分类是合适的,并且我们也不好选择一个好的初始点。所以我们在这里引入距离的概念(以欧式距离为例)。我们想找到K个中心,数据离哪些中心近我们就将其定义为哪一类,同时我们的K个中心能够使这个分类最合理也就是每个点到其中心的距离的和最小。提炼为:找K个中心,数据属于距离离其最近的中心一类,这K个中心能使所有数据距离其中心的距离和最小。算法介绍:L-Means又叫做K均值_kmeans聚类算法在数据挖掘中的应用介绍

如何在SAP的的凭证流浏览器(DRB)中显示客户自定义的凭证_sap 标准关系系浏览器-程序员宅基地

如何在SAP标准提供的的凭证流浏览器(DRB: Document Relationship Browser )中显示客户自定义的凭证? 实现起来大致有以下几个步骤:1. 为客户自定义凭证创建一个新的Business Object type,T-Code:SWO12. 建立客户自定义凭证和SAP标准凭证的关系。具体做法:在数据表ASHGETS中添加一条记录用于指定一个FM,这个FM将_sap 标准关系系浏览器

随便推点

51之KEIL调试查看ROM或RAM-程序员宅基地

KEIL之调试查看ROM或RAM 转自:http://blog.csdn.net/chenhezhuyan/article/details/8735696/ Ctrl+F5或点击调试按钮进入调试界面: 在工具栏上点击Memory Windows则右下角出现Memory1的页面,默认出现的是ROM的查看界面,在Address一栏输入十六进制的地址即可查看ROM

Vue项目 npm i 报错npm ERR! code 1_vue项目 npm i报错_早起的小青年的博客-程序员宅基地

Vue项目 npm i 报错npm ERR! code 1记录一下 npm i 的时候报错,报错信息如下记录一下 npm i 的时候报错,报错信息如下npm ERR! code 1npm ERR! path D:\workspace\exchange\exchange-web\exchange-web-ui\node_modules\node-sassnpm ERR! command failednpm ERR! command C:\Windows\system32\cmd.exe /d /s _vue项目 npm i报错

html用画布画图,HTML5入门十---Canvas画布实现画图(一)-程序员宅基地

body{background:white;}.ToolBar{float:left;font-family:"微软雅黑";font-size:14px;font-variant:small-caps;text-align:center;background:#F2F7EE;padding:10px 15px 3px 10px;margin-bottom:1px;margin-right:1px;..._html基于图片画布

Web API 上传下载文件-程序员宅基地

1.引用了一个第三方组件ICSharpCode.SharpZipLib.Zip;2.具体代码 实体类,可以用hashtable 替代 ,感觉hashtable 比较灵活public class FileResult { public string FileNames { get; set; } public string Descr...

5个例子-关于仿射集合、凸集合、锥的_任意线是仿射(affine),若过原点,则为凸锥(convex cone)-程序员宅基地

我就用通俗的语言记录了。空集、单个点和整个Rn\bm{R}^nRn空间都是Rn\bm{R}^nRn的子仿射集合(affine subsets),因此也是凸的(convex);任何直线都是仿射的(affine)。同时如果这条直线经过原点,那么它就是一个线性子空间(subspace),因此也是一个凸锥(convex cone);一条直线的分割(segment)是凸的,但不是仿射的,除非缩减到一..._任意线是仿射(affine),若过原点,则为凸锥(convex cone)

出现Java.lang.NoClassDefFoundError:org.jsoup.Jsoup的解决办法-程序员宅基地

在抓取网页数据的时候,出现了Java.lang.NoClassDefFoundError:org.jsoup.Jsoup这个错误,你可能会发现自己的代码是没有问题的,那这个到底是什么原因呢?有可能就是在导入第三方jar包的时候出现了错误。此时,你又要提问了:我buildpath里面明明都导入了啊!?此时不要慌张。一切问题都是有解决办法的。首先就是要百度或者谷歌(谷歌的话是需要VPN的),然_org.jsoup.jsoup