Spring Boot 学习研究笔记(十) -SpringBoot JAP 踩坑总结

2023-11-05

SpringBoot JAP 踩坑总结

 

一、 JSON 字段映射处理流程

1、实现类型转换接口

package com.call.show.common.utils;
​
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.io.IOException;
import java.io.Serializable;
​
@Converter(autoApply = true)
public class JsonConverter implements AttributeConverter<Object,String>,Serializable {
​
    private final static ObjectMapper objectMapper = new ObjectMapper();
​
    @Override
    public String convertToDatabaseColumn(Object meta) {
        try {
            return objectMapper.writeValueAsString(meta);
        } catch (JsonProcessingException ex) {
            return null;
        }
    }
​
    @Override
    public Object convertToEntityAttribute(String dbData) {
        try {
            return objectMapper.readValue(dbData, Object.class);
        } catch (IOException ex) {
            return null;
        }
    }
}

 

2、实体类中添加注解

 @Convert(converter=JsonConverter.class)

例如:

/**
     * 账号信息 备用字段
     */
    @Column(name = "account_info")
    @Convert(converter=JsonConverter.class)
    private  String  accountInfo ;
​
    /**
     * 扩展信息备用字段
     */
    @Column(name = "extended_info")
    @Convert(converter=JsonConverter.class)
    private  String  extendedInfo ;

 

二、JPA中的jpql-@Query的查询使用

1、单参数查询

@Query(value="select * from cst_customer where cust_name=?1",nativeQuery = true)
​
@Query(value="from Customer  where cust_name= ?1")
​
@Query(value="select c from Customer  c where c.custName=?1")
​
@Query(value="from Customer c where c.custName=:#{#custName}")
​
@Query(value="from Customer c where c.custName=:custName") List<Customer> findAllCustomerByName(@Param("custName") String custName);
123456

