Spring Data JPA进阶+swagger练习

  • 实体类
package com.example.entity;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;

/**
 * Created by 史冬阳 on 2018/9/27.
 */
@Entity
@Data
public class SysUser implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String account;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private String nickname;

    private Integer fans;

    public SysUser(String account, String password, String nickname, Integer fans) {
        this.account = account;
        this.password = password;
        this.nickname = nickname;
        this.fans = fans;
    }



    public SysUser() {
    }
}
  • dao层
package com.example.dao;

import com.example.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by 史冬阳 on 2018/9/27.
 */
public interface UserRepository extends JpaRepository {

    /**
     * 根据账号查询用户
     * @param account
     * @return
     */
    SysUser findByAccount(String account);

    /**
     * 根据粉丝数量筛选
     * @param num1
     * @param num2
     * @return
     */
    List findByFansBetween(Integer num1,Integer num2);

    /**
     * 根据昵称关键字查询
     * @param nickname
     * @return
     */
    List findByNicknameLike(String nickname);

    /**
     * 查询昵称含有指定关键词并且按粉丝数量降序排列
     * @param nickname
     * @return
     */
    @Query("from SysUser u where u.nickname  like CONCAT('%',:nickname,'%') order by  u.fans desc ")
    List findUsers(@Param("nickname") String nickname);

    //根据昵称查询,自定义JPQL语句
    @Query("from SysUser u where u.nickname=:nickname")
    SysUser findUser(@Param("nickname") String nickname);
}
  • Service层
package com.example.service;

import com.example.entity.SysUser;


import java.util.List;

/**
 * Created by 史冬阳 on 2018/9/27.
 */
public interface UserService {
    SysUser save(SysUser sysUser);

    //查询所有用户
    List getAll();

    //根据id查询用户信息
    SysUser get(Long id);

    //根据账号查询用户信息
    SysUser findByAccount(String account);

    //根据昵称关键字模糊查询用户信息
    List findByNicknameLike(String nickname);

    //查询昵称含有指定关键词并且按粉丝数降序排列
    List getUsersOrderByFans(String nickname);

    //根据粉丝数量筛选用户
    List findByFansBetween(Integer num1,Integer num2);
}
  • Service层的实现类
package com.example.service.impl;

import com.example.dao.UserRepository;
import com.example.entity.SysUser;
import com.example.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 史冬阳 on 2018/9/27.
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserRepository userRepository;


    @Override
    @Transactional
    public SysUser save(SysUser sysUser) {
        return userRepository.save(sysUser);
    }

    @Override
    public List getAll() {
        return userRepository.findAll();
    }

    @Override
    @Transactional
    public SysUser get(Long id) {
        return userRepository.findById(id).get();
    }

    @Override
    public SysUser findByAccount(String account) {
        return userRepository.findByAccount(account);
    }

    @Override
    public List findByNicknameLike(String nickname) {
        return userRepository.findByNicknameLike(nickname);
    }

    @Override
    public List getUsersOrderByFans(String nickname) {
        return  userRepository.findUsers(nickname);
    }

    @Override
    public List findByFansBetween(Integer num1, Integer num2) {
        return userRepository.findByFansBetween(num1,num2);
    }
}
  • Controller层
package com.example.dao;

import com.example.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by 史冬阳 on 2018/9/27.
 */
public interface UserRepository extends JpaRepository {

    /**
     * 根据账号查询用户
     * @param account
     * @return
     */
    SysUser findByAccount(String account);

    /**
     * 根据粉丝数量筛选
     * @param num1
     * @param num2
     * @return
     */
    List findByFansBetween(Integer num1,Integer num2);

    /**
     * 根据昵称关键字查询
     * @param nickname
     * @return
     */
    List findByNicknameLike(String nickname);

    /**
     * 查询昵称含有指定关键词并且按粉丝数量降序排列
     * @param nickname
     * @return
     */
    @Query("from SysUser u where u.nickname  like CONCAT('%',:nickname,'%') order by  u.fans desc ")
    List findUsers(@Param("nickname") String nickname);

    //根据昵称查询,自定义JPQL语句
    @Query("from SysUser u where u.nickname=:nickname")
    SysUser findUser(@Param("nickname") String nickname);

}
  • dao层的test类
package com.example.dao;

import com.example.entity.SysUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import java.util.List;

import static org.junit.Assert.*;

/**
 * Created by 史冬阳 on 2018/9/27.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {
    @Resource
    private UserRepository userRepository;

    @Test
    public void initData(){
        //创建六条数据
        SysUser[] users = {
                new SysUser("[email protected]","111","哈哈哈",45),
                new SysUser("[email protected]","222","",85),
                new SysUser("[email protected]","333","土鼠",75),
                new SysUser("[email protected]","444","星帽",280),
                new SysUser("[email protected]","555","小贝",289),
                new SysUser("[email protected]","666","路边看雪的小女孩",1056),
        };
        for (int i = 0 ; i < 6 ; i++ ){
            userRepository.save(users[i]);
        }
    }

    @Test
    public void findByAccount(){
        SysUser sysUser = userRepository.findByAccount("[email protected]");
        System.out.println(sysUser);
    }

    @Test
    public void findByFansBetween(){
        List sysUsers = userRepository.findByFansBetween(0,100);
        sysUsers.forEach(sysUser -> System.out.println(sysUser));
    }

    @Test
    public void findUsers() {
        List users = userRepository.findUsers("简");
        users.forEach(sysUser -> System.out.println(sysUser));
    }


}

你可能感兴趣的:(Spring Data JPA进阶+swagger练习)