java 对象复制 反射_利用Java反射机制实现对象相同字段的复制操作

一、如何实现不同类型对象之间的复制问题?

1、为什么会有这个问题?

近来在进行一个项目开发的时候,为了隐藏后端数据库表结构、同时也为了配合给前端一个更友好的API接口文档(swagger API文档),我采用POJO来对应数据表结构,使用VO来给传递前端要展示的数据,同时使用DTO来进行请求参数的封装。以上是一个具体的场景,可以发现这样子一个现象:POJO、VO、DTO对象是同一个数据的不同视图,所以会有很多相同的字段,由于不同的地方使用不同的对象,无可避免的会存在对象之间的值迁移问题,迁移的一个特征就是需要迁移的值字段相同。字段相同,于是才有了不同对象之间进行值迁移复制的问题。

2、现有的解决方法

一个一个的get出来后又set进去。这个方法无可避免会增加很多的编码复杂度,还是一些很没有营养的代码,看多了还会烦,所以作为一个有点小追求的程序员都没有办法忍受这种摧残。

使用别人已经存在的工具。在spring包里面有一个可以复制对象属性的工具方法,可以进行对象值的复制,下一段我们详细去分析它的这个工具方法。

自己动手丰衣足食。自己造工具来用,之所以自己造工具不是因为喜欢造工具,而是现有的工具没办法解决自己的需求,不得已而为之。

二、他山之石可以攻玉,详谈spring的对象复制工具

1、看看spring的对象复制工具到底咋样?

类名:org.springframework.beans.BeanUtils

这个类里面所有的属性复制的方法都调用了同一个方法,我们就直接分析这个原始的方法就行了。

/**

* Copy the property values of the given source bean into the given target bean.

*

Note: The source and target classes do not have to match or even be derived

* from each other, as long as the properties match. Any bean properties that the

* source bean exposes but the target bean does not will silently be ignored.

* @param source the source bean:也就是说要从这个对象里面复制值出去

* @param target the target bean:出去就是复制到这里面来

* @param editable the class (or interface) to restrict property setting to:这个类对象是target的父类或其实现的接口,用于控制属性复制的范围

* @param ignoreProperties array of property names to ignore:需要忽略的字段

* @throws BeansException if the copying failed

* @see BeanWrapper

*/

private static void copyProperties(Object source, Object target, Class> editable, String... ignoreProperties)

throws BeansException {

//这里在校验要复制的对象是不可以为null的,这两个方法可是会报错的!!

Assert.notNull(source, "Source must not be null");

Assert.notNull(target, "Target must not be null");

//这里和下面的代码就有意思了

Class> actualEditable = target.getClass();//获取目标对象的动态类型

//下面判断的意图在于控制属性复制的范围

if (editable != null) {

//必须是target对象的父类或者其实现的接口类型,相当于instanceof运算符

if (!editable.isInstance(target)) {

throw new IllegalArgumentException("Target class [" + target.getClass().getName() +

"] not assignable to Editable class [" + editable.getName() + "]");

}

actualEditable = editable;

}

//不得不说,下面这段代码乖巧的像绵羊,待我们来分析分析它是如何如何乖巧的

PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);//获取属性描述,描述是什么?描述就是对属性的方法信息的封装,好乖。

List ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

//重头戏开始了!开始进行复制了

for (PropertyDescriptor targetPd : targetPds) {

//先判断有没有写方法,没有写方法我也就没有必要读属性出来了,这个懒偷的真好!

Method writeMethod = targetPd.getWriteMethod();

//首先,没有写方法的字段我不写,乖巧撒?就是说你不让我改我就不改,让我忽略我就忽略!

if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {

PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());

//如果没办法从原对象里面读出属性也没有必要继续了

if (sourcePd != null) {

Method readMethod = sourcePd.getReadMethod();

//这里就更乖巧了!写方法不让我写我也不写!!!

if (readMethod != null &&

ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {

try {

//这里就算了,来都来了,就乖乖地进行值复制吧,别搞东搞西的了

if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {

readMethod.setAccessible(true);

}

Object value = readMethod.invoke(source);

if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {

writeMethod.setAccessible(true);

}

writeMethod.invoke(target, value);

}

catch (Throwable ex) {

throw new FatalBeanException(

"Could not copy property '" + targetPd.getName() + "' from source to target", ex);

}

}

}

}

}

}

