适配多租户

This commit is contained in:
zhonghua1
2025-12-20 17:11:46 +08:00
parent db64ed63a5
commit 1c5a6eee8b
4 changed files with 18 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rj.entity.sys.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
* <p>
@@ -18,7 +19,7 @@ import org.apache.ibatis.annotations.Select;
* - 其他通过 MyBatis-Plus 内置 CRUD 产生的 SQL 仍然会自动带上 tenant_id 条件。
* </p>
*
* @author 系统生成
* @author
* @since 2025-08-07
*/
public interface UserMapper extends BaseMapper<User> {
@@ -36,5 +37,13 @@ public interface UserMapper extends BaseMapper<User> {
@InterceptorIgnore(tenantLine = "true")
@Select("SELECT * FROM user WHERE token = #{token} LIMIT 1")
User selectByTokenIgnoreTenant(@Param("token") String token);
/**
* 根据用户ID更新用户信息登录时更新token用不走多租户拦截器。
* 此方法用于登录场景需要跨租户更新用户token因此不添加 tenant_id 条件。
*/
@InterceptorIgnore(tenantLine = "true")
@Update("UPDATE user SET token = #{user.token} WHERE user_id = #{user.userId}")
int updateByIdIgnoreTenant(@Param("user") User user);
}

View File

@@ -42,9 +42,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
// 生成token
String token = UUID.randomUUID().toString().replace("-", "");
// 更新用户的token
// 更新用户的token(登录时不走多租户拦截器,避免添加 tenant_id = null 的限制)
user.setToken(token);
this.updateById(user);
this.baseMapper.updateByIdIgnoreTenant(user);
// 构建登录响应
LoginResponse loginResponse = new LoginResponse();

View File

@@ -17,5 +17,10 @@
user_id, user_name, email, password, token, avatar
</sql>
<!-- 根据 token 查询用户token 验证、获取当前用户信息),不走多租户拦截器,不添加 tenant_id 条件 -->
<select id="selectByTokenIgnoreTenant" resultMap="BaseResultMap">
SELECT * FROM user WHERE token = #{token} LIMIT 1
</select>
</mapper>

View File

@@ -233,6 +233,7 @@ UPDATE `sales_management` SET `tenant_id` = 'DEFAULT_TENANT_ID' WHERE
-- 门店与项目类
UPDATE `dealership` SET `tenant_id` = 'DEFAULT_TENANT_ID' WHERE `tenant_id` IS NULL;
UPDATE `project_management` SET `tenant_id` = 'DEFAULT_TENANT_ID' WHERE `tenant_id` IS NULL;
UPDATE `device_management` SET `tenant_id` = 'DEFAULT_cst_20251216' WHERE `tenant_id` IS NULL;
-- 日志与其他
UPDATE `yhy_audio_upload_log` SET `tenant_id` = 'DEFAULT_TENANT_ID' WHERE `tenant_id` IS NULL;