天天看點

mybatis-plus聯合查詢使用,注解@TableName(resultMap = )這麼用

作者:摸魚女程式員那些事兒

很多程式員在實體類中看到mybatis-plus的注解(@Data @TableName(resultMap = "c_r") //不配合 typeHandler 或 numericScale 使用無意義)不知道是什麼意思,不會使用了,本篇文章,就告訴大家這些注解使用的方法。

mybatis-plus聯合查詢使用,注解@TableName(resultMap = )這麼用

在使用這個注解時,主要是實體類的配置和.xml檔案的配置相合。如如果指定了@TableName(resultMap = "c_r")這裡的resultMap="c_r"如果配置了它,第一需要在實體類的字段中含有typeHandler的使用。

如實體類:

@Data
@TableName(resultMap = "c_r") //不配合 typeHandler 或 numericScale 使用無意義,示範而已
public class Child {

    private Long id;


    private String name;

    private Long laoHanId;

    private Long laoMaId;

    @TableField(exist = false,typeHandler = JacksonTypeHandler.class)
    private Man laoHan;

    @TableField(exist = false)
    private Woman laoMa;
}           

上面實體類配置了@TableName(resultMap = "c_r"),并且字段private Man laoHan;指定了@TableField(exist = false,typeHandler = JacksonTypeHandler.class),這樣resultMap = "c_r"才有效果。

第二,resultMap = "c_r"指的是xml檔案的ID值,那麼這個xml檔案中,首先要有這個結果值,再次就得有對應着的關于@TableField(exist = false,typeHandler = JacksonTypeHandler.class)的配置如示例:

XML檔案配置

<mapper namespace="cn.test.mapper.ChildMapper">

<!--    </resultMap>-->
    <resultMap id="c_r" type="cn.test.entity.Child">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="laoHanId" column="lao_han_id"/>
        <result property="laoMaId" column="lao_ma_id"/>

        <!--laoHan和實體類裡面的laoHan對應,lao_han_id傳遞的參數,
          就是child的對應的這一列的參數-->
        <association property="laoHan" column="lao_han_id"
                     select="cn.test.mapper.ManMapper.selectById"
typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <!--laoHan和實體類裡面的laoMa對應,lao_ma_id傳遞的參數,
                       就是child的對應的這一列的參數-->
        <association property="laoMa" column="lao_ma_id"
                     select="cn.test.mapper.WomanMapper.selectById"/>


    </resultMap>

    <select id="selectLinkById" resultMap="c_r">
        select *
        from child
        where id = #{id}
    </select>
</mapper>           

綜合上面兩個聯合起來配置實體類上面的@TableName(resultMap = "c_r")才有真正的意義。

下面是全部代碼包括了多表查詢的配置:

mapper:

public interface ChildMapper extends BaseMapper<Child> {


//    @Select("select * from child where id = #{id}")
    Child selectLinkById(Long id);

    @Select("select * from child where lao_han_id = #{id}")
    List<Child> selectByLaoHanId(Long id);

    @Select("select * from child where lao_ma_id = #{id}")
    List<Child> selectByLaoMaId(Long id);


    @Select({
            "<script>" +
                    "select * from child " +
                    "where 1=1" +
                    "<if test=\"id != null\">" +
                    "and id = #{id} " +
                    "</if>" +
                    "</script>"})

    Child selectOneChile(Long id);

    @Select("select * from child where id = #{id}")
    Child getChildOne(Long id);
}           

實體類:

package cn.test.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data;

/**
 * @author miemie
 * @since 2019-11-27
 */
@Data
@TableName(resultMap = "c_r") //不配合 typeHandler 或 numericScale 使用無意義,示範而已
public class Child {

    private Long id;


    private String name;

    private Long laoHanId;

    private Long laoMaId;

    @TableField(exist = false,typeHandler = JacksonTypeHandler.class)
    private Man laoHan;

    @TableField(exist = false)
    private Woman laoMa;
}
           

xml配置檔案:

<mapper namespace="cn.test.mapper.ChildMapper">
<!--    </resultMap>-->
    <resultMap id="c_r" type="cn.test.entity.Child">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="laoHanId" column="lao_han_id"/>
        <result property="laoMaId" column="lao_ma_id"/>

        <!--laoHan和實體類裡面的laoHan對應,lao_han_id傳遞的參數,
          就是child的對應的這一列的參數-->
        <association property="laoHan" column="lao_han_id"
                     select="cn.test.mapper.ManMapper.selectById"  
typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <!--laoHan和實體類裡面的laoMa對應,lao_ma_id傳遞的參數,就是child的對應的這一列的參數-->
        <association property="laoMa" column="lao_ma_id"
                     select="cn.test.mapper.WomanMapper.selectById"/>
    </resultMap>

    <select id="selectLinkById" resultMap="c_r">
        select *
        from child
        where id = #{id}
    </select>
</mapper>           

服務層:

public interface ChildService {

List<Child> selectByLaoHanId(Long id);
Child selectLinkById();
Child getChildOne();
Child selectOneChile();


}           
@Service
@Transactional
public class ChildServiceImp extends ServiceImpl‘
<ChildMapper, Child>  implements ChildService {
    @Override
    public List<Child> selectByLaoHanId(Long id) {

        return baseMapper.selectByLaoHanId(id);
    }

    @Override
    public Child selectLinkById() {
        return baseMapper.selectLinkById(1l);
    }

    @Override
    public Child getChildOne() {
        return baseMapper.getChildOne(1l);
    }

    @Override
    public Child selectOneChile() {
        return baseMapper.selectOneChile(6L);
    }
}           

控制層:

@RestController
@RequestMapping("child")
public class ChildController {

    @Autowired
    private ChildService childService;

    @GetMapping("/selectByLaoHanId")
    public List<Child> selectByLaoHanId(){

       return childService.selectByLaoHanId(1l);
    }

    @GetMapping("/selectLinkById")
    public Child selectLinkById(){

        return childService.selectLinkById();
    }

    @GetMapping("/getChildOne")
    public Child getChildOne(){

        return childService.getChildOne();
    }

    @GetMapping("/selectOneChile")
    public Child selectOneChile(){

        return childService.selectOneChile();
    }
}
           

啟動類:

@MapperScan("cn.test.mapper")
@SpringBootApplication
public class MyApplicationStart {

    public static void main(String[] args){
        SpringApplication.run(MyApplicationStart.class,args);
    }

}           

如果有需要源代碼的朋友,關注我,發私信。

繼續閱讀