java 集成测试_javadbf.jar

java 集成测试_javadbf.jar0、pom.xml依赖<!–LMDB–> <dependency> <groupId>org.lmdbjava</groupId> <artifactId>lmdbjava</artifactId> <version>0.7.0</version> </dependency>1、application.properties配置:#maven多环境打包的支持l

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

0、pom.xml依赖

<!-- LMDB -->
		<dependency>
			<groupId>org.lmdbjava</groupId>
			<artifactId>lmdbjava</artifactId>
			<version>0.7.0</version>
		</dependency>

1、application.properties配置:

lmdb.path=E:\\lmdb
#单位为MB
lmdb.max.size=256
lmdb.database.count=10

2、LmdbServier 工具类


import lombok.extern.slf4j.Slf4j;
import org.lmdbjava.*;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import static java.lang.Integer.BYTES;
import static java.nio.ByteBuffer.allocateDirect;
import static org.lmdbjava.ByteBufferProxy.PROXY_OPTIMAL;
import static org.lmdbjava.DbiFlags.MDB_CREATE;
import static org.lmdbjava.Env.create;

/** * Lmdb操作工具类 * * @author jack<br> * @version 1.0<br> * @CreateDate 2020年5月11日 <br> */
@Slf4j
@Service
public class LmdbService { 
   

    /** * lmdb path */
    @Value("${lmdb.path}")
    private String lmdbPath;

    /** * env */
    private Env<ByteBuffer> env;

     /** * lmdb max size */
    @Value("${lmdb.max.size}")
    private int lmdbMaxSize;

    /** * lmdb库的数量 */
    @Value("${lmdb.database.count}")
    private int lmdbDatabaseCount;

    /** * Description: 初始化操作 <br> * @author jack<br> * @version 1.0<br> * @CreateDate 2020年5月11日 <br> */
    @PostConstruct
    private void init(){ 
   
        try { 
   
            final File path = new File(lmdbPath);
            env = create(PROXY_OPTIMAL)
                    .setMapSize(lmdbMaxSize * 1024 * 1024)
                    .setMaxDbs(lmdbDatabaseCount)
                    .setMaxReaders(lmdbDatabaseCount*2)
                    .open(path);
            log.info("初始化Lmdb成功......");
        } catch (Exception e) { 
   
            throw new LmdbException("IO failure", e);
        }
    }

    /** * Description: 向库中插入数据 <br> * @param dbName dbname * @param key value 数据 * @author jack<br> * @version 1.0<br> * @CreateDate 2020年5月11日 <br> */
    public void putValueToDb(String dbName, String key,String value) { 
   
        final Dbi<ByteBuffer> db = env.openDbi(dbName, MDB_CREATE);
        try (Txn<ByteBuffer> txn = env.txnWrite()) { 
   
            final Cursor<ByteBuffer> c = db.openCursor(txn);
            c.put(bb(key), bb(value));
            c.close();
            txn.commit();
        }

    }

    /** * Description: 根据指定的key获取数据 <br> * @param dbName dbname * @param key key * @author jack<br> * @version 1.0<br> * @CreateDate 2020年5月11日 <br> */
    public String getValueByKey(String dbName, String key) { 
   

        final Dbi<ByteBuffer> db = env.openDbi(dbName, MDB_CREATE);
        final Cursor<ByteBuffer> c;
        String value ="";
        try (Txn<ByteBuffer> txn = env.txnRead()) { 
   
            c = db.openCursor(txn);
            c.get(bb(key),GetOp.MDB_SET_KEY);
            ByteBuffer byteBuffer = c.val();
            c.close();
            value =  StandardCharsets.UTF_8.decode(byteBuffer).toString();
        }
        return value;
    }

    /** * Description: 获取库下所有的数据 <br> * @param dbName dbname * @author jack<br> * @version 1.0<br> * @CreateDate 2020年5月11日 <br> */
    public Map<String, String> getAllValueByDbName(String dbName) { 
   
        final Dbi<ByteBuffer> db = env.openDbi(dbName, MDB_CREATE);
        Map<String, String> map = new HashMap<>();
        try (Txn<ByteBuffer> txn = env.txnRead()) { 
   
            Cursor<ByteBuffer> cursor = db.openCursor(txn);
            while (cursor.next()) { 
   
                ByteBuffer key = cursor.key();
                ByteBuffer val = cursor.val();
                byte[] k = new byte[key.capacity()];
                byte[] v = new byte[val.capacity()];
                key.get(k);
                val.get(v);
                map.put(new String(k,StandardCharsets.UTF_8), new String(v,StandardCharsets.UTF_8));
            }
            cursor.close();
        }
        return map;
    }

