4.4: Mapper层
```package com.laoyang.provider.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.laoyang.provider.dao.po.UserTagPO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;
/**
- @author:Kevin
- @create: 2023-08-01 09:54
@Description:
*/
@Mapper
public interface IUserTagMapper extends BaseMapper {/**
- 使用或的思路来设置标签,只能允许第一次设置成功
- @param userId
- @param fieldName
- @param tag
- @return
*/
@Update("update t_user_tag set ${fieldName}=${fieldName} | #{tag} where user_id=#{userId} and ${fieldName} & #{tag}=0")
int setTag(Long userId, String fieldName, long tag);
/**
* 使用先取反在与的思路来取消标签,只能允许第一次删除成功
* @param userId
* @param fieldName
* @param tag
* @return
*/
@Update("update t_user_tag set ${fieldName}=${fieldName} &~ #{tag} where user_id=#{userId} and ${fieldName} & #{tag}=#{tag}")
int cancelTag(Long userId, String fieldName, long tag);
}
说明:这里的sql可以参考开头看到的实现原理
4.5 工具类
4.5.1:对象转换类
```package com.laoyang.common.utils;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @author:Kevin
* @create: 2023-07-29 15:03
* @Description:
*/
public class ConvertBeanUtils {
/**
* 将一个对象转成目标对象
*/
public static <T> T convert(Object source,Class<T> targetClass){
if (source == null){
return null;
}
T t = newInstance(targetClass);
BeanUtils.copyProperties(source,t);
return t;
}
/**
* 将List对象转换成目标对象
*/
public static <K,T> List<T> convertList(List<K> sourceList, Class<T> targetClass){
if (sourceList == null){
return null;
}
List targetlist = new ArrayList((int) (sourceList.size() / 0.75) + 1);
for (K source : sourceList) {
targetlist.add(source);
}
return targetlist;
}
private static <T> T newInstance(Class<T> targetClass){
try {
return targetClass.newInstance();
}catch (Exception e){
throw new BeanInstantiationException(targetClass,"instantiation error",e);
}
}
}
4.5.3 po类
```package com.laoyang.provider.dao.po;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
/**
- @author:Kevin
- @create: 2023-08-01 09:56
@Description:
*/
@Data
@TableName("t_user_tag")
public class UserTagPO {@TableId(type = IdType.INPUT)
private Long userId;
@TableField(value = "tag_info_01")
private Long tagInfo01;
@TableField(value = "tag_info_02")
private Long tagInfo02;
@TableField(value = "tag_info_03")
private Long tagInfo03;
private Date createTime;
private Date updateTime;
}
```