MyBatis 通过Id查询部分字段进行返回,其余字段不显示

数据库表中的字段并不是我们需要全部获取的,获取所有字段中的部分字段

前言

如果使用 @JsonInclude(JsonInclude.Include.NON_NULL) 注解放到实体类上,这种方法就是 如果字段值为null的字段不序列化,但是有默认值的字段依然会被查出来,所以可以通过一下方式进行解决。


mapper.xml文件

实体类中所有的字段

<!-- Student全部字段 -->
    <resultMap id="BaseResultMap" type="com.domain.entity.Student">
        <id column="id" jdbcType="VARCHAR" property="id" />
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
        <result column="student_id" jdbcType="VARCHAR" property="studentId" />
        <result column="student_name" jdbcType="VARCHAR" property="studentName" />
    </resultMap>

实体类中的部分字段

<!-- Student中要查询的字段 -->
    <resultMap id="PartStudentMap" type="com.domain.entity.Student">
        <id column="id" jdbcType="VARCHAR" property="id" />
        <result column="student_id" jdbcType="VARCHAR" property="studentId" />
        <result column="student_name" jdbcType="VARCHAR" property="studentName" />
    </resultMap>

Mybatis查询语句

<!-- Student中要查询的字段 -->
<select id="selectStudentId" parameterType="com.domain.entity.Student" resultMap="PartStudentMap">
        select
        id,student_id,student_name
        from sutdent
        where student_id= #{studentId,jdbcType=VARCHAR}
</select>

mapper层

List<Map<String, Object>> selectStudentId(String studentId);

service层

public List<Map<String, Object>> selectStudentId(String studentId) {
        List<Map<String, Object>> list = studentMapper.selectStudentId(studentId);
        return list;
}

controller层

public Result<List<Map<String, Object>>> getStudent(String studentId){
    List<Map<String, Object>> list =  studentService.selectStudentId(studentId);
    return Result.success(list);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值