Mybatis如何使用连表查询

打印 上一主题 下一主题

主题 886|帖子 886|积分 2658

某天,产品经理给了这么一个需求技术小哥,能不能帮用户添加一个搜索栏,查询包含某个关键字的所有类目。技术小哥稍微想了一下,目前跟类目相关的表有两个,一个是content_category类目表,一个是content_system内容系统表。而用户要查找的关键字是存在content_system表里面,这样一来需要连表查询一下。难度好像不大,也就爽快地答应了。
技术小哥再仔细分析了一下两个表的结构:
  1. CREATE TABLE `content_category` (
  2.   `category_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '类目编号Id',
  3.   `pid` int(10) unsigned DEFAULT NULL COMMENT '上级编号id',
  4.   `level` tinyint(4) NOT NULL COMMENT '层级',
  5.   `name` varchar(20) NOT NULL COMMENT '名称',
  6.   `description` varchar(200) DEFAULT NULL COMMENT '描述',
  7.   `icon` varchar(50) DEFAULT NULL COMMENT '图标',
  8.   `type` tinyint(3) NOT NULL DEFAULT '1' COMMENT '类型(1:普通,2:热门...)',
  9.   `alias` varchar(20) DEFAULT NULL COMMENT '别名',
  10.   <strong>`system_id` </strong>int(11) DEFAULT NULL COMMENT '系统编号id',
  11.   `ctime` bigint(20) unsigned NOT NULL COMMENT '创建时间',
  12.   `orders` bigint(255) unsigned NOT NULL COMMENT '排序',
  13.   `attention` bigint(20) unsigned NOT NULL COMMENT '关注度',
  14.   PRIMARY KEY (`category_id`),
  15.   KEY `content_category_orders` (`orders`),
  16.   KEY `content_category_pid` (`pid`),
  17.   KEY `content_category_alias` (`alias`),
  18.   KEY `content_category_level` (`level`)
  19. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='内容类目表';
复制代码

 
  1. CREATE TABLE `content_system` (
  2.   <strong>`system_id` </strong>int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '系统编号id',
  3.   `name` varchar(20) NOT NULL COMMENT '系统名称',
  4.   `code` varchar(20) DEFAULT NULL COMMENT '别名',
  5.   `description` varchar(300) DEFAULT NULL COMMENT '描述',
  6.   `ctime` bigint(20) DEFAULT NULL COMMENT '创建时间',
  7.   `orders` bigint(20) DEFAULT NULL COMMENT '排序',
  8.   PRIMARY KEY (`system_id`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='内容系统表';
复制代码

 
不难看出,两个表都有system_id作为关联,当用户输入一个关键字,例如 code = "news" 时候,系统需要自动搜索出 system_id = 1 的所有类目信息。
于是技术小哥开始了他的工作,首先是定义Mapper.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.thomson.content.rpc.mapper.ContentCategoryExtMapper">
  4.    
  5.     <resultMap id="BaseResultMap" type="com.thomson.content.dao.model.ContentCategory">
  6.         
  7.         <id column="category_id" jdbcType="INTEGER" property="categoryId" />
  8.         <result column="pid" jdbcType="INTEGER" property="pid" />
  9.         <result column="level" jdbcType="TINYINT" property="level" />
  10.         <result column="name" jdbcType="VARCHAR" property="name" />
  11.         <result column="description" jdbcType="VARCHAR" property="description" />
  12.         <result column="icon" jdbcType="VARCHAR" property="icon" />
  13.         <result column="type" jdbcType="TINYINT" property="type" />
  14.         <result column="alias" jdbcType="VARCHAR" property="alias" />
  15.         <result column="system_id" jdbcType="INTEGER" property="systemId" />
  16.         <result column="ctime" jdbcType="BIGINT" property="ctime" />
  17.         <result column="orders" jdbcType="BIGINT" property="orders" />
  18.         <result column="attention" jdbcType="BIGINT" property="attention" />
  19.     </resultMap>
  20.    
  21.     <resultMap extends="BaseResultMap" id="ClassesResultMap" type="com.thomson.content.dao.model.ContentCategory">
  22.     </resultMap><br>  <br>   
  23.     <select id="selectContentCategoryByCode" parameterType="map" resultMap="ClassesResultMap">
  24.         select content_c.* from content_category content_c left join content_system content_s on content_s.code=content_s.code=#{code,jdbcType=VARCHAR}
  25.          where content_s.system_id=content_c.system_id
  26.     </select>
  27.    
  28.     <cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
  29. </mapper>
复制代码
 
然后是Mapper接口
  1. package com.thomson.content.rpc.mapper;
  2. import com.thomson.content.dao.model.ContentCategory;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. /**
  6. * 类目ExtMapper
  7. * Created by Thomson on 2022/01/10.
  8. * 根据content_system 的 code 获取类目
  9. */
  10. public interface ContentCategoryExtMapper {
  11.     int up(String code);
  12.     int down(String code);
  13.   
  14.     List<ContentCategory> selectContentCategoryByCode(@Param("code") String code);
  15. }
复制代码
 
接下来是实现类
  1. import com.thomson.Content.dao.model.ContentArticle;
  2. import com.thomson.Content.rpc.mapper.ContentCategoryExtMapper;
  3. import com.thomson.common.annotation.BaseService;
  4. import com.thomson.common.base.BaseServiceImpl;
  5. import com.thomson.Content.dao.mapper.ContentCategoryMapper;
  6. import com.thomson.Content.dao.model.ContentCategory;
  7. import com.thomson.Content.dao.model.ContentCategoryExample;
  8. import com.thomson.Content.rpc.api.ContentCategoryService;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import java.util.List;
  15. /**
  16. * ContentCategoryService实现
  17. * Created by Thomson on 2021/01/10.
  18. */
  19. @Service
  20. @Transactional
  21. @BaseService
  22. public class ContentCategoryServiceImpl extends BaseServiceImpl<ContentCategoryMapper, ContentCategory, ContentCategoryExample> implements ContentCategoryService  {
  23.     private static final Logger LOGGER = LoggerFactory.getLogger(ContentCategoryServiceImpl.class);
  24.     @Autowired
  25.     ContentCategoryExtMapper ContentCategoryExtMapper;
  26.     // @Override
  27.     public List<ContentCategory> selectContentCategoryByCode(String code) {
  28.         return ContentCategoryExtMapper.selectContentCategoryByCode(code);
  29.     }
  30. }
复制代码
 
在controll下获取数据
  1.     @ApiOperation(value = "类目列表")
  2.     @RequiresPermissions("content:category:read")
  3.     @RequestMapping(value = "/list", method = RequestMethod.GET)
  4.     @ResponseBody
  5.     public Object list(
  6.             @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
  7.             @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
  8.             @RequestParam(required = false, value = "sort") String sort,
  9.             @RequestParam(required = false, value = "order") String order) {<br>
复制代码
  1.      Map<String, Object> result = new HashMap<>(2);
复制代码
  1.         String code = "news";
  2.         List<ContentCategory> categories = ContentCategoryService.selectContentCategoryByCode(code);
  3.         System.out.print("\n"+categories+"\n");<br>
复制代码
  1.       result.put("rows", categories);<br>     result.put("total", total);
复制代码
  1. return result;
  2.     }
复制代码
至此,技术小哥获取到了自己想要的数据。顺利完成了产品经理给的任务。 
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表