SpringBoot 1.5.9到2.0.0的踩坑记录

本文介绍从SpringDataJPA1.x版本迁移到2.0版本的关键变更,包括findOne方法替换为findById,save方法的使用变化,PageRequest构造方法的更改以及WebMvcConfigurerAdapter的弃用。

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

一.2018/3/19 09:13 findOne()
刚刚升级到2.0,发现service的实现类报错,发现findOne方法无法再根据id查询了
findOne(ID)-->findById(ID)
需要先判断findById的返回值
/**
 * If a value is present in this {@code Optional}, returns the value,
 * otherwise throws {@code NoSuchElementException}.
 *
 * @return the non-null value held by this {@code Optional}
 * @throws NoSuchElementException if there is no value present
 *
 * @see Optional#isPresent()
 */
public T get() {
    if (value == null) {
        throw new NoSuchElementException("No value present");
    }
    return value;
}

/**
 * 根据用户id查询用户信息
 *
 * @param id 用户ID
 * @return 查询结果
 */
@Override
public Result getUserInfo(Long id) {
    /*
     *
     * 版本更新说明:
     * Spring Data JPA 1.xx:
     * 根据ID查询使用的是:T findOne(ID var)
     * 需要对返回值进行null判断,以判断是否能根据ID查询到对应的对象
     * Spring Data JPA 2.xx:
     * 根据ID查询使用的方法是:Optional<T> findById(ID id)-->T t = Optional<T>.get();
     * Optional<T>是非null的,但是如果查不到的话,它的get方法会报错,no value present;
     * 所以在进行get之前,需要使用Optional.isPresent()方法进行判断
     *
     */
    Optional<User> userById = userDao.findById(id);
    if (userById.isPresent()) {
        return Result.ok().put("user", userById.get());
    } else {
        return Result.error(615, "无效的查询请求");
    }
}


二.2018/3/19 09:42 save方法报错
save只能保存单个对象,如果要保存所有的,需要saveAll
/**
 * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
 * entity instance completely.
 * 
 * @param entity must not be {@literal null}.
 * @return the saved entity will never be {@literal null}.
 */
<S extends T> S save(S entity);

/**
 * Saves all given entities.
 * 
 * @param entities must not be {@literal null}.
 * @return the saved entities will never be {@literal null}.
 * @throws IllegalArgumentException in case the given entity is {@literal null}.
 */
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
三.2018/3/19 10:28 PageRequest pageRequest = new PageRequest(int,int)过时,被静态方法of代替

/**
 * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return the first
 * page.
 *
 * @param page zero-based page index.
 * @param size the size of the page to be returned.
 * @deprecated use {@link #of(int, int)} instead.
 */
@Deprecated
public PageRequest(int page, int size) {
   this(page, size, Sort.unsorted());
}
/**
 * Creates a new unsorted {@link PageRequest}.
 *
 * @param page zero-based page index.
 * @param size the size of the page to be returned.
 * @since 2.0
 */
public static PageRequest of(int page, int size) {
   return of(page, size, Sort.unsorted());
}
四.2018/3/19 10:34 WebMvcConfigurerAdapter过时
/**
 * An implementation of {@link WebMvcConfigurer} with empty methods allowing
 * subclasses to override only the methods they're interested in.
 *
 * @author Rossen Stoyanchev
 * @since 3.1
 * @deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made
 * possible by a Java 8 baseline) and can be implemented directly without the
 * need for this adapter
 */
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
从继承WebMvcConfigurerAdapter变成实现WebMvcConfigurer
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值