2、对复制工具的一些看法和总结

总结上一段代码的分析,我们发现spring自带的工具有以下特点:

它名副其实的是在复制属性,而不是字段!!

它可以通过一个目标对象的父类或者其实现的接口来控制需要复制属性的范围

很贴心的可以忽略原对象的某些字段,可以通过2的方法忽略某些目标对象的字段

但是,这远远不够!!!我需要如下的功能:

复制对象的字段,而不是属性,也就是说我需要一个更暴力的复制工具。

我需要忽略原对象的某些字段,同时也能够忽略目标对象的某些字段。

我的项目还需要忽略原对象为null的字段和目标对象不为null的字段

带着这三个需求,开始我的工具制造。

三、自己动手丰衣足食

1、我需要解析字节码

为了避免对字节码的重复解析,使用缓存来保留解析过的字节码解析结果,同时为了不让这个工具太占用内存,使用软引用来进行缓存,上代码:

/*

******************************************************

* 基础的用于支持反射解析的解析结果缓存,使用软引用实现

******************************************************

*/

private static final Map,SoftReference>> resolvedClassCache = new ConcurrentHashMap<>();

/**

* 同步解析字节码对象,将解析的结果放入到缓存 1、解析后的字段对象全部 accessAble

* 1、返回的集合不支持修改,要修改请记得自己重新建一个复制的副本

* @param sourceClass:需要解析的字节码对象

*/

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")

public static Map resolveClassFieldMap(final Class> sourceClass){

SoftReference> softReference = resolvedClassCache.get(sourceClass);

//判断是否已经被初始化

if(softReference == null || softReference.get() == null){

//对同一个字节码对象的解析是同步的,但是不同字节码对象的解析是并发的,因为字节码对象只有一个

synchronized(sourceClass){

softReference = resolvedClassCache.get(sourceClass);

if(softReference == null || softReference.get() == null){

//采用: 来记录解析结果

Map fieldMap = new HashMap<>();

/*

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this

Class object. This includes public, protected, default access, and private fields, but excludes inherited fields

*/

Field[] declaredFields = sourceClass.getDeclaredFields();

if(declaredFields != null && declaredFields.length > 0){

for(Field field : declaredFields){

/*

Set the accessible flag for this object to the indicated boolean value.

*/

field.setAccessible(true);

//字段名称和字段对象

fieldMap.put(field.getName(),field);

}

}

//设置为不变Map,这个肯定是不能够改的啊!所以取的时候需要重新构建一个map

fieldMap = Collections.unmodifiableMap(fieldMap);

softReference = new SoftReference<>(fieldMap);

/*

更新缓存,将解析后的数据加入到缓存里面去

*/

resolvedClassCache.put(sourceClass,softReference);

return fieldMap;

}

}

}

/*

运行到这里来的时候要么早就存在,要么就是已经被其他的线程给初始化了

*/

return softReference.get();

}

2、我需要能够进行对象的复制,基本方法

/**

* 进行属性的基本复制操作

* @param source:源对象

* @param sourceFieldMap:原对象解析结果

* @param target:目标对象

* @param targetFieldMap:目标对象解析结果

*/

public static void copyObjectProperties(Object source,Map sourceFieldMap,Object target,Map targetFieldMap){

//进行属性值复制

sourceFieldMap.forEach(

(fieldName,sourceField) -> {

//查看目标对象是否存在这个字段

Field targetField = targetFieldMap.get(fieldName);

if(targetField != null){

try{

//对目标字段进行赋值操作

targetField.set(target,sourceField.get(source));

}catch(IllegalAccessException e){

e.printStackTrace();

}

}

}

);

}

3、夜深了,准备睡觉了

基于这两个方法,对其进行封装,实现了我需要的功能,并且在项目中运行目前还没有bug,应该可以直接用在生产环境,各位看官觉得可以可以拿来试一试哦!!

4、完整的代码(带注释:需要自取,无外部依赖,拿来即用)

package edu.cqupt.demonstration.common.util;

import java.lang.ref.SoftReference;

import java.lang.reflect.Field;

import java.util.*;

import java.util.concurrent.ConcurrentHashMap;

/**

* 反射的工具集,主要用于对对象的复制操作

*/

