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查询某一字段

你可能感兴趣的:(java,mybatisplus)