首页 > 其他分享 >后端写法总结

后端写法总结

时间:2024-01-31 10:56:50浏览次数:39  
标签:总结 map return List gun param import 写法

一、类型转换之间的工具类

package com.hengan.citicPlatGunNew.utils;

import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc: 模型转化Util
 * @author: lc
 * @since: 2023/6/5
 */
public class ModelConverterUtils {
    private ModelConverterUtils(){

    }
    /**
     * 创建类的一个实例
     *
     * @param beanClass 类
     */
    public static <T> T newInstance(Class<T> beanClass) {
        try {
            return beanClass.newInstance();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 属性拷贝
     *
     * @param source 源对象
     * @param target 目标对象
     */
    public static void copyProperties(Object source, Object target) {
        if (source == null) {
            return;
        }
        BeanUtils.copyProperties(source, target);
    }

    /**
     * 对象转换
     *
     * @param source      源对象
     * @param targetClass 目标对象
     * @param <T>         目标对象class类型
     * @return 返回新的目标对象
     */
    public static <T> T convert(Object source, Class<T> targetClass) {
        if (source == null) {
            return null;
        }
        T target = newInstance(targetClass);
        copyProperties(source, target);
        return target;
    }

    /**
     * 对象List转换
     *
     * @param sources     源对象
     * @param targetClass 目标对象
     * @param <T>         目标对象class类型
     * @return 返回新的目标对象List
     */
    public static <T> List<T> convert(Collection<?> sources, Class<T> targetClass) {
        if (sources == null) {
            return null;
        }
        List<T> targets = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(sources)) {
            targets = sources.stream().map(x -> convert(x, targetClass)).collect(Collectors.toList());
        }
        return targets;
    }
}

二、跨库查询

1、在resources目录下新建一个文件constants.properties

2、内容 constants.dataSource=A,13808;

3、在java文件夹下新建config文件夹,新建类文件

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 2024-01-11
 */
@Configuration
@ConfigurationProperties(prefix = "constants", ignoreUnknownFields = false)
@PropertySource(value = "classpath:/constants.properties")
@Data
public class ConstantsConfig {
    private String dataSource;
}

4、获取参数文件中的内容信息

@Slf4j
@Service
public class ServiceImpl implements IService {
  @Resource
    GunUseFreqMapper gunUseFreqMapper;
   @Resource
    private ConstantsConfig constantsConfig;

