微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mybatis学习第21节 -- 扩展结果集

大部分现实应用中我们并不需要把数据库中的整行数据都拉出来, 所以要做扩展结果集 笔记: 在实际项目过程中, 先用逆向工程自动生成, 然后再做扩展结果集来满足大部分的现实需要   本节内容应该注意扩展结果集的命名方式

创建一个扩展类ShopCustom, (这样命名可以和Shop在项目树里面排列在一起)

package io.github.coinsjack.pojo;

public class ShopCustom extends Shop {

private String shopName;
private String shopDesc;

public ShopCustom() {
}

创建一个mapper接口 ShopMapperCustom 

package io.github.coinsjack.dao;

import io.github.coinsjack.pojo.ShopCustom;

public interface ShopMapperCustom {

ShopCustom getShopById(Integer id);

}

创建对应的映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="io.github.coinsjack.dao.ShopMapperCustom">

<cache/>
 
    <resultMap id="shopResultMap" type="ShopCustom">
<id column="shop_id" property="id" ></id>
<result column="shop_name" property="shopName"></result>
<result column="shop_desc" property="shopDesc"/>
</resultMap>

<select id="getShopById" parameterType="int" resultMap="shopResultMap" >
select `shop_id`, `shop_name`, `shop_desc`
from tb_shop
where `shop_id` = #{id}
</select>
</mapper>
测试
@Test
public void testGetShopByIdCustom() {
sqlSession session = MyBatisUtil.getsqlSession();
ShopMapperCustom mapper = session.getMapper(ShopMapperCustom.class);
System.out.println(mapper.getShopById(29));
session.close();
}

结果

2018-12-29 11:33:32,656 [main] DEBUG [io.github.coinsjack.dao.ShopMapperCustom] - Cache Hit Ratio [io.github.coinsjack.dao.ShopMapperCustom]: 0.0
2018-12-29 11:33:33,128 [main] DEBUG [io.github.coinsjack.dao.ShopMapperCustom.getShopById] - ==> Preparing: select `shop_id`, `shop_name`, `shop_desc` from tb_shop where `shop_id` = ? 
2018-12-29 11:33:33,237 [main] DEBUG [io.github.coinsjack.dao.ShopMapperCustom.getShopById] - ==> Parameters: 29(Integer)
2018-12-29 11:33:33,309 [main] DEBUG [io.github.coinsjack.dao.ShopMapperCustom.getShopById] - <== Total: 1
ShopCustom{shopName='暴漫奶茶店', shopDesc='过来喝喝就知道啦,你是我的奶茶'}


Java知识点 如果父类实现了某接口,子类也会继承接口的实现

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