    /** * Description: 根据dbname获取该库下的数量 <br> * @param dbName dbname * @author jack<br> * @version 1.0<br> * @CreateDate 2020年5月11日 <br> */
    public long getDbCount(String dbName) { 
   
        return getAllValueByDbName(dbName).size();
    }

    /** * 格式化 ByteBuffer * @param value * @author jack<br> * @version 1.0<br> * @CreateDate 2020年5月11日 <br> */
    static ByteBuffer bb(final int value) { 
   
        final ByteBuffer bb = allocateDirect(BYTES);
        bb.putInt(value).flip();
        return bb;
    }

    /** * 格式化 ByteBuffer * @param value * @author jack<br> * @version 1.0<br> * @CreateDate 2020年5月11日 <br> */
    static ByteBuffer bb(final String value) { 
   
        byte[] val = value.getBytes(StandardCharsets.UTF_8);
        final ByteBuffer bb = allocateDirect(val.length);
        bb.put(val).flip();
        return bb;
    }

}

3、lmdb数据库名称枚举:LmdbNameEnum

主要用于:定义lmdb中的dbName

public enum LmdbNameEnum { 
   
	//点位状态
	PointStatus("pointStatus");

	private String dbName;

	LmdbNameEnum(String dbName) { 
   
		this.dbName = dbName;
	}

	public String getDbName() { 
   
		return dbName;
	}

	@Override
	public String toString() { 
   
		return "LmdbNameEnum{" + "dbName=" + dbName + '}';
	}
}

4、Demo测试:LmdbController

package com.jack.maintain.controller;

import com.jack.framework.json.resp.BaseResp;
import com.jack.maintain.cache.LmdbService;
import com.jack.maintain.enums.LmdbNameEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping(value = "/lmdb")
public class LmdbController{ 
   

    @Autowired
    private LmdbService lmdbOperation;


    @RequestMapping(value = "setPoint/{point}",method = RequestMethod.GET)
    @ResponseBody
    public BaseResp setPoint(@PathVariable("point") String point){ 
   
        lmdbOperation.putValueToDb(LmdbNameEnum.PointStatus.getDbName(),point,point);
        return BaseResp.success("设值成功");
    }

    @RequestMapping(value = "getPoint/{point}",method = RequestMethod.GET)
    @ResponseBody
    public BaseResp getPoint(@PathVariable("point") String point){ 
   
        String val = lmdbOperation.getValueByKey(LmdbNameEnum.PointStatus.getDbName(),point);
        return BaseResp.success("成功",val);
    }

    @RequestMapping(value = "getPointCount",method = RequestMethod.GET)
    @ResponseBody
    public BaseResp getPointCount(){ 
   
        long count = lmdbOperation.getDbCount(LmdbNameEnum.PointStatus.getDbName());
        return BaseResp.success("成功",count);
    }

    @RequestMapping(value = "getPointList",method = RequestMethod.GET)
    @ResponseBody
    public BaseResp getDbVal(){ 
   
        Map<String,String> resultMap = lmdbOperation.getAllValueByDbName(LmdbNameEnum.PointStatus.getDbName());
        return BaseResp.success("成功",resultMap);
    }

}

测试结果:

插入值:
在这里插入图片描述
获取值:
在这里插入图片描述
在这里插入图片描述
配置文件中定义的lmdb数据路径:
在这里插入图片描述

资源补全:(相关的公共方法类)

BaseResp.java(接口统一返回封装类)


import java.util.List;

import com.jack.framework.json.JsonMsgUtil;
import com.fasterxml.jackson.annotation.JsonView;

import lombok.Data;
import lombok.experimental.Accessors;

/** * @Description: 基础返回类 * @date 2018年5月21日 下午9:28:00 */
@Data
@Accessors(chain = true)
public class BaseResp { 
   

	public interface DefaultJsonView { 
   
	};

