使用Mybatis,resultMap完成<collection> 一对多 <association> 一对一

需要使用一个插件EasyCode,只针对于mybatis
@RestController
@RequestMapping("apArticle")
public class ApArticleController {
   //补充一个接口,需求:通过文章id查询文章对象,包含了两个属性
    //只用了mybatis框架,没有使用mybatis-plus
    //使用mybatis resultmap 完成 <collection> 一对多 <association> 一对一
    @PostMapping("/selects/{id}")
    public ApArticle select(@PathVariable Long id){
        System.out.println(apArticleService.queryByIds(id));
        return apArticleService.queryByIds(id);
    }
}
====================================================
public interface ApArticleDao {
    ApArticle queryByIds(@Param("id") Long id);
}

@Service("apArticleConfigService")
public class ApArticleConfigServiceImpl implements ApArticleConfigService {
  @Override
    public ApArticleConfig update(ApArticleConfig apArticleConfig) {
        this.apArticleConfigDao.update(apArticleConfig);
        return this.queryById(apArticleConfig.getId());
    }
    }
 ===========================================
public interface ApArticleService {
    ApArticle queryByIds(Long 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="com.brianxia.articledemo.dao.ApArticleDao">

    <resultMap type="com.brianxia.articledemo.entity.ApArticle" id="ApArticleMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="title" column="title" jdbcType="VARCHAR"/>
        <result property="authorId" column="author_id" jdbcType="INTEGER"/>
        <result property="authorName" column="author_name" jdbcType="VARCHAR"/>
        <result property="channelId" column="channel_id" jdbcType="INTEGER"/>
        <result property="channelName" column="channel_name" jdbcType="VARCHAR"/>
        <result property="layout" column="layout" jdbcType="INTEGER"/>
        <result property="flag" column="flag" jdbcType="INTEGER"/>
        <result property="images" column="images" jdbcType="VARCHAR"/>
        <result property="labels" column="labels" jdbcType="VARCHAR"/>
        <result property="likes" column="likes" jdbcType="INTEGER"/>
        <result property="collection" column="collection" jdbcType="INTEGER"/>
        <result property="comment" column="comment" jdbcType="INTEGER"/>
        <result property="views" column="views" jdbcType="INTEGER"/>
        <result property="provinceId" column="province_id" jdbcType="INTEGER"/>
        <result property="cityId" column="city_id" jdbcType="INTEGER"/>
        <result property="countyId" column="county_id" jdbcType="INTEGER"/>
        <result property="createdTime" column="created_time" jdbcType="TIMESTAMP"/>
        <result property="publishTime" column="publish_time" jdbcType="TIMESTAMP"/>
        <result property="syncStatus" column="sync_status" jdbcType="INTEGER"/>
        <result property="origin" column="origin" jdbcType="INTEGER"/>
        <result property="staticUrl" column="static_url" jdbcType="VARCHAR"/>
    </resultMap>


    <resultMap type="com.brianxia.articledemo.entity.ApArticle" id="ApArticleAndConfigMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="title" column="title" jdbcType="VARCHAR"/>
        <result property="authorId" column="author_id" jdbcType="INTEGER"/>
        <result property="authorName" column="author_name" jdbcType="VARCHAR"/>
        <result property="channelId" column="channel_id" jdbcType="INTEGER"/>
        <result property="channelName" column="channel_name" jdbcType="VARCHAR"/>
        <result property="layout" column="layout" jdbcType="INTEGER"/>
        <result property="flag" column="flag" jdbcType="INTEGER"/>
        <result property="images" column="images" jdbcType="VARCHAR"/>
        <result property="labels" column="labels" jdbcType="VARCHAR"/>
        <result property="likes" column="likes" jdbcType="INTEGER"/>
        <result property="collection" column="collection" jdbcType="INTEGER"/>
        <result property="comment" column="comment" jdbcType="INTEGER"/>
        <result property="views" column="views" jdbcType="INTEGER"/>
        <result property="provinceId" column="province_id" jdbcType="INTEGER"/>
        <result property="cityId" column="city_id" jdbcType="INTEGER"/>
        <result property="countyId" column="county_id" jdbcType="INTEGER"/>
        <result property="createdTime" column="created_time" jdbcType="TIMESTAMP"/>
        <result property="publishTime" column="publish_time" jdbcType="TIMESTAMP"/>
        <result property="syncStatus" column="sync_status" jdbcType="INTEGER"/>
        <result property="origin" column="origin" jdbcType="INTEGER"/>
        <result property="staticUrl" column="static_url" jdbcType="VARCHAR"/>
        <association property="apArticleConfig" javaType="com.brianxia.articledemo.entity.ApArticleConfig">
            <id property="id" column="cid" jdbcType="INTEGER"/>
            <result property="articleId" column="article_id" jdbcType="VARCHAR"/>
            <result property="isComment" column="is_comment" jdbcType="INTEGER"/>
            <result property="isForward" column="is_forward" jdbcType="INTEGER"/>
            <result property="isDown" column="is_down" jdbcType="INTEGER"/>
            <result property="isDelete" column="is_delete" jdbcType="INTEGER"/>
        </association>

        <collection property="configs" ofType="com.brianxia.articledemo.entity.ApArticleConfig">
            <id property="id" column="cid" jdbcType="INTEGER"/>
            <result property="articleId" column="article_id" jdbcType="VARCHAR"/>
            <result property="isComment" column="is_comment" jdbcType="INTEGER"/>
            <result property="isForward" column="is_forward" jdbcType="INTEGER"/>
            <result property="isDown" column="is_down" jdbcType="INTEGER"/>
            <result property="isDelete" column="is_delete" jdbcType="INTEGER"/>
        </collection>

    </resultMap>

    <select id="queryByIds" resultMap="ApArticleAndConfigMap">
        SELECT aa.*,aac.id cid,aac.article_id,aac.is_comment ,aac.is_forward ,aac.is_down ,aac.is_delete
        FROM ap_article aa
                 left join ap_article_config aac on aa.id = aac.article_id
        where aa.id = #{id}
    </select>
    =================================================
# 应用名称
spring.application.name=article-demo
# 应用服务 WEB 访问端口
server.port=8080
#下面这些内容是为了让MyBatis映射
#指定MybatisMapper文件,配置文件要放在resources/mapper/*.xml
mybatis.mapper-locations=classpath:mapper/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.brianxia.articledemo.mybatis.entity
spring.datasource.url=jdbc:mysql://localhost:3306/leadnews_article?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最好的期待,未来可期

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值