        @Override
    public ResponseData getUsedCount() {
        Map<String,Object> resultMap = new HashMap<>();
        try{
            List<DataSourceEx> dataSourceList =new ArrayList<>();
            Map<String,Object> map = new HashMap<>();
            map.put("date", LocalDate.now());
            map.put("isDelete", Constants.SysOptionConf.DATA_STATUS_NORMAL.getCode());
            map.put("status",Constants.SysOptionConf.OUT_STATUS.getCode());
            map.put("isEnabled",Constants.SysOptionConf.USED_CODE.getCode());
            // 数据源
            String dataSource =constantsConfig.getDataSource();
            if(dataSource !=null){
                String[] configs = dataSource.split(";");
                for(String con : configs) {
                    DataSourceEx ex = new DataSourceEx();
                    String[] cons = con.split(",");
                    ex.setDbName(cons[0]);
                    ex.setOrgId(cons[1]);
                    dataSourceList.add(ex);
                }
            }
            // 获得使用率
            List<GunUsedFreq> list = gunUseFreqMapper.getGunUsedCount(dataSourceList,map);
            // 结果不为空
            if(CollectionUtils.isNotEmpty(list)){
                int totalCount =list.stream().mapToInt(GunUsedFreq::getTotalCount).sum();
                int usedCount = list.stream().mapToInt(GunUsedFreq::getUseCount).sum();
                resultMap.put("list",list);
                // 界面按照百分比显示
                resultMap.put("usedCount",usedCount);
                resultMap.put("totalCount",totalCount);
                resultMap.put("onCount",totalCount-usedCount);
            }
            return ResponseUtil.success(resultMap);
        }catch (Exception e){
            log.error(e.toString());
            return ResponseUtil.error(ResponseEnum.SERVICE_ERR);
        }
    }


}

5、mapper文件

package com.hengan.citicPlatGunNew.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hengan.citicPlatGunNew.entity.GunUsedFreq;
import com.hengan.citicPlatGunNew.entity.ex.DataSourceEx;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * <p>
Mapper 接口 * </p> * * @author hengan * @since 2022-01-12 */ public interface GunUseFreqMapper extends BaseMapper<GunUsedFreq> { List<GunUsedFreq> getGunUsedCount(@Param("list")List<DataSourceEx> dataSourceList, @Param("map") Map<String,Object> map); }

6、实现接口文件

<?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.hengan.citicPlatGunNew.mapper.GunUseFreqMapper">
    <select id="getGunUsedCount" parameterType="java.util.HashMap" resultType="com.hengan.citicPlatGunNew.entity.GunUsedFreq">
        <foreach collection="list" item="item" index="index" separator="UNION" >
            select a.totalCount,b.useCount,c.usedCount,(c.usedCount/a.totalCount) gunFreq, #{item.orgId} orgId
            from
            (select count(1) totalCount from `${item.dbName}`.base_gun_info where is_delete=#{map.isDelete} and is_enabled=#{map.isEnabled}) a,
            (select count(1) useCount from `${item.dbName}`.base_gun_info b left join `${item.dbName}`.busi_gun_acess_record_real r on b.gun_code = r.gun_code
                where r.gun_status=#{map.gunStatus} and b.is_delete=#{map.isDelete} and  b.is_enabled=#{map.isEnabled} and r.take_datetime like concat(#{map.date},'%')) b,
            (select count(1) usedCount from `${item.dbName}`.base_gun_info b left join `${item.dbName}`.busi_gun_acess_record_real r on b.gun_code = r.gun_code
             where  b.is_delete=#{map.isDelete} and  b.is_enabled=#{map.isEnabled} and r.take_datetime like concat(#{map.date},'%')) c
        </foreach>
    </select>
</mapper>

 

标签:总结,map,return,List,gun,param,import,写法
From: https://www.cnblogs.com/flyShare/p/17998743

相关文章

  • vue 前端写法总结
    一、图片 1、<divclass="loginDiv":style="'background-image:url('+Background+');'"> 2、 <img:src="Logo"class="img-logo"><script><!--引入样式-->import'@/assets/styl......
  • 今日总结
    <properties><spark.version>2.1.0</spark.version><scala.version>2.11</scala.version></properties><dependencies><dependency><groupId>org.apache.spark</groupId><artifa......
  • 大模型模型结构总结
    对比各个大模型的网络结构ps:使用自己的config,但是模型结构跟官方配置原理一致.chatglm3ChatGLMForConditionalGeneration((transformer):ChatGLMModel((embedding):Embedding((word_embeddings):Embedding(65024,4096))(rotary_pos_emb):Rotar......
  • ABC388-VP赛总结-A/B(日结)
    首先A题(一个difficultly为21的题卡了我20分钟)ProblemStatementYouaregivenanon-emptystring\(S\)consistingofuppercaseandlowercaseEnglishletters.Determinewhetherthefollowingconditionissatisfied:Thefirstcharacterof\(S\)isuppercase,and......
  • 2023年度总结:我们都在用力的活着,拼尽了全力,却换回了伤痕累累!!!
    阅前必读:2023你还记得让你听过最扎心的话吗?你印象里记得你做的哪些不如意痛心的事吗?当你的付出得不到回报的时候。你有过绝望吗?闭上眼睛,想起过往时候,你流泪了吗?其实我并不害怕黑夜,我只是怕了孤单。走在那条回忆的路上,想我了血肉模糊的风景。承受过了背叛。其实并不是放不下。......
  • 如何做好一个信息系统项目经理,一个项目经理的个人体会和经验总结(二)
    前言之前文章讲了在项目开始阶段,作为一个信息系统项目经理应该做好哪些内容(参见如何做好一个信息系统项目经理,一个项目经理的个人体会和经验总结(一)),这一篇我们继续聊聊在项目开发阶段,项目经理又需要做好哪些事情呢?......
  • 设计模式一句话总结
    1.设计原则(SOLID原则)原则名字原则描述单一职责原则(S)功能只有一个开闭原则(O)开放扩展,关闭修改里氏替换原则(L)子类需要实现父类功能以保持兼容性接口隔离原则(I)不用的函数或者功能不要出现依赖倒置原则(D)细节依赖于抽象,约定优先迪米特法则只和朋友说话......
  • 每日总结2024年1月30日
    今天完成了歌声转换的第一阶段验收。我们小组选择的服务外包杯课题是A13的歌声转化,我们计划实现两个方面的功能。能够选择人声,然后根据导入的歌曲,替换原声,生成新的歌曲,这是我们计划的基础目标。但是考虑到条件的局限性,我们很难让用户自行导入音色素材直接转化为需要的音色,目前计......
  • python获取表格数据总结
    获取表格内容:图片中首先import了两个模块,一个os一个openyxl,然后指定表格路径,打开表格。os:这里os在Python中,os.chdir()方法用于改变当前的工作目录。工作目录是指当前正在执行的脚本所在的目录。通过使用os.chdir()方法,我们可以在脚本执行过程中切换到不同的目录。openy......
  • Unity5.x shader打包AssetBundle总结
    unity5.x  shader打包AssetBundle总结最近比较忙,好久没有更新博客了,新项目切换到unity5.x后使用了新的打包机制,在打包shader的时候遇到了一些问题,这里来记录一下吧。 在上一个项目中,我们使用unity4.7,对于shader并没有进行依赖打包,而是由unity打包到了每个用到的AssetBundle......