分页方法
@Operation(summary = "列表")
@GetMapping("/list")
public R<?> getSubscribeInfo(CmsExdProjectGs entity, Page<CmsExdProjectGs> page) {
Map<String, Object> map = BeanUtil.beanToMap(entity, true, true);
QueryWrapper<CmsExdProjectGs> queryWrapper = new QueryWrapper<>();
// 模糊查询字段
if (map.containsKey("pname")) {
queryWrapper.like("pname", entity.getPname());
map.remove("pname");
}
queryWrapper.allEq(map);
Page<CmsExdProjectGs> page1 = projectGsService.page(page, queryWrapper);
return R.ok(page1);
}
一对一、一对多关联查询
一对一
@Results({
//将用户id传给findById(),映射到user属性中
@Result(property = "submitRecord",//要封装的属性名称
column = "r1id", //根据哪个字段来查表的信息
one = @One(select = "com.ruoyi.talent.mapper.OutTalentSubmitRecordMapper.selectById")),
@Result(property = "recommenderRecord",//要封装的属性名称
column = "r1recommender_id", //根据哪个字段来查表的信息
one = @One(select = "com.ruoyi.talent.mapper.OutTalentSubmitterManagementMapper.selectById"))
})
一对多同理
…
MybatisPlus分页、自动填充插件等配置
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Configuration
@Slf4j
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
return interceptor;
}
@Bean
public MetaObjectHandler metaObjectHandler() {
return new MetaObjectHandler() {
@Override
public void insertFill(MetaObject metaObject) {
log.info("开始插入填充...");
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("开始更新填充...");
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
}
};
}
}
MybatisPlus分页、自动填充插件等配置带创建更新人
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Date;
@Configuration
@Slf4j
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
return interceptor;
}
@Bean
public MetaObjectHandler metaObjectHandler() {
return new MetaObjectHandler() {
@Override
public void insertFill(MetaObject metaObject) {
LoginUser loginUser = getLoginUser();
log.info("公共字段自动填充[insert]入参: {},当前登录用户:{}", metaObject,loginUser);
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
if (loginUser!=null){
this.strictInsertFill(metaObject, "createBy", String.class, loginUser.getUserId().toString());
}
}
@Override
public void updateFill(MetaObject metaObject) {
LoginUser loginUser = getLoginUser();
log.info("公共字段自动填充[update]入参: {},当前登录用户:{}", metaObject,loginUser);
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
if (loginUser!=null){
this.strictInsertFill(metaObject, "updateBy", String.class, loginUser.getUserId().toString());
}
}
};
}
/**
* 获取当前登录用户(可选)
*/
private LoginUser getLoginUser() {
LoginUser loginUser;
try {
loginUser = SecurityUtils.getLoginUser();
} catch (Exception e) {
log.warn("自动注入警告 => 用户未登录");
return null;
}
return loginUser;
}
}