zoukankan      html  css  js  c++  java
  • MyBatis 返回值 摘自 https://blog.csdn.net/kangsenkangsen/article/details/51236279

    关于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>


    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));
    }
    }
    }

    再附上链接啊

    https://blog.csdn.net/u010003835/article/details/78813155

  • 相关阅读:
    vue2.0对于数组变化不及时刷新视图的问题
    [Node]报错:gyp verb check python checking for Python executable "python2" in the PATH
    spring的@ControllerAdvice注解
    antdVue--Upload使用
    antd Vue--this.$confirm弹窗使用
    播放视频判断是否暂停_Javascript判断Video视频播放、暂停、结束完成及获取长度事件监听处理...
    P5400 [CTS2019]随机立方体
    P7502 「HMOI R1」不知道是啥的垃圾题
    vue3插槽使用
    cocos creator破解
  • 原文地址:https://www.cnblogs.com/lixiaowei395659729/p/9917130.html
Copyright © 2011-2022 走看看