Mybatis学习笔记(6)-输出映射

输出映射

MyBatis的输出映射分为两种方式,resultType和resultMap

resultType

我们上面所有的例子里都是定义的resultType,resultType可以是简单类型,比如我们想获取用户信息的,还有就是输出POJO对象或者POJO列表。
不管是POJO对象还是POJO列表,我们在resultType中的定义都是一样的,只不过接口定义不一样:

单个对象

public User findUserById(int id) throws Exception;

Mybatis根据接口返回值判断返回一条对象,如果用过ibatis的可以知道内部调用了session.selectOne。

返回列表

public List<User> findUserByUsername(String username) throws Exception;

内部使用session.selectList,Mapper接口使用List< XXX >作为返回值。

查询出来的列名和POJO属性只要有一个一致就会创建POJO对象,顶多别的字段为默认值,但是如果全部不一致,就不会创建该POJO对象了。

resultMap

上面的resultType在查询的列名和POJO属性值一致的时候才可以映射成功,如果不一致的话,就需要resultMap登场表演了。
如果查询出来的列名和POJO的属性名不一致,通过定义一个resultMap对列名和POJO属性名之间作一个映射关系。
在使用resultMap前我们需要定义resultMap。
假设我们查询用户表的两个字段username和birthday,但是查询的时候定义为_username和_birthday。列名和属性名不一致了,先定义resultMap。

<resultMap id="userMap" type="java.util.HashMap">
        <id column="_username" property="username"></id>
        <result column="_birthday" property="birthday"></result>
    </resultMap>

其中

  • id标识这个resultMap
  • type标识映射哪个POJO里面的属性,但是这里不能用com.will.entiy.User,编译器会报类型转换错误,因为我们的查询没有查询该表的所有字段。
  • id元素标识这个对象的主键,result就是普通字段
  • property代表POJO的属性名称
  • column表示数据库SQL的列名

再看我们的select元素:

<select id="findUsernameAndBirthday" parameterType="int" resultMap="userMap">
        select username _username,birthday _birthday from user where id = #{VALUE}
    </select>

使用resultType进行输出映射时,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。 此外,resultMap还可以做一对多、多对多等高级映射,这些内容将在后续文章中介绍。

insert

insert相对于select简单很多,下面是往用户表插入一个用户的实例:

    <!-- 添加用户 -->
    <insert id="insertUser" parameterType="com.will.entiy.User" useGeneratedKeys="true" keyProperty="id">
        insert into user(username,birthday,sex,address)
        values(#{username},#{birthday},#{sex},#{address})
    </insert>

接口为:

//添加用户信息
    public void insertUser(User user)throws Exception;

这里我们注意到了insert语句里多了两个属性useGeneratedKeyskeyProperty,这里涉及到的是主键回填的概念。

  • useGeneratedKeys:告诉Mybatis是否使用数据库内置策略生成主键
  • keyProperty:告诉Mybatis哪一列是主键

这样我们无须传入id值,Mybatis就可以帮我们设置主键,同时还会回填POJO内的id值,当我们像下面这样调用时:

@Test
    public void insertUserTest() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setUsername("Amy");
        user.setSex("femal");
        user.setAddress("北京西路");
        user.setBirthday(new Date(2016-06-05));
        userMapper.insertUser(user);
        sqlSession.commit();
    }

测试该代码,可以看到Mybatis已经为该行返回了一个id 即行号,默认新增主键的方式为+1,但是这个需要在数据库中给主键增加auto increment的方式。

如果我们想要定义自己的主键生成方式,可以使用selectKey进行自定义:

<!-- 添加用户 -->
    <insert id="insertUser" parameterType="com.will.entiy.User">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
             select if (max(id) is null, 1, max(id) + 2) as id from user
        </selectKey>
        insert into user(username,birthday,sex,address)
        values(#{username},#{birthday},#{sex},#{address})
    </insert>

这里我们定义主键值是最大id加2,如果还没有记录,则初始化为1。

update和delete

<!-- 删除 用户
        根据id删除用户,需要输入 id值
         -->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>

    <!-- 根据id更新用户
    分析:
    需要传入用户的id
    需要传入用户的更新信息
    parameterType指定user对象,包括 id和更新信息,注意:id必须存在
    #{id}:从输入 user对象中获取id属性值
     -->
    <update id="updateUser" parameterType="com.will.entiy.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address}
        where id=#{id}
    </update>

这一节主要介绍了主要的几个语句的使用,着重介绍了select语句,同时结合select语句说明了Mybatis中输入映射和输出映射的内容,其它一些高级的映射内容,留到后面的文章介绍。

转载自卡巴拉的树

猜你喜欢

转载自blog.csdn.net/lin74love/article/details/80974892
今日推荐