需要使用一个插件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映射
#指定Mybatis的Mapper文件,配置文件要放在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
使用Mybatis,resultMap完成<collection> 一对多 <association> 一对一
最新推荐文章于 2024-03-14 20:17:57 发布