SpringBoot 1.5.9到2.0.0的踩坑记录

一.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 findById(ID id)-->T t = Optional.get();
     * Optional是非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

你可能感兴趣的:(JAVA)