13-Mybatis封装输出结果之二 -- resultMap

上一篇:12-Mybatis封装输出结果之一 – resultType (含typeAliases)https://blog.csdn.net/fsjwin/article/details/109673170

处理使用resultType之外,也可以使用resultMap,即当我们java类的属性名和数据库的名字不一致的时候可以一用。

1.resultMap解决属性值和表列名不一致的情况

1.1 StudentDao

    public MyStudent selectStudentResultMap(Integer id);

1.2 StudentDao.xml

   <resultMap id="myStudent" type="com.yuhl.domain.MyStudent">
        <id property="myid" column="id"/>
        <result property="myname" column="name"/>
        <result property="myage" column="age"/>
        <result property="myemail" column="email"/>

    </resultMap>
    <!-- ResultMap 表中类名和类中属性名不一致的时候-->
    <select id="selectStudentResultMap"  resultMap="myStudent">
        select id,name,email,age from student where id = #{id}
    </select>

1.3 测试

  @Test
    public void test14() {
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
        //可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
        MyStudent myStudent = studentDao.selectStudentResultMap(1001);
        System.out.println(myStudent);
    }

1.4 测试结果

Checking to see if class com.yuhl.vo.StudentVo matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 188576144.
==>  Preparing: select id,name,email,age from student where id = ?
==> Parameters: 1001(Integer)
<==    Columns: id, name, email, age
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
MyStudent{myid=1001, myname='张三', myemail='zhangsan@qq.com', myage=20}

2.属性值和表列名不一致第二种解决方案as

2.1 StudentDao

 public MyStudent selectStudentResultMap2(Integer id);

2.2 StudentDao.xml

  <resultMap id="myStudent" type="com.yuhl.domain.MyStudent">
        <id property="myid" column="id"/>
        <result property="myname" column="name"/>
        <result property="myage" column="age"/>
        <result property="myemail" column="email"/>
    <select id="selectStudentResultMap2"  resultType="com.yuhl.domain.MyStudent">
        select id as myid,name as myname,email as myemail,age as myage from student where id = #{id}
    </select>

2.3 测试

 @Test
    public void test15() {
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
        //可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
        MyStudent myStudent = studentDao.selectStudentResultMap2(1001);
        System.out.println(myStudent);
    }

2.4 测试结果

Checking to see if class com.yuhl.vo.StudentVo matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 188576144.
==>  Preparing: select id as myid,name as myname,email as myemail,age as myage from student where id = ?
==> Parameters: 1001(Integer)
<==    Columns: myid, myname, myemail, myage
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
MyStudent{myid=1001, myname='张三', myemail='zhangsan@qq.com', myage=20}

3.总结

resultMap和as可以解决java类属性名称和表类名不一致的情况。做到解耦!
脑图:
https://www.processon.com/view/link/5fae2e01f346fb2d03b384d3

下一篇:14-Mybatis中like使用的两种情况https://blog.csdn.net/fsjwin/article/details/109674722

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值