Mybatis使用连表查询的操作代码

 更新时间:2022年08月22日 11:38:16   作者:日积硅步  
这篇文章主要介绍了Mybatis如何使用连表查询,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

某天,产品经理给了这么一个需求技术小哥,能不能帮用户添加一个搜索栏,查询包含某个关键字的所有类目。技术小哥稍微想了一下,目前跟类目相关的表有两个,一个是content_category类目表,一个是content_system内容系统表。而用户要查找的关键字是存在content_system表里面,这样一来需要连表查询一下。难度好像不大,也就爽快地答应了。

技术小哥再仔细分析了一下两个表的结构:

CREATE TABLE `content_category` (
  `category_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '类目编号Id',
  `pid` int(10) unsigned DEFAULT NULL COMMENT '上级编号id',
  `level` tinyint(4) NOT NULL COMMENT '层级',
  `name` varchar(20) NOT NULL COMMENT '名称',
  `description` varchar(200) DEFAULT NULL COMMENT '描述',
  `icon` varchar(50) DEFAULT NULL COMMENT '图标',
  `type` tinyint(3) NOT NULL DEFAULT '1' COMMENT '类型(1:普通,2:热门...)',
  `alias` varchar(20) DEFAULT NULL COMMENT '别名',
  `system_id` int(11) DEFAULT NULL COMMENT '系统编号id',
  `ctime` bigint(20) unsigned NOT NULL COMMENT '创建时间',
  `orders` bigint(255) unsigned NOT NULL COMMENT '排序',
  `attention` bigint(20) unsigned NOT NULL COMMENT '关注度',
  PRIMARY KEY (`category_id`),
  KEY `content_category_orders` (`orders`),
  KEY `content_category_pid` (`pid`),
  KEY `content_category_alias` (`alias`),
  KEY `content_category_level` (`level`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='内容类目表';

CREATE TABLE `content_system` (
  `system_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '系统编号id',
  `name` varchar(20) NOT NULL COMMENT '系统名称',
  `code` varchar(20) DEFAULT NULL COMMENT '别名',
  `description` varchar(300) DEFAULT NULL COMMENT '描述',
  `ctime` bigint(20) DEFAULT NULL COMMENT '创建时间',
  `orders` bigint(20) DEFAULT NULL COMMENT '排序',
  PRIMARY KEY (`system_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='内容系统表';

不难看出,两个表都有system_id作为关联,当用户输入一个关键字,例如 code = "news" 时候,系统需要自动搜索出 system_id = 1 的所有类目信息。

于是技术小哥开始了他的工作,首先是定义Mapper.xml

<?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.thomson.content.rpc.mapper.ContentCategoryExtMapper">
    <!-- 定义基础类型 -->
    <resultMap id="BaseResultMap" type="com.thomson.content.dao.model.ContentCategory">
        <!-- 实体类的字段名和数据表的字段名映射 -->
        <id column="category_id" jdbcType="INTEGER" property="categoryId" />
        <result column="pid" jdbcType="INTEGER" property="pid" />
        <result column="level" jdbcType="TINYINT" property="level" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="description" jdbcType="VARCHAR" property="description" />
        <result column="icon" jdbcType="VARCHAR" property="icon" />
        <result column="type" jdbcType="TINYINT" property="type" />
        <result column="alias" jdbcType="VARCHAR" property="alias" />
        <result column="system_id" jdbcType="INTEGER" property="systemId" />
        <result column="ctime" jdbcType="BIGINT" property="ctime" />
        <result column="orders" jdbcType="BIGINT" property="orders" />
        <result column="attention" jdbcType="BIGINT" property="attention" />
    </resultMap>
    <!--继承基础类型BaseResultMap, association 一对一关联查询 -->
    <resultMap extends="BaseResultMap" id="ClassesResultMap" type="com.thomson.content.dao.model.ContentCategory">
    </resultMap>      <!-- 这一步是关键的连表查询 -->
    <select id="selectContentCategoryByCode" parameterType="map" resultMap="ClassesResultMap">
        select content_c.* from content_category content_c left join content_system content_s on content_s.code=content_s.code=#{code,jdbcType=VARCHAR}
         where content_s.system_id=content_c.system_id
    </select>
    <!-- 缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
</mapper>

然后是Mapper接口

package com.thomson.content.rpc.mapper;

import com.thomson.content.dao.model.ContentCategory;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 类目ExtMapper
 * Created by Thomson on 2022/01/10.
 * 根据content_system 的 code 获取类目
 */
public interface ContentCategoryExtMapper {
    int up(String code);
    int down(String code);
    List<ContentCategory> selectContentCategoryByCode(@Param("code") String code);
}

接下来是实现类

import com.thomson.Content.dao.model.ContentArticle;
import com.thomson.Content.rpc.mapper.ContentCategoryExtMapper;
import com.thomson.common.annotation.BaseService;
import com.thomson.common.base.BaseServiceImpl;
import com.thomson.Content.dao.mapper.ContentCategoryMapper;
import com.thomson.Content.dao.model.ContentCategory;
import com.thomson.Content.dao.model.ContentCategoryExample;
import com.thomson.Content.rpc.api.ContentCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* ContentCategoryService实现
* Created by Thomson on 2021/01/10.
*/
@Service
@Transactional
@BaseService
public class ContentCategoryServiceImpl extends BaseServiceImpl<ContentCategoryMapper, ContentCategory, ContentCategoryExample> implements ContentCategoryService  {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContentCategoryServiceImpl.class);

    @Autowired
    ContentCategoryExtMapper ContentCategoryExtMapper;

    // @Override
    public List<ContentCategory> selectContentCategoryByCode(String code) {
        return ContentCategoryExtMapper.selectContentCategoryByCode(code);
    }

}

在controll下获取数据

@ApiOperation(value = "类目列表")
    @RequiresPermissions("content:category:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
     Map<String, Object> result = new HashMap<>(2);
        String code = "news";
        List<ContentCategory> categories = ContentCategoryService.selectContentCategoryByCode(code);
        System.out.print("\n"+categories+"\n");
      result.put("rows", categories);     result.put("total", total);
return result;
    }

至此,技术小哥获取到了自己想要的数据。顺利完成了产品经理给的任务。

到此这篇关于Mybatis如何使用连表查询的文章就介绍到这了,更多相关Mybatis连表查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java读取配置文件(properties)的时候,unicode码转utf-8方式

    java读取配置文件(properties)的时候,unicode码转utf-8方式

    这篇文章主要介绍了java读取配置文件(properties)的时候,unicode码转utf-8方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • lambda表达式解决java后台分组排序过程解析

    lambda表达式解决java后台分组排序过程解析

    这篇文章主要介绍了lambda表达式解决java后台分组排序过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java Shell springboot通用Shell启动脚本方式

    Java Shell springboot通用Shell启动脚本方式

    这篇文章主要介绍了Java Shell springboot通用Shell启动脚本方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • SpringBoot外部化配置使用Plus版的方法示例

    SpringBoot外部化配置使用Plus版的方法示例

    这篇文章主要介绍了SpringBoot外部化配置使用Plus版的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 深入理解hibernate的三种状态

    深入理解hibernate的三种状态

    本篇文章主要介绍了深入理解hibernate的三种状态 ,主要包括了transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),有兴趣的同学可以了解一下
    2017-05-05
  • 浅析Java中的XML文件处理

    浅析Java中的XML文件处理

    ‌XML‌是一种用于存储和传输数据的标记语言,由W3C(万维网联盟)于1998年发布,本文主要来和大家聊聊Java中XML文件处理的相关知识,有需要的可以参考下
    2024-11-11
  • Open Feign之非SpringCloud方式使用示例

    Open Feign之非SpringCloud方式使用示例

    这篇文章主要为大家介绍了Open Feign之非SpringCloud方式使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 在Maven下代理服务器设定的方式

    在Maven下代理服务器设定的方式

    今天小编就为大家分享一篇关于在Maven下代理服务器设定的方式,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Java订单30分钟未支付自动取消该怎么实现

    Java订单30分钟未支付自动取消该怎么实现

    在开发中往往会遇到一些关于延时任务的需求,例如生成订单30分钟未支付,则自动取消,下面这篇文章主要给大家介绍了关于Java订单30分钟未支付自动取消该怎么实现的相关资料,需要的朋友可以参考下
    2023-03-03
  • Mybatis-Plus使用@TableField实现自动填充日期的代码示例

    Mybatis-Plus使用@TableField实现自动填充日期的代码示例

    数据库中经常有create_time,update_time两个字段,在代码中设置时间有点太麻烦了 mybatis-plus可以帮我们自动填充,本文主要介绍了Mybatis-Plus使用@TableField实现自动填充日期的代码示例,感兴趣的可以了解一下
    2022-04-04

最新评论