关于mybatis返回单一对象或对象列表的问题

关于mybatis返回单一对象或对象列表的问题

一.说明

  • 返回数据类型由dao中的接口和map.xml文件共同决定。另外,不论是返回单一对象还是对象列表,***map.xml中的配置都是一样的,都是resultMap=”***Map”或resultType=“* .* .*”类型.

  • 每一次mybatis从数据库中select数据之后,都会检查数据条数和dao中定义的返回值是否匹配。

  • 若返回一条数据,dao中定义的返回值是一个对象或对象的List列表,则可以正常匹配,将查询的数据按照dao中定义的返回值存放。

  • 若返回多条数据,dao中定义的返回值是一个对象,则无法将多条数据映射为一个对象,此时mybatis报错。

二.代码测试

UserMap.xml映射文件:

<resultMap id="BaseResultMap" type="com.ks.ssm.domain.User" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="qq" property="qq" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="gender" property="gender" jdbcType="BIT" />
    <result column="birthday" property="birthday" jdbcType="DATE" />
    <result column="city" property="city" jdbcType="VARCHAR" />
    <result column="mood" property="mood" jdbcType="VARCHAR" />
    <result column="single" property="single" jdbcType="BIT" />
    <result column="enrolltime" property="enrolltime" jdbcType="TIMESTAMP" />
    <result column="level" property="level" jdbcType="TINYINT" />
    <result column="status" property="status" jdbcType="BIT" />
    <result column="titlepic" property="titlepic" jdbcType="VARCHAR" />
    <result column="job" property="job" jdbcType="VARCHAR" />
    <result column="logintime" property="logintime" jdbcType="TIMESTAMP" />
    <result column="loginip" property="loginip" jdbcType="VARCHAR" />
    <result column="token" property="token" jdbcType="VARCHAR" />
    <result column="modifytime" property="modifytime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, username, password, email, qq, phone, gender, birthday, city, mood, single, enrolltime, 
    level, status, titlepic, job, logintime, loginip, token, modifytime
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from user_info
    where id = #{id,jdbcType=BIGINT}
  </select>
  <!-- add by ks -->
    <select id="selectByUserName" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user_info
    where username = #{username,jdbcType=VARCHAR}
   </select>
   <!-- mybatis 非常的智能,返回值统一使用 resultMap="BaseResultMap",mybatis会根据查询到的条目数量自动进行判断,如果是一条就返回对象,如果是多条就返回List对象列表-->
  <select id="selectByEmail" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user_info
    where email = #{email,jdbcType=VARCHAR}
   </select>
   <!-- end by ks -->

dao文件UserMap.java:

public interface UserMapper {

    User selectByPrimaryKey(Long id);

    User selectByUserName(String username );

    /**关于mybatis返回单一对象或对象列表的问题:
     * 1.返回数据类型由dao中的接口和*map.xml文件共同决定。另外,不论是返回单一对象还是对象列表,*map.xml中的配置都是一样的,都是resultMap="*Map"*或resultType=“* .* .*”类型.
     * 2.每一次mybatis从数据库中select数据之后,都会检查数据条数和dao中定义的返回值是否匹配。
     * 3.若返回一条数据,dao中定义的返回值是一个对象或对象的List列表,则可以正常匹配,将查询的数据按照dao中定义的返回值存放。
     * 4.若返回多条数据,dao中定义的返回值是一个对象,则无法将多条数据映射为一个对象,此时mybatis报错。
     * */
    List<User> selectByEmail(String email );
}

测试代码和结果文件:

@RunWith(SpringJUnit4ClassRunner.class)     //表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})

public class TestMyBatis {
  private static Logger logger = Logger.getLogger(TestMyBatis.class);
  @Resource
  private UserMapper userDao;
  @Test
  public void testMybatis() {
    User user = userDao.selectByUserName("ks");
    logger.info("user.........................");
    logger.info(JSON.toJSONString(user));
    List<User> users=userDao.selectByEmail("ks");
    logger.info("list.........................");
    for(User userTemp : users)
    {
        logger.info(JSON.toJSONString(userTemp));
    }
  }
}

测试结果

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值