mybatis中只查询部分字段的处理方式

mybatis中如果返回对象集合的话,会把对象中的所有字段都返回,如果表中字段很多而我只需要部分字段,有几种解决方案

1、重新定义类,里面存放要返回的字段属性

2、将结果定义为List<Map<String, Object>>类型,如下:

BookMapper.xml 文件中定义如下:

<!-- Book全部字段 -->
<resultMap id="BaseResultMap" type="com.lgsc.cjbd.book.model.Book">
    <id column="book_id" property="bookId" jdbcType="BIGINT" />
    <result column="book_name" property="bookName" jdbcType="VARCHAR" />
    <result column="press" property="press" jdbcType="VARCHAR" />
    <result column="author" property="author" jdbcType="VARCHAR" />
    <result column="translator" property="translator" jdbcType="VARCHAR" />
    <result column="isbn" property="isbn" jdbcType="CHAR" />
</resultMap>

<!-- 定义resultMap,type为HashMap -->
<resultMap id="PartBookMap" type="java.util.HashMap">
    <id column="book_id" property="bookId" jdbcType="BIGINT" />
    <result column="book_name" property="bookName" jdbcType="VARCHAR" />
    <result column="author" property="author" jdbcType="VARCHAR" />
</resultMap>

<!-- 查询语句 -->
<select id="selectPartBook" resultMap="PartBookMap">
    select book_id, book_name, author from book
</select>

BookMapper.java 文件中定义如下:

List<Map<String, Object>> selectPartBook();
  •  

BookService.java 用 List< Map< String, Object > > 来接收

List<Map<String, Object>> map = bookMapper.selectPartBook();

本文来自 siwuai 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u014710520/article/details/73569257?utm_source=copy

  • 10
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Mybatis-plus可以使用select方法来查询部分字段,具体操作如下: 1. 在实体类定义需要查询字段,使用@TableField注解标注字段名。 2. 在mapper接口定义查询方法,使用@Select注解标注SQL语句,使用@Result注解标注返回结果。 3. 在service层调用mapper接口查询方法,获取部分字段查询结果。 示例代码如下: 实体类: ``` @Data public class User { @TableId(type = IdType.AUTO) private Long id; @TableField("user_name") private String userName; @TableField("password") private String password; @TableField("age") private Integer age; } ``` Mapper接口: ``` public interface UserMapper extends BaseMapper<User> { @Select("select id, user_name from user where id = #{id}") @Results(id = "userMap", value = { @Result(column = "id", property = "id"), @Result(column = "user_name", property = "userName") }) User selectPartById(@Param("id") Long id); } ``` Service层: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectPartById(Long id) { return userMapper.selectPartById(id); } } ``` 以上代码,User实体类定义了需要查询字段,UserMapper接口定义了查询方法,使用@Select注解标注SQL语句,使用@Result注解标注返回结果。在ServiceImpl调用mapper接口查询方法,获取部分字段查询结果。 ### 回答2: Mybatis-plus是在Mybatis的基础上进行封装的一个持久层框架,它简化了部分sql操作,提高了开发效率。在使用Mybatis-plus进行查询时,我们有时只需要查询部分字段而非全部字段,那么该如何处理呢? 一种简单的方法是使用select方法,该方法可以指定返回查询结果的字段。例如,我们需要查询user表的id和name字段,可以这样编写代码: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.select("id", "name"); List<User> userList = userMapper.selectList(wrapper); ``` 另一种方法是使用注解@TableField,该注解可以指定实体类哪些字段需要进行数据查询。 例如,我们有一个User实体类,其有id、name和age三个字段,现在我们需要查询id和name字段,可以给User类的age字段使用@TableField注解进行标记,表示不进行查询: ``` @TableField(exist = false) private Integer age; ``` 然后再使用select方法进行查询,例如: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.select(User.class, info -> !info.getColumn().equals("age")); List<User> userList = userMapper.selectList(wrapper); ``` 这里使用了Lambda表达式,表示只查询User类不包含age字段的列。 以上就是使用Mybatis-plus查询部分字段的方法。具体应该选用哪种方法,需要根据具体的应用场景来选择。如果需要查询字段较多,可以使用第一种方法;如果需要查询字段比较特别,可以使用第二种方法。总之,Mybatis-plus提供了很多灵活性,让我们可以根据具体情况进行选择和处理。 ### 回答3: MyBatis-Plus是MyBatis的一个增强工具包,它可以使MyBatis的使用更加简单便捷。在MyBatis-Plus查询部分字段有多种方式,下面我将分别介绍。 1. 使用Wrapper 在MyBatis-Plus,我们可以通过Wrapper来查询指定的字段,Wrapper可以通过QueryWrapper或UpdateWrapper创建。例如,我们使用QueryWrapper查询user表的id和name字段,代码如下: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.select("id", "name"); List<User> userList = userDao.selectList(wrapper); ``` 上述代码,我们创建了一个QueryWrapper,通过select方法指定查询字段。在查询时,我们传入这个Wrapper参数,MyBatis-Plus会根据我们指定的字段进行查询。 2. 使用注解@SqlSelect MyBatis-Plus还提供了注解@SqlSelect,它可以在实体类上使用,用于指定查询字段和 SQL 语句。例如,我们在User实体类上使用@SqlSelect注解查询用户id和name字段,代码如下: ``` @SqlSelect("SELECT id, name FROM user") public class User { private Long id; private String name; // getter and setter } ``` 上述代码,我们在User类上使用@SqlSelect注解指定了查询字段和 SQL 语句。在查询时,我们只需调用UserDao的selectList方法,MyBatis-Plus会根据注解指定的 SQL 语句查询指定的字段。 3. 使用注解@TableField 在MyBatis-Plus,我们还可以在实体类的字段上使用注解@TableField,用于指定数据库表字段名。例如,我们在User实体类的id和name字段上使用注解@TableField指定数据库表字段名,代码如下: ``` public class User { @TableId private Long id; @TableField("user_name") private String name; // getter and setter } ``` 上述代码,我们在id字段上使用@TableId注解指定其为主键,指定数据库表字段名为id;在name字段上使用@TableField注解指定数据库表字段名为user_name。在查询时,我们只需调用UserDao的selectList方法即可查询指定的字段。 以上是使用MyBatis-Plus查询部分字段的几种方式,根据实际情况选择合适的方式即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值