public class ReflectUtil {

/*

******************************************************

* 基础的用于支持反射解析的解析结果缓存,使用软引用实现

******************************************************

*/

private static final Map,SoftReference>> resolvedClassCache = new ConcurrentHashMap<>();

/*

****************************************

* 获取一个对象指定条件字段名称的工具方法

****************************************

*/

/**

* 获取一个对象里面字段为null的字段名称集合

*/

public static String[] getNullValueFieldNames(Object source){

//非空校验:NullPointerException

Objects.requireNonNull(source);

Class> sourceClass = source.getClass();

//从缓存里面获取,如果缓存里面没有就会进行第一次反射解析

Map classFieldMap = getClassFieldMapWithCache(sourceClass);

List nullValueFieldNames = new ArrayList<>();

classFieldMap.forEach(

(fieldName,field) -> {

try{

//挑选出值为null的字段名称

if(field.get(source) == null){

nullValueFieldNames.add(fieldName);

}

}catch(IllegalAccessException e){

e.printStackTrace();

}

}

);

return nullValueFieldNames.toArray(new String[]{});

}

/**

* 获取一个对象里面字段不为null的字段名称集合

*/

public static String[] getNonNullValueFieldNames(Object source){

//非空校验

Objects.requireNonNull(source);

//获取空值字段名称

String[] nullValueFieldNames = getNullValueFieldNames(source);

Map classFieldMap = getClassFieldMapWithCache(source.getClass());

//获取全部的字段名称,因为原数据没办法修改,所以需要重新建立一个集合来进行判断

Set allFieldNames = new HashSet<>(classFieldMap.keySet());

//移除掉值为null的字段名称

allFieldNames.removeAll(Arrays.asList(nullValueFieldNames));

return allFieldNames.toArray(new String[]{});

}

/*

***************************************************************

* 复制一个对象的相关工具方法,注意事项如下:

* 1、只能复制字段名称相同且数据类型兼容的字段数据

* 2、只能复制这个对象实际类(运行时动态类型)里面声明的各种字段

***************************************************************

*/

/**

* 将一个对象里面字段相同、类型兼容的数据复制到另外一个对象去

* 1、只复制类的运行时类型的声明的全部访问权限的字段

* @param source:从这个对象复制

* @param target:复制到这个对象来

*/

public static void copyPropertiesSimple(Object source,Object target){

copyObjectProperties(

source,new HashMap<>(getClassFieldMapWithCache(source.getClass())),

target,new HashMap<>(getClassFieldMapWithCache(target.getClass())));

}

/**

* 除实现 copyPropertiesSimple 的功能外,会忽略掉原对象的指定字段的复制

* @param ignoreFieldNames:需要忽略的原对象字段名称集合

*/

public static void copyPropertiesWithIgnoreSourceFields(Object source,Object target,String ...ignoreFieldNames){

Map sourceFieldMap = new HashMap<>(getClassFieldMapWithCache(source.getClass()));

filterByFieldName(sourceFieldMap,ignoreFieldNames);

copyObjectProperties(source,sourceFieldMap,target,new HashMap<>(getClassFieldMapWithCache(target.getClass())));

}

/**

* 除实现 copyPropertiesSimple 的功能外,会忽略掉原对象字段值为null的字段

*/

public static void copyPropertiesWithNonNullSourceFields(Object source,Object target){

Map sourceFieldMap = new HashMap<>(getClassFieldMapWithCache(source.getClass()));

filterByFieldValue(source,sourceFieldMap,true);

copyObjectProperties(source,sourceFieldMap,target,new HashMap<>(getClassFieldMapWithCache(target.getClass())));

}

/**

* 除实现 copyPropertiesSimple 的功能外,会忽略掉目标对象的指定字段的复制

* @param ignoreFieldNames:需要忽略的原对象字段名称集合

*/

public static void copyPropertiesWithIgnoreTargetFields(Object source,Object target,String ...ignoreFieldNames){

Map targetFieldMap = new HashMap<>(getClassFieldMapWithCache(target.getClass()));

filterByFieldName(targetFieldMap,ignoreFieldNames);

copyObjectProperties(source,new HashMap<>(getClassFieldMapWithCache(source.getClass())),target,targetFieldMap);

}

/**

* 除实现 copyPropertiesSimple 的功能外,如果目标对象的属性值不为null将不进行覆盖

*/

public static void copyPropertiesWithTargetFieldNonOverwrite(Object source,Object target){

Map targetFieldMap = new HashMap<>(getClassFieldMapWithCache(target.getClass()));

filterByFieldValue(target,targetFieldMap,false);

copyObjectProperties(source,new HashMap<>(getClassFieldMapWithCache(source.getClass())),target,targetFieldMap);

}

/**

* 进行复制的完全定制复制

* @param source:源对象

* @param target:目标对象

* @param ignoreSourceFieldNames:需要忽略的原对象字段名称集合

* @param ignoreTargetFieldNames:要忽略的目标对象字段集合

* @param isSourceFieldValueNullAble:是否在源对象的字段为null的时候仍然进行赋值

* @param isTargetFiledValueOverwrite:是否在目标对象的值不为null的时候仍然进行赋值

*/

public static void copyPropertiesWithConditions(Object source,Object target

,String[] ignoreSourceFieldNames,String[] ignoreTargetFieldNames

,boolean isSourceFieldValueNullAble,boolean isTargetFiledValueOverwrite){

Map sourceFieldMap = new HashMap<>(getClassFieldMapWithCache(source.getClass()));

Map targetFieldMap = new HashMap<>(getClassFieldMapWithCache(target.getClass()));

if(!isSourceFieldValueNullAble){

filterByFieldValue(source,sourceFieldMap,true);

}

if(!isTargetFiledValueOverwrite){

filterByFieldValue(target,targetFieldMap,false);

}

filterByFieldName(sourceFieldMap,ignoreSourceFieldNames);

filterByFieldName(targetFieldMap,ignoreTargetFieldNames);

copyObjectProperties(source,sourceFieldMap,target,targetFieldMap);

}

/*

******************************

* 内部工具方法或者内部兼容方法

******************************

*/

/**

* 同步解析字节码对象,将解析的结果放入到缓存 1、解析后的字段对象全部 accessAble

* 1、返回的集合不支持修改,要修改请记得自己重新建一个复制的副本

* @param sourceClass:需要解析的字节码对象

*/

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")

public static Map resolveClassFieldMap(final Class> sourceClass){

SoftReference> softReference = resolvedClassCache.get(sourceClass);

//判断是否已经被初始化

if(softReference == null || softReference.get() == null){

//对同一个字节码对象的解析是同步的,但是不同字节码对象的解析是并发的

synchronized(sourceClass){

softReference = resolvedClassCache.get(sourceClass);

if(softReference == null || softReference.get() == null){

Map fieldMap = new HashMap<>();

/*

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this

Class object. This includes public, protected, default access, and private fields, but excludes inherited fields

*/

Field[] declaredFields = sourceClass.getDeclaredFields();

if(declaredFields != null && declaredFields.length > 0){

for(Field field : declaredFields){

/*

Set the accessible flag for this object to the indicated boolean value.

*/

field.setAccessible(true);

fieldMap.put(field.getName(),field);

}

}

//设置为不变Map

fieldMap = Collections.unmodifiableMap(fieldMap);

softReference = new SoftReference<>(fieldMap);

/*

更新缓存,将解析后的数据加入到缓存里面去

*/

resolvedClassCache.put(sourceClass,softReference);

return fieldMap;

}

}

}

/*

运行到这里来的时候要么早就存在,要么就是已经被其他的线程给初始化了

*/

return softReference.get();

}

/**

* 确保正确的从缓存里面获取解析后的数据

* 1、返回的集合不支持修改,要修改请记得自己重新建一个复制的副本

* @param sourceClass:需要解析的字节码对象

*/

public static Map getClassFieldMapWithCache(Class> sourceClass){

//查看缓存里面有没有已经解析完毕的现成的数据

SoftReference> softReference = resolvedClassCache.get(sourceClass);

//确保classFieldMap的正确初始化和缓存

if(softReference == null || softReference.get() == null){

//解析字节码对象

return resolveClassFieldMap(sourceClass);

}else {

//从缓存里面正确的取出数据

return softReference.get();

}

}

/**

* 将一个可变参数集合转换为List集合,当为空的时候返回空集合

*/

public static List resolveArrayToList(T ...args){

List result = new ArrayList<>();

if(args != null && args.length > 0){

result = Arrays.asList(args);

}

return result;

}

/**

* 进行属性的基本复制操作

* @param source:源对象

* @param sourceFieldMap:原对象解析结果

* @param target:目标对象

* @param targetFieldMap:目标对象解析结果

*/

public static void copyObjectProperties(Object source,Map sourceFieldMap,Object target,Map targetFieldMap){

//进行属性值复制

sourceFieldMap.forEach(

(fieldName,sourceField) -> {

//查看目标对象是否存在这个字段

Field targetField = targetFieldMap.get(fieldName);

if(targetField != null){

try{

//对目标字段进行赋值操作

targetField.set(target,sourceField.get(source));

}catch(IllegalAccessException e){

e.printStackTrace();

}

}

}

);

}

/**

* 忽略掉对象里面的某些字段

*/

public static void filterByFieldName(Map fieldMap,String ... ignoreFieldNames){

//需要忽略的对象字段

List ignoreNames = ReflectUtil.resolveArrayToList(ignoreFieldNames);

//移除忽略的对象字段

fieldMap.keySet().removeAll(ignoreNames);

}

/**

* 忽略掉非空的字段或者空的字段

*/

public static void filterByFieldValue(Object object,Map fieldMap,boolean filterNullAble){

Iterator iterator = fieldMap.keySet().iterator();

if(filterNullAble){

while(iterator.hasNext()){

try{

//移除值为null的字段

if(fieldMap.get(iterator.next()).get(object) == null){

iterator.remove();

}

}catch(IllegalAccessException e){

e.printStackTrace();

}

}

}else {

while(iterator.hasNext()){

try{

//移除字段不为null的字段

if(fieldMap.get(iterator.next()).get(object) != null){

iterator.remove();

}

}catch(IllegalAccessException e){

e.printStackTrace();

}

}

}

}

}