	@JsonView(DefaultJsonView.class)
	private boolean rs;

	@JsonView(DefaultJsonView.class)
	private int code;

	@JsonView(DefaultJsonView.class)
	private String msg;

	@JsonView(DefaultJsonView.class)
	private Object data;

	@JsonView(DefaultJsonView.class)
	private List<?> datas;

	public BaseResp() { 
   
		this(0);
	}

	public BaseResp(int code) { 
   
		this.code = code;
	}

	public BaseResp(int code, String msg) { 
   
		this.code = code;
		this.msg = msg;
	}

	public String getMsg() { 
   
		if (msg == null) { 
   
			this.msg = JsonMsgUtil.getMsg(code);
		}
		if (this.msg == "") { 
   
			this.msg = JsonMsgUtil.getMsg(9999);
		}
		return msg;
	}

	public boolean getRs() { 
   
		rs = code == 0;
		return rs;
	}

	public boolean isRs() { 
   
		rs = code == 0;
		return rs;
	}

	public static boolean isSuccess(BaseResp resp) { 
   
		return resp.isRs();
	}

	/** * 功能描述:请求成功 * @param filePath */
	public static BaseResp success(String msg) { 
   
		BaseResp r = new BaseResp(0);
		r.setMsg(msg);
		return r;
	}
	//
	public static BaseResp success(String msg, Object data) { 
   
		BaseResp r = new BaseResp(0);
		r.setMsg(msg);
		r.setData(data);
		return r;
	}
	public static BaseResp success(String msg, List<?> datas) { 
   
		BaseResp r = new BaseResp(0);
		r.setMsg(msg);
		r.setDatas(datas);
		return r;
	}
	/** * 功能描述:参数出错 */
	public static BaseResp paramsError(String msg) { 
   
		BaseResp r = new BaseResp(100);
		r.setMsg(msg);
		return r;
	}

	/** * 功能描述:验证出错 */
	public static BaseResp validError(String msg) { 
   
		BaseResp r = new BaseResp(101);
		r.setMsg(msg);
		return r;
	}

	/** * 功能描述:逻辑判断出错 */
	public static BaseResp logicError(String msg) { 
   
		BaseResp r = new BaseResp(102);
		r.setMsg(msg);
		return r;
	}

	/** * 功能描述:未登录 */
	public static BaseResp invalidLogin(String url) { 
   
		BaseResp r = new BaseResp(9888);
		r.data = url;
		return r;
	}

	/** * 功能描述:无权限 */
	public static BaseResp invalidPerms(String msg) { 
   
		BaseResp r = new BaseResp(9889);
		r.setMsg(msg);
		return r;
	}

}

JsonMsgUtil.java(获取.properties文件属性的工具类)

package com.jack.framework.json;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import lombok.extern.slf4j.Slf4j;

/** * 功能说明:资源工具类 */
@Slf4j
public final class JsonMsgUtil { 
   

	private static Properties propertie = new Properties();

	private static String DEFAULT_MESSAGE = "";

	static { 
   
		try { 
   
			propertie = PropertiesLoaderUtils.loadProperties(new PathMatchingResourcePatternResolver().getResource("classpath:conf/json-msg.properties"));
		} catch (IOException e1) { 
   
			String s = "Load resource file failed.";
			log.error(s);
		}
	}

	public static String getMsg(String key) { 
   
		String message = propertie.getProperty(key, DEFAULT_MESSAGE);
		try { 
   
			message = new String(message.getBytes("UTF-8"), "UTF-8");
		} catch (UnsupportedEncodingException e) { 
   
			e.printStackTrace();
			return DEFAULT_MESSAGE;
		}
		return message;
	}

	public static String getMsg(int key) { 
   
		String message = propertie.getProperty("" + key, DEFAULT_MESSAGE);
		try { 
   
			message = new String(message.getBytes("UTF-8"), "UTF-8");
		} catch (UnsupportedEncodingException e) { 
   
			e.printStackTrace();
			return DEFAULT_MESSAGE;
		}
		return message;
	}

}

json-msg.properties(定义系统的统一返回码)
通常中文需要转成Unicode格式

##################
#JSON MSG
#返回码规划如下
#100 --默认参数异常
#1XXX --通用错误异常
#2XXX --业务模块异常
#3XXX --API模块异常

