JAVA 草稿

1. 项目启动报错:Failed to start bean 'documentationPluginsBootstrapper'

度娘解释:swagger 的匹配模式进行调整导致,导致默认的匹配默认在spring boot中不会使用,导致报错。
处理方式:(不建议降低spring boot 版本,调整太大了)

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

2. spring gateway路由出现503

是因为在Spring Cloud 2020版本以后,默认移除了对Netflix的依赖,其中就包括Ribbon,官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon,并成为了Spring Cloud负载均衡器的唯一实现,只需要将含有这个过滤器的依赖进行导入就行了

<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

4. List 转为 Map

public void test4() {
        Student stu1 = new Student("张三", 23);
        Student stu2 = new Student("李四", 24);
        Student stu3 = new Student("王五", 25);
        Student stu4 = new Student("赵六", 23);
        Student stu5 = new Student("前七", 25);
 
        List<Student> strList = new ArrayList<>();
        Collections.addAll(strList, stu1, stu2, stu3, stu4,stu5);
        // 1.根据age作为key,name作为value转map(age相同时前面覆盖后面的数据)
        Map<Integer, String> collect = strList.stream().collect(Collectors.toMap(Student::getAge, Student::getName, (key1,key2) -> key1 ));
        for (Map.Entry<Integer, String> integerStudentEntry : collect.entrySet()) {
            System.out.println(integerStudentEntry.getKey() + ":" + String.valueOf(integerStudentEntry.getValue()));
        }
 
        // 2.根据age作为key,student对象作为value转map(age相同时前面覆盖后面的数据)
        Map<Integer, Student> collectStu = strList.stream().collect(Collectors.toMap(Student::getAge, Function.identity(), (key1, key2) -> key2));
        for (Map.Entry<Integer, Student> integerStudentEntry : collectStu.entrySet()) {
            System.out.println(integerStudentEntry.getKey() + "::" + String.valueOf(integerStudentEntry.getValue()));
        }
 
        // 3.根据age作为key,student对象作为value分组转map(age相同时前面覆盖后面的数据)
        Map<Integer, List<Student>> listMap = strList.stream().collect(Collectors.groupingBy(Student::getAge));
        for (Map.Entry<Integer, List<Student>> integerStudentEntry : listMap.entrySet()) {
            System.out.println(integerStudentEntry.getKey() + "::" + String.valueOf(integerStudentEntry.getValue()));
        }
    }

5. MVC(MODEL、VIEW、Controller)

软件构建模式,常见的分层思想,主要用于网站建立。

  • 视图层(view):是指界面层,也就是HTML代码那部分
  • 模型图(model):是对应的页面,给界面使用的Model实体类
  • 控制层(controller):可以操作模型层的实体,它控制数据流向模型对象,并在数据变化时更新视图。它强制使用视图与模型分开

6. WebAPI(对接方式)

是用于服务和服务对接方式,现阶段多用于前后端分离。
开发模式类似MVC,只是view层,由后端开发改为前端开发。


7. MVVM(Model-View-ViewModel)

软件设计模式,MVC 的升级版。前端架构vue
View: 没变。与MVC一样,就是页面,被css渲染后的html页面。永远不能与model进行交互。
Model:感觉没变。

  • MVVM中Model 是指整个后端,包括数据与业务逻辑,我认为就是指对接返回的数据
    • 数据和业务逻辑
    • 由data entities, business objects, repositories and services构成业务流程
  • MVC中Model 是指数据库查询后的数据实体类
    • 由data entities, dto 构成的数据载体

ViewModel: 与 controller 相同也不同。

  • 相同点:都是沟通 models与view 的渠道。
  • 不同点:
    • ViewModel紧连view,归属于前端部分,就是vue中我们开发的部分。
    • Controller归属于后端部分

image


8. yml文件定义参数

String、对象、集合、map、ListMap(集合套map) 添加实例
# 自定义参数
cus:
  val: 张三
  bean: 
    id: 20
    name: 张三
  arr1: 1, 3, 4
  arr2: [1, 3, 4]
  list:
    - lista
    - listb
  map:
    a: mapa
    b: mapb
  listmap:
    - a: a
      b: b
      c: c
    - d: d
      e: e
      f: f

获取方法1:
@Value获取yml文件中单个配置值,可以是字符串、对象、数组等,复制到对应代码的对象中
例如:

@Value("${cus.arr1}")
private String cusVal;

获取方法2:
@ConfigurationProperties("cus") 获取yml文件中多个配置值,按照引用名称进行匹配。可以认为是多个@value一起使用

@ConfigurationProperties(prefix="cus")
public class CustomParam {
	
	private String val;
	private User bean;
	private String[] arr1;
	private String[] arr2;
	private List<String> list;
	private Map<String, String> map;
	private List<Map<String, String>> listmap;
}

9. Redis 内容乱码

  设置 redisTemplate 的序列化。序列化类型,key 使用 string类型序列化,value 使用 json类型序列化

public class RedisUtils {
    private RedisTemplate<Object, Object> redisTemplate;

    public RedisUtils(RedisTemplate<Object, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
	//
	// 设置编码类型,key 使用 string类型序列化,value 使用 json类型序列化
	// json 类型可以存储string、number类型值,不影响反序列化
        this.redisTemplate.setKeySerializer(new StringRedisSerializer());
        this.redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        this.redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        this.redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    }
}	

10. MapStruct 部分字段没有映射成功

  项目中使用MapStruct进行转换,在实体类UserUserDto中添加新的字段,但是项目运行后,并没有进行赋值。其它字段转换没有问题。

  MapStruct的原理:通过映射方式,将UserUserDto中对应属性相互赋值

可能存在问题1
  字段名称不同。在不使用@mapping注释时,二者之间是通过字段名称进行映射的。
可能存在问题2:
  实现类没有更新。MapStruct会自动生成实现类,也就是class文件。由于是MapStruct生成的,并不会随之代码的变动而实时变动。通过jd-gui.exe反编译查看impl文件,可以发现此问题。
  处理方式:使用mavenclean命令,可以清除class文件(trage文件夹)
image


11. java: 错误: 无效的源发行版:17

  报错原因是JDK版本不一致。IDE设置的项目JDK与写代码时的JDK不一致。

  实际上,我的问题是,在IDEA创建的springboot 3+版本时选择配置了jdk8。但springboot 3+版本已经放弃了JDK8,默认改为JDK17。所以明面上不管怎么调整JDK8的配置,都会报这个错误。
  改为JDK17就好了~~~~~~~
image


12. Oracle 跨库查询

--如果没有dblink权限则使用dba账号进行授权即可

--表示给账号授予创建公有dblink权限
grant create public database link to 需授权dblink的账号名称;
--表示给账号授予创建私有dblink权限
grant create database link to 需授权dblink的账号名称;
--表示给账号授予删除公有dblink权限
grant DROP PUBLIC DATABASE LINK to 需授权dblink的账号名称;

--创建 跨库链接,没有public,表示创建一个私有的连接
create public database link dblink名称 connect to 用户名 identified by 密码 using 'ip:1521/服务名'

--使用
select count(1) from tabname@dblink名称;

--删除已经存在的dblink
drop public database link 已经存在的dblink名称;
posted @ 2023-06-28 14:39  之士咖啡  阅读(10)  评论(0编辑  收藏  举报