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

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

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);


相关文章
|
3天前
|
XML Java 应用服务中间件
【JavaEE】JavaEE进阶:框架的学习 - Spring的初步认识
【JavaEE】JavaEE进阶:框架的学习 - Spring的初步认识
7 0
|
3天前
|
Java 开发工具 Maven
根据SpringBoot Guides完成进行示例学习(详细步骤)
根据SpringBoot Guides完成进行示例学习(详细步骤)
7 1
|
3天前
|
移动开发 前端开发 NoSQL
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
49 0
|
3天前
|
XML Java 数据库连接
Spring框架与Spring Boot的区别和联系
Spring框架与Spring Boot的区别和联系
24 0
|
3天前
|
存储 前端开发 Java
Spring Boot自动装配的源码学习
【4月更文挑战第8天】Spring Boot自动装配是其核心机制之一,其设计目标是在应用程序启动时,自动配置所需的各种组件,使得应用程序的开发和部署变得更加简单和高效。下面是关于Spring Boot自动装配的源码学习知识点及实战。
17 1
|
3天前
|
SQL Java 数据库连接
Springboot框架整合Spring JDBC操作数据
JDBC是Java数据库连接API,用于执行SQL并访问多种关系数据库。它包括一系列Java类和接口,用于建立数据库连接、创建数据库操作对象、定义SQL语句、执行操作并处理结果集。直接使用JDBC涉及七个步骤,包括加载驱动、建立连接、创建对象、定义SQL、执行操作、处理结果和关闭资源。Spring Boot的`spring-boot-starter-jdbc`简化了这些步骤,提供了一个在Spring生态中更便捷使用JDBC的封装。集成Spring JDBC需要添加相关依赖,配置数据库连接信息,并通过JdbcTemplate进行数据库操作,如插入、更新、删除和查询。
|
3天前
|
SQL Java 数据库连接
Springboot框架整合Spring Data JPA操作数据
Spring Data JPA是Spring基于ORM和JPA规范封装的框架,简化了数据库操作,提供增删改查等接口,并可通过方法名自动生成查询。集成到Spring Boot需添加相关依赖并配置数据库连接和JPA设置。基础用法包括定义实体类和Repository接口,通过Repository接口可直接进行数据操作。此外,JPA支持关键字查询,如通过`findByAuthor`自动转换为SQL的`WHERE author=?`查询。
|
3天前
|
缓存 监控 Java
【Spring系列笔记】AOP
面向切面编程就是面向特定方法编程。通过将横切关注点(cross-cutting concerns)从主要业务逻辑中分离出来,提供一种更好的代码模块化和可维护性。 横切关注点指的是在应用程序中横跨多个模块或层的功能,例如日志记录、事务管理、安全性、缓存、异常处理等。
25 0
|
3天前
|
存储 缓存 Java
【Spring系列笔记】依赖注入,循环依赖以及三级缓存
依赖注入: 是指通过外部配置,将依赖关系注入到对象中。依赖注入有四种主要方式:构造器注入、setter方法注入、接口注入以及注解注入。其中注解注入在开发中最为常见,因为其使用便捷以及可维护性强;构造器注入为官方推荐,可注入不可变对象以及解决循环依赖问题。本文基于依赖注入方式引出循环依赖以及三层缓存的底层原理,以及代码的实现方式。
24 0
|
3天前
|
XML Java 数据格式
Spring学习__一篇足矣
Spring学习__一篇足矣
Spring学习__一篇足矣