#8XXX --管理模块异常
#9XXX --系统异常
##################

##########################正常############################
0=成功

##########################业务模块异常############################
#2X0X--业务通用模块
2001=文件上传失败
2002=业务参数异常
2003=你无权操作

##########################API模块异常############################
3999=时间戳错误
3998=随机码错误
3997=签名错误

3000=未登录或已失效,请重新登录

##########################系统异常############################
9400=无效请求
9404=非法请求
9405=方法不被允许
9406=无法接受
9415=不支持的媒体类型
9500=执行异常

9900=运行时异常
9901=空值异常
9902=数据类型转换异常
9903=IO异常
9904=未知方法异常
9905=数组越界异常

9888=未登录或已失效,请重新登录
9889=权限不足

9997=Json格式错误
9998=数据格式错误
9999=系统异常

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189014.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm安装2021最新详细教程小白入门「建议收藏」

    pycharm安装2021最新详细教程小白入门「建议收藏」一、官网下载安装包Pycharm的官网(https://www.python.org/),这个链接进去直接是下载界面:https://www.jetbrains.com/pycharm/download/1、专业版(Professional)是收费的,但是可以试用一个月,包括了所有PyCharm的功能。2、社区版(Community)是免费的,但是比专业版少了一部分功能。例如:Web开发,科学工具,PythonWeb框架,Python代码分析,远程开发能力,数据库和SQ…

    2022年8月29日
    2
  • 最炫python表白代码_有趣的python代码表白

    最炫python表白代码_有趣的python代码表白文章目录前言演示网站制作部署网站二维码制作总结前言跟着我做,不要跳着看,否则你会失败。第一步是制作二维码;第二步是制作网站。演示具体成果地址:https://yanghanwen.xyz/ai/网站制作首先你需要下载我的这个完整项目:链接:https://pan.baidu.com/s/1EmRehx_gRnT5hLjJvKuAIg提取码:pz1y–来自百度网盘超级会员V2的分享下载好后文件目录如下:然后你需要注意的是我把img里面的图片删了,涉及隐私,大家自己替换自己追

    2022年8月30日
    6
  • rj45网络接口线序_rj11接口定义4芯线序

    rj45网络接口线序_rj11接口定义4芯线序RJ45接口定义常见的RJ45接口有两类:用于以太网网卡、路由器以太网接口等的DTE类型,还有用于交换机等的DCE类型。DTE我们可以称做“数据终端设备”,DCE我们可以称做“数据通信设备”。从某种意义来说,DTE设备称为“主动通信设备”,DCE设备称为“被动通信设备”。当两个类型一样的设备使用RJ45接口连接通信时,必须使用交叉线连接。这个可以从如下的RJ45DTE类型引脚定义和RJ4

    2022年9月17日
    5
  • 【pycharm】python代码块整体缩进,整体取消缩进

    【pycharm】python代码块整体缩进,整体取消缩进pycharm编辑器的缩进和取消缩进快捷键:整体缩进:tab整体取消缩进:shift+tabpython自带编辑器的缩进和取消缩进快捷键:整体缩进Ctrl+【整体取消缩进Ctrl+】…

    2022年8月28日
    2
  • 选择器gt_css的基本选择器

    选择器gt_css的基本选择器一、后代选择器选取指定元素的后代元素与子元素选择器相比,后代选择器选取的不一定是直接后代(儿子),而是作用于所有后代元素(儿子、孙子、重孙…)都可以。二、(>)子元素选择器选取某个元素的

    2022年8月1日
    6
  • nmap命令大全[通俗易懂]

    nmap命令大全[通俗易懂]扫描类型-sTTCPconnect()扫描:这是最基本的TCP扫描方式。connect()是一种系统调用,由操作系统提供,用来打开一个连接。如果目标端口有程序监听,connect()就会成功返回,否则这个端口是不可达的。这项技术最大的优点是,你勿需root权限。任何UNIX用户都可以自由使用这个系统调用。这种扫描很容易被检测到,在目标主机的日志中会记录大批的连接请求以及错误信息。-sSTCP同步扫描(TCPSYN):因为不必全部打开一个TCP连接,所以这项技术通常称为半开扫描(half-ope

    2022年5月28日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号