补充知识:Java将两个JavaBean里相同的字段自动填充

最近因为经常会操作讲两个JavaBean之间相同的字段互相填充,所以就写了个偷懒的方法。记录一下

/**

* 将两个JavaBean里相同的字段自动填充

* @param dto 参数对象

* @param obj 待填充的对象

*/

public static void autoFillEqFields(Object dto, Object obj) {

try {

Field[] pfields = dto.getClass().getDeclaredFields();

Field[] ofields = obj.getClass().getDeclaredFields();

for (Field of : ofields) {

if (of.getName().equals("serialVersionUID")) {

continue;

}

for (Field pf : pfields) {

if (of.getName().equals(pf.getName())) {

PropertyDescriptor rpd = new PropertyDescriptor(pf.getName(), dto.getClass());

Method getMethod = rpd.getReadMethod();// 获得读方法

PropertyDescriptor wpd = new PropertyDescriptor(pf.getName(), obj.getClass());

Method setMethod = wpd.getWriteMethod();// 获得写方法

setMethod.invoke(obj, getMethod.invoke(dto));

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 将两个JavaBean里相同的字段自动填充,按指定的字段填充

* @param dto

* @param obj

* @param String[] fields

*/

public static void autoFillEqFields(Object dto, Object obj, String[] fields) {

try {

Field[] ofields = obj.getClass().getDeclaredFields();

for (Field of : ofields) {

if (of.getName().equals("serialVersionUID")) {

continue;

}

for (String field : fields) {

if (of.getName().equals(field)) {

PropertyDescriptor rpd = new PropertyDescriptor(field, dto.getClass());

Method getMethod = rpd.getReadMethod();// 获得读方法

PropertyDescriptor wpd = new PropertyDescriptor(field, obj.getClass());

Method setMethod = wpd.getWriteMethod();// 获得写方法

setMethod.invoke(obj, getMethod.invoke(dto));

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

但这样写不能把父类有的属性自动赋值所以修改了一下

/**

* 将两个JavaBean里相同的字段自动填充

* @param obj 原JavaBean对象

* @param toObj 将要填充的对象

*/

public static void autoFillEqFields(Object obj, Object toObj) {

try {

Map getMaps = new HashMap<>();

Method[] sourceMethods = obj.getClass().getMethods();

for (Method m : sourceMethods) {

if (m.getName().startsWith("get")) {

getMaps.put(m.getName(), m);

}

}

Method[] targetMethods = toObj.getClass().getMethods();

for (Method m : targetMethods) {

if (!m.getName().startsWith("set")) {

continue;

}

String key = "g" + m.getName().substring(1);

Method getm = getMaps.get(key);

if (null == getm) {

continue;

}

// 写入方法写入

m.invoke(toObj, getm.invoke(obj));

}

} catch (Exception e) {

e.printStackTrace();

}

}

以上这篇利用Java反射机制实现对象相同字段的复制操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/523507.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

企业实战_01_Redis下载/安装/运行/停止

文章目录一、Redis下载&#xff1a;官方&#xff1a;https://redis.io/二、Redis安装&#xff1a;2.1. 上传redis软件服务器2.2. 解压redis2.3. 进入redis目录&#xff0c;进行redis安装2.4. 执行redis安装测试&#xff1a;2.5. 安装异常处理三、redis 启动、停止3.1. 进入src目…

关于5G,你必须知道的事儿……

戳蓝字“CSDN云计算”关注我们哦&#xff01;文 | 小枣君来源 | 鲜枣课堂什么是5G 5G&#xff0c;就是5th Generation Mobile Networks&#xff08;第五代移动通信网络&#xff09;&#xff0c;也可以称为5th Generation Wireless Systems&#xff08;第五代无线通信系统&a…

java管理员登录_idea实现管理员登录javaweb

mysql创建db_0106数据库&#xff0c;创建表添加一条数据&#xff0c;id int自增&#xff0c;密码&#xff1a;为MD5加密insert into tb_sys values(null,admin,MD5(123),"系统管理员");项目目录结构com.isoft.db包下db.properties文件mysql.drivercom.mysql.jdbc.Dri…

linux环境下redis5.0的安装配置

文章目录一、Redis介绍&#xff1a;二、安装Redis2.1. 下载 解压 进入文件夹 然后 编译2.2. 启动Redis2.2.1. 指定配置文件启动redis2.2.2. 配置redis后台启动三. 登录验证一、Redis介绍&#xff1a; Redis是当前比较热门的NOSQL系统之一&#xff0c;它是一个key-value存储系统…

漫话:如何给女朋友解释什么是编译与反编译

戳蓝字“CSDN云计算”关注我们哦&#xff01;来源 | 漫话编程某天下班后&#xff0c;我在家里进行电话面试&#xff0c;问到面试者这样一个问题&#xff1a;"你知道使用哪些办法可以反编译Java代码吗&#xff1f;"。但是面试者回答的并不好&#xff0c;所以我在面试评…

java数组下标越界处理_可恶的Java数组下标越界检查

刚刚学习Java不到一个星期&#xff0c;本来是搞C的&#xff0c;没事学习Java&#xff0c;将来可以更好的想Android方向发展一下。现在正处于磨基础的阶段&#xff0c;对与每一个新手来书&#xff0c;最痛苦的莫过于此了。写了一个冒泡排序&#xff0c;用C的思想写&#xff0c;没…

企业实战_02_Redis基础

接上一篇&#xff1a;企业实战_01_Redis下载/安装/运行/停止https://blog.csdn.net/weixin_40816738/article/details/99198062 Redis小知识&#xff1a; 向服务器发送命令 ①redis-cli连上redis服务器后&#xff0c;可以在命令行发送指令&#xff1b; ②ping&#xff0c;测试…

Python 爬取 42 年高考数据,告诉你高考为什么这么

戳蓝字“CSDN云计算”关注我们哦&#xff01;作者 | 徐麟责编 | 伍杏玲封图 | CSDN付费下载于东方IC对于像作者一样已经工作的“上班族”来说&#xff0c;6月7号到9号三天无疑是兴奋到飞起的&#xff0c;终于迎来了令人愉悦的端午假期&#xff1a;然而有那么一群人&#xff0c;…

windows下载、安装运行redis

https://github.com/microsoftarchive/redis/ redis的配置文件&#xff1a; redis.windows.conf

java读取excel某个单元格的值_[转载]Java读取Excel中的单元格数据

目前网上能找到的读取Excel表格中数据的两种比较好的方案&#xff1a;PageOffice好用开发效率高&#xff1b;POI免费。供大家参考&#xff0c;针对具体情况选择具体方案。1. PageOffice读取excelimport com.zhuozhengsoft.pageoffice.*;import com.zhuozhengsoft.pageoffice.ex…

【五分钟】看完一道有装逼解法的算法题

戳蓝字“CSDN云计算”关注我们哦&#xff01;作者 | 程序员小吴来源 | 五分钟学算法题目来源于 LeetCode 上第 342 号问题&#xff1a;4 的幂。题目难度为 Easy&#xff0c;目前通过率为 45.3% 。题目描述给定一个整数 (32 位有符号整数)&#xff0c;请编写一个函数来判断它是否…

企业实战_03_Redis基础命令

接上一篇&#xff1a;企业实战_02_Redis基础 https://blog.csdn.net/weixin_40816738/article/details/99204244 先启动redis服务端&#xff0c;在启动redis客户端 说明命令info查看系统信息ping测试连通性dbsizekey数量keys *查看所有的keyselect 1切换到键空间(keyspace1)…

Docker精华问答 | Docker commit如何用?

Docker 是个划时代的开源项目&#xff0c;它彻底释放了计算虚拟化的威力&#xff0c;极大提高了应用的维护效率&#xff0c;降低了云计算应用开发的成本&#xff01;使用 Docker&#xff0c;可以让应用的部署、测试和分发都变得前所未有的高效和轻松&#xff01;1Q&#xff1a;…

java channel源码_Netty 4.0 源码分析(三):Channel和ChannelPipeline

Client和server通过Channel连接&#xff0c;然后通过ByteBuf进行传输。每个Channel有自己的Pipeline&#xff0c;Pipeline上面可以添加和定义Handler和Event。Channel类1 package io.netty.channel;2 import io.netty.buffer.ByteBuf;3 import io.netty.buffer.MessageBuf;4 im…

(解决)can't connect to redis-server

编辑 vim redis.conf bind 127.0.0.1 添加本机IP地址 protected-mode no //将yes改为no yes为保护模式 requirepass gblfy//找到此处设置密码 ./redis-server ../redis.conf //重启redis ./redis-cli -a gblfy shutdown//关闭redis

一拍即合、一见钟情之后,智慧城市的“福利”来啦……

戳蓝字“CSDN云计算”关注我们哦&#xff01;“未来双方的合作会针对智慧城市、智慧建筑以及智慧地域开发等领域开展创新型的解决方案&#xff0c;这种创造对于目前已经存在的&#xff0c;该领域技术甚至是竞争对手都是一个强大的震撼与颠覆。”达索系统董事会副主席兼首席执行…

Springboot部署到Tomcat,可以不带项目名进行访问

文章目录1. 进入tomcat的conf目录2. 编辑server.xml2.1. 修改第一处2.2. 修改第二处2.3. 发布war包2.4. 浏览器请求2.5. windows样例1. 进入tomcat的conf目录 cd /app/tomcat8081/conf/2. 编辑server.xml Tomcat9使用war包设置默认项目需要设置下server.xml就行 vim server.…

python ndarray append_9-Python-NumPy数组元素的添加与删除

数组元素的添加与删除 相关函数列表如下&#xff1a;函数元素及描述resize返回指定形状的新数组append将值添加到数组末尾insert沿指定轴将值插入到指定下标之前delete删掉某个轴的子数组&#xff0c;并返回删除后的新数组unique查找数组内的唯一元素1)返回指定大小的新数组num…

Java -jar 如何在后台运行项目

演示项目&#xff1a; GitHub链接&#xff1a;https://github.com/gb-heima/java-jar-nohup zip下载链接&#xff1a;https://github.com/gb-heima/java-jar-nohup/archive/master.zip git下载地址&#xff1a; git clone gitgithub.com:gb-heima/java-jar-nohup.git编译打包 …

裁员1700人,IBM 声称内部调整团队;谷歌将以26亿美元全现金收购Looker,绝对大手笔...

关注并标星星CSDN云计算极客头条&#xff1a;速递、最新、绝对有料。这里有企业新动、这里有业界要闻&#xff0c;打起十二分精神&#xff0c;紧跟fashion你可以的&#xff01;每周三次&#xff0c;打卡即read更快、更全了解泛云圈精彩newsgo go go 贝索斯旗下蓝色起源将登月球…