Mybatis学习第22节 -- 高级结果映射 构造方法映射

知识储备:  对于大部分编程语言的函数来说, 函数的signature是函数名和函数参数,而对于函数参数的名称, 编译器不关心.

为ShopCustom创建一个构造函数

public ShopCustom(Integer id, String shopName, String shopDesc) {    this.id = id;    this.shopName = shopName;    this.shopDesc = shopDesc;}

为ShopMapperCustom添加一个新方法

public interface ShopMapperCustom {

    ShopCustom getShopById(Integer id);    ShopCustom  getShopByIdConstructor(Integer id);}

在映射文件中编写相应的配置

对于constructor的参数来说, 关心的只是要把什么内容按序填入就好了, 通过javaType来与构造函数的参数列表产生对应

<resultMap id="shopResultMapConstructor" type="ShopCustom">    <constructor>        <idArg column="shop_id" javaType="int"/>        <arg column="shop_name" javaType="string"/>        <arg column="shop_desc" javaType="string"/>    </constructor></resultMap>
<select id="getShopByIdConstructor" parameterType="int" resultMap="shopResultMapConstructor" >    select `shop_id`, `shop_name`, `shop_desc`    from tb_shop    where `shop_id` = #{id}</select>

测试

@Testpublic void testGetShopByIdCustomConstructor() {    SqlSession session = MyBatisUtil.getSqlSession();    ShopMapperCustom mapper = session.getMapper(ShopMapperCustom.class);    System.out.println(mapper.getShopByIdConstructor(29));    session.close();}

结果

2018-12-29 11:53:04,097 [main] DEBUG [io.github.coinsjack.dao.ShopMapperCustom] - Cache Hit Ratio [io.github.coinsjack.dao.ShopMapperCustom]: 0.0
2018-12-29 11:53:04,484 [main] DEBUG [io.github.coinsjack.dao.ShopMapperCustom.getShopByIdConstructor] - ==> Preparing: select `shop_id`, `shop_name`, `shop_desc` from tb_shop where `shop_id` = ? 
2018-12-29 11:53:04,576 [main] DEBUG [io.github.coinsjack.dao.ShopMapperCustom.getShopByIdConstructor] - ==> Parameters: 29(Integer)
2018-12-29 11:53:04,649 [main] DEBUG [io.github.coinsjack.dao.ShopMapperCustom.getShopByIdConstructor] - <== Total: 1
ShopCustom{shopName=‘暴漫奶茶店‘, shopDesc=‘过来喝喝就知道啦,你是我的奶茶‘}

原文地址:https://www.cnblogs.com/litran/p/10546254.html

时间: 2024-08-30 14:19:40

Mybatis学习第22节 -- 高级结果映射 构造方法映射的相关文章

Mybatis学习第19节 -- 嵌套查询一对多的配置

一对多和多对多是一样的. 而多对多是指 A对于B来说是一对多的关系, 同时B对于A来说也是一对多的关系, 互为一对多,即为多对多. 比如说一个标签下面有多篇文章,一篇文章也可能有多个标签 Shop实体类设计 List<Product> productList; Product实体类设计 public class Product implements Serializable{ Integer id; String name; String desc; String imgAddr; Strin

MyBatis学习(四)XML配置文件之SQL映射的XML文件

SQL映射文件常用的元素: 1.select 查询语句是MyBatis最常用的语句之一. 执行简单查询的select元素是非常简单的: <select id="selectUser" parameterType="int" resultType="hashmap"> SELECT * FROM PERSON WHERE ID = #{id} </select> 这个语句被称作selectUser,接受一个int类型的参数,

Mybatis学习第8节 -- 动态sql-if

需求 查询id小于10,并且shopname包含"关键词"的记录 如果用户没有输入任何关键词, 那么列出所有结果 接口 List<Shop> get10ShopByTitle(String value); 映射 <select id="get10ShopByTitle" resultMap="simpleResultMap"> select * from tb_shop <where> <if test=

Mybatis学习第23节 -- 鉴别器 discriminator

discriminator或者叫做分类器 Vehicle类   package io.github.coinsjack.pojo; import java.util.Date; public class Vehicle { protected Integer id; protected String vin; protected Date year; protected String make; protected String model; protected String color; pu

Mybatis学习第5节 -- 插入并获取ID

插入过程 接口 int insertShop(Shop shop); 映射 <insert id="insertShop" parameterType="Shop" useGeneratedKeys="true" keyProperty="id"> INSERT INTO `oto`.`tb_shop` ( `owner_id`, `area_id`, `shop_category_id`, `shop_name`

Mybatis学习第2节 -- 模糊查询之#和$的区别

先说结论, #是占位符,而$的行为是字符串拼接, 在参数是java的基本类型且只有一个参数的情况下,使用$时,只能用value作为参数传递 需求决定设计, 先在interface里面添加相应方法 public interface ShopMapper { Shop getShopById(Integer id); Shop getShopByIdAlias(Integer id); Shop getShopByTitleContainDollar(String value); Shop getS

Mybatis学习第6节 -- 修改功能和修改部分字段

方案一, 先查询再修改 接口 int updateShop(Shop shop); 映射 <update id="updateShop" parameterType="Shop"> UPDATE `tb_shop` SET `owner_id` = 'owner_id', `area_id` = 'area_id', `shop_category_id` = 'shop_category_id', `shop_name` = 'shop_name', `

Mybatis学习第25节 -- 懒加载 积极与不积极

积极懒加载是指如果你访问一个对象的属性,Mybatis就会帮你把需要进步一查询的该属性或者其他属性在数据库中查询出来. 不积极懒加载是指,有这种必要的时候,采取进行必要的数据库检索 我看的教程中的Mybatis版本默认是积极的lazy加载, 而我实际用的Mybatis3.4.6实际上默认不积极的lazy加载. mybatis-config.xml文件 <settings> <!-- 打印查询语句 --> <setting name="logImpl" va

Mybatis学习第4节 -- 多参数传递

第一种方法使用索引 一般不使用,不记录 第二种方法使用注解 接口 List<Shop> getShopListByPageAno(@Param(value = "offset") int offset, @Param(value = "pagesize") int pagesize); mapper <select id="getShopListByPageAno" resultMap="simpleResultMap