【mybatisplus】查询指定字段
使用mybatisplus查询指定字段。
·
使用mybatisplus查询指定字段
实体类
package com.test.entity;
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 com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.util.JSONPObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@TableName("student")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
@TableId(type = IdType.AUTO)
private int id;
private String name;
/*@TableField(typeHandler = FastjsonTypeHandler.class)
private JSONPObject param;*/
}
查询名称
package com.test.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.entity.Student;
import com.test.mapper.StudentMapper;
import com.test.service.StudentService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
@Resource
StudentMapper studentMapper;
@Override
public ResponseEntity findNameById(Integer id) {
QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>().select("name");
queryWrapper.eq("id",id);
List<Object> objects = studentMapper.selectObjs(queryWrapper);
return ResponseEntity.ok(objects);
}
}
mapper层
package com.test.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface StudentMapper extends BaseMapper<Student> {
}
参考博客:
mybatisplus查询某一字段
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)