java8技巧


String skuMarkBinary = Integer.toBinaryString(Integer.parseInt(skuMark));

 String value = skuMarkBinary.charAt(skuMarkBinary.length()-index-1)+"";  

 

changeDelivery
updateCargoDelivery 
// post domain event
domainEventPublisher.publish(new CargoBookDomainEvent(cargo));
eventBus里面有register注册方法和决定方法

 

 /**

 * 超级会员

 */

public class SuperVipPayService implements UserPayService, InitializingBean{

    @Override

    public BigDicimal quote(BigDecimal orderPrice){

        return 8折价格;

    } 

    

    @Override

    public void afterPropertiesSet(){

        UserPayServiceStrategyFactory.register("SuperVip", this);

    }

 

 

public class UserPayServiceStrategyFactory{

    private static Map<String, UserPayService> services = new ConcurrentHashMap<String, UserPayService>();
    
    public static UserPayService getByUserType(String type){
        return services.get(type);
    }
    
    public static void register(String userType, UserPayService userPayService){
        Assert.notNull(userType, "userType can't be null");
        services.put(userType, userPayService);
    }
} 


class Entity{
    public Integer id;
    public Integer colA;
    public BigDecimal colB;
    public Object colB;
}
List转Map
Map<Integer, Entity> map = entityList.stream().collect(Collectors.toMap(k->k.getId(), k->k);
分组
Map<Integer, List<Entity>> = entityList.stream().collect(Collectors.groupingBy(Entity::getId);
过滤
List<Entity> = entityList.stream().filter(k -> k.getColA.equals(1)).collect(Collectors.toList());
求和
//decimal
List<Entity> = entityList.stream().map(k -> k.getColB()).reduce(BigDecimal.ZERO, BigDecimal::add) 
//int
 List<Entity> = entityList.stream().mapToInt(k -> k.getColA()).sum()


Sum(*)  group by a, b

Map<String, AdmSelfSkuSaleQtyMEntity> partMergeResult =
batchGoodsSales
        .stream()
        .collect(Collectors.groupingBy(
                x -> x.getGoodsId() + "," + x.getWarehouseId() + "," + x.getSaleMonth(),
                Collectors.reducing(
                        null,
                        x -> x,
                        AdmSelfSkuSaleQtyMEntity::add
                )
        ));


Merge

monthlyResult = Stream
   .concat(monthlyResult.entrySet().stream(), partMergeResult.entrySet().stream())
   .collect(Collectors.toMap(
           Map.Entry::getKey,
           Map.Entry::getValue,
           AdmSelfSkuSaleQtyMEntity::add
   ));

 

 

  1. domain:基础实体类
    |-- domain
    |--|-- entity:存放DO结尾的数据库实体类
    |--|-- bo:存放BO结尾的业务实体类
    |--|-- enums:存放全局的枚举类。

inner-api包目录
|--|-- domain
|--|--|--|-- dto:DTO实体类
|--|--|--|-- query:用于封装请求参数的实体类
|--|--|--|-- enums:枚举类
|--|--|--|-- constant:常量类
|--|-- service:服务接口

 

 

 


1)结果实体类:

(1)service层以BO结尾

(2)mananger层DO结尾

(3)dao层以DO结尾

(4)jsf传输层以DTO结尾

(5)controller层传递给页面以VO结尾

2)请求实体类:

上层向下层提交请求的时候,如果没有封装成对象,则用Param结尾的实体,负责为BO或DO,具体如下:

写请求:

(1)controller层Param结尾

(2)service层BO结尾

(3)mananger层DO结尾

(4)dao层DO结尾

查请求:

(1)controller层Param结尾

(2)service层Query结尾

(3)mananger层Query结尾

(4)dao层Query结尾

实体类转换工具BeanUtils.copyProperties

public OrderBO getById(Integer id) {
    OrderDO orderDO = orderManager.getById(id);
    if (orderDO == null) {
        return null;
    }
    OrderBO orderBO = new OrderBO();
    BeanUtils.copyProperties(orderDO, orderBO);
    return orderBO;
}



public class BeanUtils extends org.springframework.beans.BeanUtils {
 
    public static void copyProperties(Object source, Object target) throws BeansException {
        copyProperties(source, target, null, (String[]) null);
    }
 
 
    private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
            throws BeansException {
 
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
 
        Class<?> actualEditable = target.getClass();
        if (editable != null) {
            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<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
 
        for (PropertyDescriptor targetPd : targetPds) {
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreProperties == 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 (value != null) {
                                if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                    writeMethod.setAccessible(true);
                                }
                                writeMethod.invoke(target, value);
                            }
                        }
                        catch (Exception ex) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", ex);
                        }
                    }
                }
            }
        }
    }
}

 

 客户端提交的数据又问题:

   sattus: 422  ;  messags:密码无效。  校验不通过

 

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions.

 

 

 

 

 

一、

我们不能用常规操作文件的方法来读取ResourceJar.jar中的资源文件res.txt,但可以通过Class类的getResourceAsStream()方法来获取 ,这种方法是如何读取jar中的资源文件的,这一点对于我们来说是透明的。我们将Resource.java改写成:

 

public void getResource() throws IOException{  

        //返回读取指定资源的输入流  

        InputStream is=this.getClass().getResourceAsStream("/resource/res.txt");   

        BufferedReader br=new BufferedReader(new InputStreamReader(is));  

        String s="";  

        while((s=br.readLine())!=null)  

            System.out.println(s);  

    }  

 

(二)

 

x比如test.jar根目录下有个a.txt,可以用下面的代码获取:

 

URL url=new URL("jar:file:/C:/test.jar!/a.txt"); 

InputStream in=url.openStream(); 

byte b[]=new byte[1000]; 

in.read(b);

 

 

 File f=new File("C:/ResourceJar.jar!/resource/res.txt");

当然不可能,因为".../ResourceJar.jar!/resource/...."并不是文件资源定位符的格式 (jar中资源有其专门的URL形式: 

{*}jar:<url>!/

 

 

现在实现
  • 采用流的方式进行处理,同时读取流时设置编码utf-8
  • 使用InputStream inputStream=this.getClass().getResourceAsStream("") 会指定要加载的资源路径与当前类所在包的路径一致。因此能正常读取文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值