mybatis学习(51):扩展集
生活随笔
收集整理的這篇文章主要介紹了
mybatis学习(51):扩展集
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
大部分現(xiàn)實(shí)應(yīng)用中我們并不需要把數(shù)據(jù)庫(kù)中的整行數(shù)據(jù)都拉出來(lái), 所以要做擴(kuò)展結(jié)果集
筆記: 在實(shí)際項(xiàng)目過(guò)程中, 先用逆向工程自動(dòng)生成, 然后再做擴(kuò)展結(jié)果集來(lái)滿足大部分的現(xiàn)實(shí)需要
?
本節(jié)內(nèi)容應(yīng)該注意擴(kuò)展結(jié)果集的命名方式
創(chuàng)建一個(gè)擴(kuò)展類(lèi)ShopCustom, (這樣命名可以和Shop在項(xiàng)目樹(shù)里面排列在一起)
| package io.github.coinsjack.pojo;public class ShopCustom extends Shop {private String shopName;private String shopDesc;public ShopCustom() {} |
創(chuàng)建一個(gè)mapper接口 ShopMapperCustom?
| package io.github.coinsjack.dao;import io.github.coinsjack.pojo.ShopCustom;public interface ShopMapperCustom {ShopCustom getShopById(Integer id);} |
創(chuàng)建對(duì)應(yīng)的映射文件
| <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//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_shopwhere `shop_id` = #{id}</select> </mapper> |
測(cè)試
| @Test public void testGetShopByIdCustom() {SqlSession session = MyBatisUtil.getSqlSession();ShopMapperCustom mapper = session.getMapper(ShopMapperCustom.class);System.out.println(mapper.getShopById(29));session.close(); } |
結(jié)果
| 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='過(guò)來(lái)喝喝就知道啦,你是我的奶茶'} |
?
Java知識(shí)點(diǎn)
如果父類(lèi)實(shí)現(xiàn)了某接口,子類(lèi)也會(huì)繼承接口的實(shí)現(xiàn)
總結(jié)
以上是生活随笔為你收集整理的mybatis学习(51):扩展集的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java线程中关于Synchronize
- 下一篇: python酒店评论分析_酒店评论的情感