开发数据流动遇到的常见问题

本文介绍了如何在数据库操作中进行复杂的SQL查询,包括左连接和分组聚合。同时,讲解了使用Orika或BeanUtils进行对象间的属性拷贝,以及LocalDateTime的时间格式处理。在序列化过程中,利用JsonFormat注解处理日期格式,并展示了使用Jackjson的方法。此外,还讨论了如何在方法上应用Validated注解进行参数校验,以及如何将Integer转换为枚举类型。文章最后提供了MyBatis入门的相关链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从数据库拿数据遇到的问题

组合对象的查询

  1. 写跨表查询的sql
  2. 定义对应的BO 对象
  3. resultType 为BO 类型

组合对象一对多查询

select u.username,group_concat(r.name),min(r.priority) from user u left join user_role ur on u.id = ur.user_id left outer join role r on r.id = ur.role_id group by username

数据之间的拷贝

Orika https://blog.csdn.net/weixin_38331049/article/details/118682486

BeanUtils.copyProperties()

时间的处理

  1. 如果需要序列化的 JsonForamt 来回都要jackjson 才能生效

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public class Person {
    
        private Long id;
    
        @Length(min = 2, max = 20, message = "姓名在2-10个字母之间")
        private String name;
        @Range(min = 0,max = 100,message = "年龄不在有效范围内")
        private Integer age;
        @NotNull
        @JsonFormat( pattern = DatePattern.NORM_DATETIME_PATTERN,timezone="GMT+8")
        private LocalDateTime birthday;
    }
    
    @PostMapping("/add/person")
    public Collection<Person> addPerson(@Validated @RequestBody Person person){
        persons.put(person.getId(),person);
        return persons.values();
        //        return JSON.toJSONString(persons.values());
        //        final ObjectMapper objectMapper = new ObjectMapper();
        //        return objectMapper.writeValueAsString(persons.values());
    }
    
  2. 本地日期序列化

    final LocalDateTime now = LocalDateTime.now();
    final String format = now.format(DatePattern.CHINESE_DATE_TIME_FORMATTER);
    System.out.println(format);
    

字段的效验

在方法加 Validated

https://mp.csdn.net/mp_blog/creation/success/123602054

枚举类型的处理

  1. 将Integer 通过枚举解析

参考文档

入门_MyBatis中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值