这次我们主要讲一下后端代码
用户实体类
@Entity
@Data
@Builder
public class SysUser {
@Id
@GeneratedValue
private Integer userId;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String password;
private String nickname;
private String avatar;
private String description;
private Integer wordsCount;
private Integer likeCount;
}
- Respository
public interface SysUserRepositry extends JpaRepository<SysUser, Integer> {
/**
* 根据邮箱查找用户
* @param email
* @return
*/
SysUser findSysUserByEmail(String email);
/**
* 根据热度降序排列
* JPQL语句
* @return
*/
@Query("FROM SysUser u ORDER BY u.likeCount DESC ")
List<SysUser> findHotUsers();
}
Service及其实现类
public interface SysUserService {
ResponseUtil userLogin(LoginUser loginUser);
List<SysUser> getHotUsers();
}
@Service
public class SysUserServiceImpl implements SysUserService {
@Resource
private SysUserRepositry sysUserRepositry;
@Override
public ResponseUtil userLogin(LoginUser loginUser) {
SysUser sysUser = sysUserRepositry.findSysUserByEmail(loginUser.getEmail());
//邮箱有效
if (sysUser != null) {
//判断密码是否正确
if (loginUser.getPassword().equals(sysUser.getPassword())) {
return new ResponseUtil(0, "login success", sysUser);
} else {
return new ResponseUtil(0, "password wrong");
}
} else {
return new ResponseUtil(0, "email wrong");
}
}
@Override
public List<SysUser> getHotUsers() {
return sysUserRepositry.findHotUsers();
}
}
- Controller层
@RestController
@RequestMapping("/user")
@CrossOrigin("http://localhost")
public class SysUserController {
@Resource
private SysUserService sysUserService;
@RequestMapping(value = "/sign_in", method = RequestMethod.POST)
public ResponseUtil signIn(@RequestBody LoginUser loginUser) {
System.out.println(loginUser);
return sysUserService.userLogin(loginUser);
}
@RequestMapping(value = "/hot", method = RequestMethod.GET)
public ResponseUtil getHotUsers() {
return new ResponseUtil(0, "get hot users!", sysUserService.getHotUsers());
}
}