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() {
}
}
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<SysUser,Long> {
/**
* 根据账号查询用户
* @param account
* @return
*/
SysUser findByAccount(String account);
/**
* 根据粉丝数量筛选
* @param num1
* @param num2
* @return
*/
List<SysUser> findByFansBetween(Integer num1,Integer num2);
/**
* 根据昵称关键字查询
* @param nickname
* @return
*/
List<SysUser> findByNicknameLike(String nickname);
/**
* 查询昵称含有指定关键词并且按粉丝数量降序排列
* @param nickname
* @return
*/
@Query("from SysUser u where u.nickname like CONCAT('%',:nickname,'%') order by u.fans desc ")
List<SysUser> findUsers(@Param("nickname") String nickname);
//根据昵称查询,自定义JPQL语句
@Query("from SysUser u where u.nickname=:nickname")
SysUser findUser(@Param("nickname") String nickname);
}
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<SysUser> getAll();
//根据id查询用户信息
SysUser get(Long id);
//根据账号查询用户信息
SysUser findByAccount(String account);
//根据昵称关键字模糊查询用户信息
List<SysUser> findByNicknameLike(String nickname);
//查询昵称含有指定关键词并且按粉丝数降序排列
List<SysUser> getUsersOrderByFans(String nickname);
//根据粉丝数量筛选用户
List<SysUser> findByFansBetween(Integer num1,Integer num2);
}
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<SysUser> 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<SysUser> findByNicknameLike(String nickname) {
return userRepository.findByNicknameLike(nickname);
}
@Override
public List<SysUser> getUsersOrderByFans(String nickname) {
return userRepository.findUsers(nickname);
}
@Override
public List<SysUser> findByFansBetween(Integer num1, Integer num2) {
return userRepository.findByFansBetween(num1,num2);
}
}
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<SysUser,Long> {
/**
* 根据账号查询用户
* @param account
* @return
*/
SysUser findByAccount(String account);
/**
* 根据粉丝数量筛选
* @param num1
* @param num2
* @return
*/
List<SysUser> findByFansBetween(Integer num1,Integer num2);
/**
* 根据昵称关键字查询
* @param nickname
* @return
*/
List<SysUser> findByNicknameLike(String nickname);
/**
* 查询昵称含有指定关键词并且按粉丝数量降序排列
* @param nickname
* @return
*/
@Query("from SysUser u where u.nickname like CONCAT('%',:nickname,'%') order by u.fans desc ")
List<SysUser> findUsers(@Param("nickname") String nickname);
//根据昵称查询,自定义JPQL语句
@Query("from SysUser u where u.nickname=:nickname")
SysUser findUser(@Param("nickname") String nickname);
}
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("ggg@qq.com","111","哈哈哈",45),
new SysUser("hhh@qq.com","222","简书",85),
new SysUser("iii@qq.com","333","土鼠",75),
new SysUser("jjj@qq.com","444","星帽",280),
new SysUser("kkk@qq.com","555","小贝",289),
new SysUser("lll@qq.com","666","路边看雪的小女孩",1056),
};
for (int i = 0 ; i < 6 ; i++ ){
userRepository.save(users[i]);
}
}
@Test
public void findByAccount(){
SysUser sysUser = userRepository.findByAccount("aaa@qq.com");
System.out.println(sysUser);
}
@Test
public void findByFansBetween(){
List<SysUser> sysUsers = userRepository.findByFansBetween(0,100);
sysUsers.forEach(sysUser -> System.out.println(sysUser));
}
@Test
public void findUsers() {
List<SysUser> users = userRepository.findUsers("简");
users.forEach(sysUser -> System.out.println(sysUser));
}
}