好友表:
好友表 | tb_friend | ||
---|---|---|---|
字段名称 | 字段含义 | 字段类型 | 备注 |
userid | 用户ID | 文本 | |
friendid | 好友ID | 文本 | |
islike | 是否互相喜欢 | 文本 | 0:单向喜欢 1:互相喜欢 |
非好友表:
好友表 | tb_nofriend | ||
---|---|---|---|
字段名称 | 字段含义 | 字段类型 | 备注 |
userid | 用户ID | 文本 | |
friendid | 好友ID | 文本 |
1)当用户登陆后在推荐好友列表中点击“心”,表示喜欢此人 ,在数据库tb_friend表中插入一条数据,islike 为0;
2)当你点击了喜欢过的人,也喜欢了你 , 表示互粉成功!也向tb_friend表中插入一条数据,islike为1 ,并且将你喜欢她的数据islike也修改为1;
3)当你点击了不喜欢某人(点击了叉),向tb_nofriend添加记录;
4)当两个人互粉后,其中一人不喜欢对方了,删除好友表中的记录 ,向非好友表中添;
加记录
什么场景下使用springCloud呢?
我们来看用户表,有两列: fanscount 表示粉丝数 ,followcount表示关注数
1)当用户点击了喜欢:
比如小宝关注了楚楚,小宝的followcount(关注数)加1 , 楚楚的fanscount (粉丝数)加1;
2)当用户删除了好友:
比如楚楚删除了好友小宝,小宝的fanscount (粉丝数)减1 ,楚楚的followcount(关注数)减1;
org.springframework.boot
spring-boot-starter-data-jpa
com.tensquare
tensquare_common
1.0-SNAPSHOT
mysql
mysql-connector-java
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
server:
port: 9010
spring:
application:
name: tensquare-friend
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.2.100:3306/tensquare_friend?characterEncoding=utf-8
username: root
password: 888888
jpa:
database: MySQL
show-sql: true
generate-ddl: true
rabbitmq:
host: 192.168.2.10
eureka:
client:
serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
defaultZone: http://192.168.2.10:6868/eureka/
instance:
prefer-ip-address: true #把自己的ip地址暴露给Eureka服务器端
jwt:
config:
key: youcan
package com.tensquare.friend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import util.JwtUtil;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class FriendApplication {
public static void main(String[] args) {
SpringApplication.run(FriendApplication.class);
}
@Bean
public JwtUtil jwtUtil(){
return new JwtUtil();
}
}
4)创建JwtInterceptor(参见User工程)
5)创建InterceptorConfig(参见User工程)
package com.tensquare.friend.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "tb_friend")
@IdClass(Friend.class)
public class Friend implements Serializable{
@Id
private String userid;
@Id
private String friendid;
private String islike;
......
}
package com.tensquare.friend.dao;
import com.tensquare.friend.pojo.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface FriendDao extends JpaRepository<Friend, String> {
public Friend findByUseridAndFriendid(String userid, String friendid);
@Modifying
@Query(value = "UPDATE tb_friend SET islike=? WHERE userid = ? AND friendid = ?", nativeQuery = true)
public void updateIslike(String islike, String userid, String friendid);
@Modifying
@Query(value = "delete FROM tb_friend WHERE userid = ? AND friendid = ?", nativeQuery = true)
void deletefriend(String userid, String friendid);
}
创建com.tensquare.friend.service包,包下建立类FriendService
package com.tensquare.friend.service;
import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.dao.NoFriendDao;
import com.tensquare.friend.pojo.Friend;
import com.tensquare.friend.pojo.NoFriend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class FriendService {
@Autowired
private FriendDao friendDao;
@Autowired
private NoFriendDao noFriendDao;
public int addFriend(String userid, String friendid) {
//先判断userid到friendid是否有数据,有就是重复添加好友,返回0
Friend friend = friendDao.findByUseridAndFriendid(userid, friendid);
if(friend!=null){
return 0;
}
//直接添加好友,让好友表中userid到friendid方向的type为0
friend = new Friend();
friend.setUserid(userid);
friend.setFriendid(friendid);
friend.setIslike("0");
friendDao.save(friend);
//判断从friendid到userid是否有数据,如果有,把双方的状态都改为1
if(friendDao.findByUseridAndFriendid(friendid, userid)!=null){
//把双方的islike都改成1
friendDao.updateIslike("1", userid, friendid);
friendDao.updateIslike("1", friendid, userid);
}
return 1;
}
public int addNoFriend(String userid, String friendid) {
//先判断是否已经是非好友
NoFriend noFriend = noFriendDao.findByUseridAndFriendid(userid, friendid);
if(noFriend!=null){
return 0;
}
noFriend = new NoFriend();
noFriend.setUserid(userid);
noFriend.setFriendid(friendid);
noFriendDao.save(noFriend);
return 1;
}
public void deleteFriend(String userid, String friendid) {
//删除还有表中userid到friendid这条数据
friendDao.deletefriend(userid, friendid);
//更新friendid到userid的islike为0
friendDao.updateIslike("0", friendid, userid);
//非好友表中添加数据
NoFriend nofriend = new NoFriend();
nofriend.setUserid(userid);
nofriend.setFriendid(friendid);
noFriendDao.save(nofriend);
}
}
创建包com.tensquare.friend.controller,包下创建FriendController
package com.tensquare.friend.controller;
import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/friend")
public class FriendController {
@Autowired
private HttpServletRequest request;
@Autowired
private FriendService friendService;
@Autowired
private UserClient userClient;
/**
* 添加好友或者添加非好友
* @return
*/
@RequestMapping(value = "/like/{friendid}/{type}", method = RequestMethod.PUT)
public Result addFriend(@PathVariable String friendid, @PathVariable String type){
//验证是否登录,并且拿到当前登录的用户id
Claims claims = (Claims) request.getAttribute("claims_user");
if(claims==null) {
//说明当前用户没有user角色
return new Result(false, StatusCode.LOGINERROR, "权限不足");
}
//得到当前登录的用户id
String userid = claims.getId();
//判断是添加好友还是添加非好友
if(type!=null){
if(type.equals("1")){
//添加好友
int flag = friendService.addFriend(userid, friendid);
if(flag==0){
return new Result(false, StatusCode.ERROR, "不能重复添加好友");
}
if(flag==1){
userClient.updatefanscountandfollowcount(userid, friendid, 1);
return new Result(true, StatusCode.OK, "添加成功");
}
}else if (type.equals("2")){
//添加非好友
int flag = friendService.addNoFriend(userid, friendid);
if(flag==0){
return new Result(false, StatusCode.ERROR, "不能重复添加非好友");
}
if(flag==1){
return new Result(true, StatusCode.OK, "添加成功");
}
}
return new Result(false, StatusCode.ERROR, "参数异常");
}else {
return new Result(false, StatusCode.ERROR, "参数异常");
}
}
@RequestMapping(value = "/{friendid}", method = RequestMethod.DELETE)
public Result deleteFriend(@PathVariable String friendid){
//验证是否登录,并且拿到当前登录的用户id
Claims claims = (Claims) request.getAttribute("claims_user");
if(claims==null){
//说明当前用户没有user角色
return new Result(false, StatusCode.LOGINERROR, "权限不足");
}
//得到当前登录的用户id
String userid = claims.getId();
friendService.deleteFriend(userid, friendid);
userClient.updatefanscountandfollowcount(userid, friendid, -1);
return new Result(true, StatusCode.OK, "删除成功");
}
}
启动用户微服务,登陆用户获取token
启动交友微服务,通过postMan测试 http://localhost:9010/friend/like/1/1,
添加头信息 Authorization 内容为Bearer 加上获取的token
需求:当用户点击不喜欢某人,将对方的ID记录不喜欢列表中
package com.tensquare.friend.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "tb_nofriend")
@IdClass(NoFriend.class)
public class NoFriend implements Serializable {
@Id
private String userid;
@Id
private String friendid;
......
}
package com.tensquare.friend.dao;
import com.tensquare.friend.pojo.NoFriend;
import org.springframework.data.jpa.repository.JpaRepository;
public interface NoFriendDao extends JpaRepository<NoFriend, String> {
public NoFriend findByUseridAndFriendid(String userid, String friendid);
}
package com.tensquare.friend.service;
import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.dao.NoFriendDao;
import com.tensquare.friend.pojo.Friend;
import com.tensquare.friend.pojo.NoFriend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class FriendService {
@Autowired
private FriendDao friendDao;
@Autowired
private NoFriendDao noFriendDao;
public int addNoFriend(String userid, String friendid) {
//先判断是否已经是非好友
NoFriend noFriend = noFriendDao.findByUseridAndFriendid(userid, friendid);
if(noFriend!=null){
return 0;
}
noFriend = new NoFriend();
noFriend.setUserid(userid);
noFriend.setFriendid(friendid);
noFriendDao.save(noFriend);
return 1;
}
}
public int addFriend(String userid, String friendid) {
//先判断userid到friendid是否有数据,有就是重复添加好友,返回0
Friend friend = friendDao.findByUseridAndFriendid(userid, friendid);
if(friend!=null){
return 0;
}
//直接添加好友,让好友表中userid到friendid方向的type为0
friend = new Friend();
friend.setUserid(userid);
friend.setFriendid(friendid);
friend.setIslike("0");
friendDao.save(friend);
//判断从friendid到userid是否有数据,如果有,把双方的状态都改为1
if(friendDao.findByUseridAndFriendid(friendid, userid)!=null){
//把双方的islike都改成1
friendDao.updateIslike("1", userid, friendid);
friendDao.updateIslike("1", friendid, userid);
}
return 1;
}
@Modifying
@Query(value = "delete FROM tb_friend WHERE userid = ? AND friendid = ?", nativeQuery = true)
void deletefriend(String userid, String friendid);
public void deleteFriend(String userid, String friendid) {
//删除还有表中userid到friendid这条数据
friendDao.deletefriend(userid, friendid);
//更新friendid到userid的islike为0
friendDao.updateIslike("0", friendid, userid);
//非好友表中添加数据
NoFriend nofriend = new NoFriend();
nofriend.setUserid(userid);
nofriend.setFriendid(friendid);
noFriendDao.save(nofriend);
}
@RequestMapping(value = "/{friendid}", method = RequestMethod.DELETE)
public Result deleteFriend(@PathVariable String friendid){
//验证是否登录,并且拿到当前登录的用户id
Claims claims = (Claims) request.getAttribute("claims_user");
if(claims==null){
//说明当前用户没有user角色
return new Result(false, StatusCode.LOGINERROR, "权限不足");
}
//得到当前登录的用户id
String userid = claims.getId();
friendService.deleteFriend(userid, friendid);
userClient.updatefanscountandfollowcount(userid, friendid, -1);
return new Result(true, StatusCode.OK, "删除成功");
}
1)修改tensquare_user工程的UserDao,增加方法定义
package com.tensquare.user.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.tensquare.user.pojo.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
/**
* 数据访问接口
* @author Administrator
*/
public interface UserDao extends JpaRepository<User,String>,JpaSpecificationExecutor<User> {
public User findByMobile(String mobile);
@Modifying
@Query(value = "update tb_user set fanscount=fanscount+? where id=?", nativeQuery = true)
public void updatefanscount(int x, String friendid);
@Modifying
@Query(value = "update tb_user set followcount=followcount+? where id=?", nativeQuery = true)
public void updatefollowcount(int x, String userid);
}
2)UserService增加方法
@Transactional
public void updatefanscountandfollowcount(int x, String userid, String friendid) {
userDao.updatefanscount(x, friendid);
userDao.updatefollowcount(x, userid);
}
3)UserController增加方法
/**
* 更新好友粉丝数和用户关注数
* @return
*/
@RequestMapping(value = "/{userid}/{friendid}/{x}", method = RequestMethod.PUT)
public void updatefanscountandfollowcount(@PathVariable String userid, @PathVariable String friendid, @PathVariable int x){
userService.updatefanscountandfollowcount(x, userid, friendid);
}
package com.tensquare.friend.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Component
@FeignClient("tensquare-user")
public interface UserClient {
@RequestMapping(value = "/user/{userid}/{friendid}/{x}", method = RequestMethod.PUT)
public void updatefanscountandfollowcount(
@PathVariable("userid") String userid,
@PathVariable("friendid") String friendid,
@PathVariable("x") int x);
}
说明:
1)@FeignClient注解用于指定从哪个服务中调用功能;
2)@RequestMapping注解用于对被调用的微服务进行地址映射。
注意 @PathVariable注解一定要指定参数名称,否则出错;
修改addFriend方法,增加对userClient方法的调用
@Autowired
private UserClient userClient;
@Autowired
private UserClient userClient;
/**
* 添加好友或者添加非好友
* @return
*/
@RequestMapping(value = "/like/{friendid}/{type}", method = RequestMethod.PUT)
public Result addFriend(@PathVariable String friendid, @PathVariable String type){
//验证是否登录,并且拿到当前登录的用户id
Claims claims = (Claims) request.getAttribute("claims_user");
if(claims==null){
//说明当前用户没有user角色
return new Result(false, StatusCode.LOGINERROR, "权限不足");
}
//得到当前登录的用户id
String userid = claims.getId();
//判断是添加好友还是添加非好友
if(type!=null){
if(type.equals("1")){
//添加好友
int flag = friendService.addFriend(userid, friendid);
if(flag==0){
return new Result(false, StatusCode.ERROR, "不能重复添加好友");
}
if(flag==1){
userClient.updatefanscountandfollowcount(userid, friendid, 1);
return new Result(true, StatusCode.OK, "添加成功");
}
}else if (type.equals("2")){
//添加非好友
int flag = friendService.addNoFriend(userid, friendid);
if(flag==0){
return new Result(false, StatusCode.ERROR, "不能重复添加非好友");
}
if(flag==1){
return new Result(true, StatusCode.OK, "添加成功");
}
}
return new Result(false, StatusCode.ERROR, "参数异常");
}else {
return new Result(false, StatusCode.ERROR, "参数异常");
}
}
修改FriendService的deleteFriend方法 ,增加调用
public void deleteFriend(String userid, String friendid) {
//删除还有表中userid到friendid这条数据
friendDao.deletefriend(userid, friendid);
//更新friendid到userid的islike为0
friendDao.updateIslike("0", friendid, userid);
//非好友表中添加数据
NoFriend nofriend = new NoFriend();
nofriend.setUserid(userid);
nofriend.setFriendid(friendid);
noFriendDao.save(nofriend);
}