这几种方式是等价的

  • @Query(value=“select * from cst_customer where cust_name= ?1”,nativeQuery = true)

  • @Query(value=“from Customer where cust_name= ?1”)

  • @Query(value=“select c from Customer c where c.custName=?1”)

  • @Query(value=“from Customer c where c.custName=:#{#custName}”)

  • @Query(value=“from Customer c where c.custName=:custName”)

 

2、多参数查询

@Query(value="from Customer  c where c.custId=?2 and c.custName=?1")
​
@Query(value="from Customer  c where c.custId=:#{#custId} and c.custName=:#{#custName}")
​
@Query(value="from Customer  c where c.custId=:custId and c.custName=:custName")
List<Customer> findCustomersByNameAndIndus(@Param("custName") String name,@Param("custId") Long id);
1234

这几种方式是等价的

  • @Query(value=“from Customer c where c.custId=?2 and c.custName=?1”)

  • @Query(value=“from Customer c where c.custId=:#{#custId} and c.custName=:#{#custName}”)

  • @Query(value=“from Customer c where c.custId=:custId and c.custName=:custName”)

 

3.传对象

这里需要特别注意,可能很多人会写错

传对象的语法是: :#{#对象名称.对象属性} 如下图

@Query(value="from Customer  c where c.custId=:#{#customer.custId}")
Customer findCustomerByInfo(@Param("customer") Customer customer);

 

三、自定义的@Query报异常

org.springframework.dao.InvalidDataAccessApiUsageException: For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters

通过@Query注解返回视图对象列表 :

public interface StudentRepository extends CrudRepository<Student, Integer> {
  @Query(
      "select new com.alphathur.jpademo.model.vo.StudentVo(name, age) from Student where salary = :salary ")
  List<StudentVo> findStudentVo(Double salary);
​
}

这个接口使用了‘ :参数名’ 来绑定参数,且没有对salary使用@Param参数,所以报错了。

 

解决问题方案:

加上@Param,将接口修改如下:

public interface StudentRepository extends CrudRepository<Student, Integer> {
  @Query(
      "select new com.alphathur.jpademo.model.vo.StudentVo(name, age) from Student where salary = :salary ")
  List<StudentVo> findStudentVo(@Param("salary") Double salary);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot 学习研究笔记(十) -SpringBoot JAP 踩坑总结 的相关文章

随机推荐

  • 基于FPGA的频率计

    1 简介 频率计又称为频率计数器 是一种专门对被测信号频率进行测量的电子测量仪器 2 传统测量法 传统测量法有两种 周期测量法 和 频率测量法 2 1 周期测量法 原理 先测出被测信号的周期 T T T 然后根据频率 f
  • 宏包algorithm与algorithmic引发的Undefined control sequence问题

    背景 自己是在texlive vs code环境下写小论文 在写算法的时候 一直出现输入控制语句全部都是没有定义的 如下 Undefined control sequence REQUIRE Undefined control sequen
  • Typora--图片上传方案Typora+PicGo+Gitee

    目录 一 简介 二 步骤 1 Gitee的部署 2 PicGo的设置 3 Typora的设置 三 其他 一 简介 当使用Typora的MarkDown编辑软件来做学习记录 上传图片时 都要创建一个文件夹来存放图片 这样子一来很不方便 有没有
  • 使用JDBC数据迁移把Mysql数据到另一个库中

    数据迁移 简介 整体思路链接2个需要迁移的数据库 根据sql 进行查询 判断什么样的数据需要迁移 什么样的数据需要过滤掉 数据重复或者出错的情况输出到某个文件中 如果数据可以整体迁移而且不出格式差异的情况也可以直接导出sql文件进行迁移 此
  • Binder 连接池的学习

    利用AIDL方式能很方便地进行客户端和服务端的跨进程通信 但是 我们想一下 如果按照我们之前的使用方法 必须满足一个AIDL接口对应一个service 那么问题来了 假如我们的应用 有很多业务场景 而每一个业务场景都需要和服务端通讯 那么我
  • MyBatis-Plus深入 —— 条件构造器与插件管理

    前言 在前面的文章中 荔枝梳理了一个MyBatis Plus的基本使用 配置和通用Service接口 我们发现在MyBatis Plus的辅助增强下我们不再需要通过配置xml文件中的sql语句来实现基本的sql操作了 不愧是最佳搭档 在这篇
  • keil5编译出现Error: L6411E:的解决办法

    出现这个问题很大的可能是keil5与ads产生冲突 此时只需要删除ads的环境变量即可 如下图 将其值含有ads的系统变量和用户变量全部删除 然后新建一个用户变量 变量名为ARMCC5LIB 其值要看你keil的安装路径 我的值是Y sof
  • android查看app是platform_app,system_app还是priv_app

    untrusted app 第三方app 没有Android平台签名 没有system权限 platform app 有android平台签名 没有system权限 system app 有android平台签名和system权限 从上面划
  • Windows10下载安装openjdk11及配置环境变量

    Windows10下载安装openjdk11及配置环境变量 下载JDK 首先我们需要下载java开发工具包JDK 下载地址 https cn azul com downloads zulu community version java 11
  • UE4外包团队:更新一下UE4和Unity3D案例

    全部的贴图都是用出的法线贴图构建的话只用了阳光和天光 都是静态光源 视角是第一人称模板最后的效果嘛就是全4K 120帧 0错误0警告 场景小是小了点但是效果还不错 工作活有时间更新 欢迎有UE4和Unity项目外包的联系我们 谢谢 转载于
  • CNN卷积核

    接着呢 我们需要处理我们的xs 把xs的形状变成 1 28 28 1 1代表先不考虑输入的图片例子多少这个维度 后面的1是channel的数量 因为我们输入的图片是黑白的 因此channel是1 例如如果是RGB图像 那么channel就是
  • Kafka - a simple consumer demo - c++

    Kafka 如果有 kafka 基础的同学可以不用看前面的废话 可以从第五条 配置 开始看起 代码在第七条 前言 官网比我这标准多了 官网跳转 大家可以先完成quickStart部分kafka单机生产消费 一 概念简介 Kafka 是一个分
  • 谷歌语法(详解+举例)

    一 谷歌语法是什么 谷歌语法就是利用搜索引擎在渗透测试过程搜索到特定页面的一种语法 二 如何利用谷歌语法 谷歌语法基础的符号 xxx 将要搜索的关键字用引号括起来 表示完全匹配 即关键词不能分开 顺序也不能变 例如 腾讯课堂如果不加 的话
  • 内存时延效能

    时延 Latency 小张一看到这个图 不禁大叫 太复杂了 看得我都犯密集恐惧症了 看不懂 没关系 我们拆开了一个个看 1 CL CAS Latency CL是指CAS发出之后 仍要经过一定的时间才能有数据输出 从CAS与读取命令发出到第一
  • 【华为OD统一考试A卷

    华为OD统一考试A卷 B卷 新题库说明 2023年5月份 华为官方已经将的 2022 0223Q 1 2 3 4 统一修改为OD统一考试 A卷 和OD统一考试 B卷 你收到的链接上面会标注A卷还是B卷 请注意 根据反馈 目前大部分收到的都是
  • 在云服务器存储数据的10个好处

    本文编辑 富哥 云服务器已经成为最适合在线存储数据的选项 在较早时期 大多数公司依靠内部服务器来存储他们不断增长的数据和在线文件 但今天将数据存储在在线的云服务器中已经成为新的趋势 因为它允许无限存储 将所有数据存储在云中最好的方面是确保可
  • Java中类的构成

    Java中的每个类一般包含属性 构造器 块 方法 内部类五部分 属性 用来定义对象的数据 构造器 构造器也是方法 每一个类中都一定会有构造器 包含有参构造器和无参构造器每一个对象在创建的时候都会调用构造器 如果没有构造器 系统将提供一个默认
  • 十大经典排序算法总结

    https blog csdn net hellozhxy article details 79911867
  • AI嵌入式全景:各厂商、系列和开发工具的综合概览

    要看几个方面 1 算力 2 支持何种模型 3 是否支持可视化的窗口系统 一般而言各个平台均采用linux操作系统 官方提供对应SDK 安装好后可使用硬件加速资源 而且如果要使用其硬件加速 一般都要完成模型转换 将模型转为该平台所特有的格式
  • Spring Boot 学习研究笔记(十) -SpringBoot JAP 踩坑总结

    SpringBoot JAP 踩坑总结 一 JSON 字段映射处理流程 1 实现类型转换接口 package com call show common utils import com fasterxml jackson core Json