经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Mybatis使用连表查询的操作代码
来源:jb51  时间:2022/8/22 14:55:04  对本文有异议

某天,产品经理给了这么一个需求技术小哥,能不能帮用户添加一个搜索栏,查询包含某个关键字的所有类目。技术小哥稍微想了一下,目前跟类目相关的表有两个,一个是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. `system_id` 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. `system_id` 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. <!--继承基础类型BaseResultMap, association 一对一关联查询 -->
  21. <resultMap extends="BaseResultMap" id="ClassesResultMap" type="com.thomson.content.dao.model.ContentCategory">
  22. </resultMap>   <!-- 这一步是关键的连表查询 -->
  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.  
  3. import com.thomson.content.dao.model.ContentCategory;
  4. import org.apache.ibatis.annotations.Param;
  5.  
  6. import java.util.List;
  7.  
  8. /**
  9. * 类目ExtMapper
  10. * Created by Thomson on 2022/01/10.
  11. * 根据content_system 的 code 获取类目
  12. */
  13. public interface ContentCategoryExtMapper {
  14. int up(String code);
  15. int down(String code);
  16. List<ContentCategory> selectContentCategoryByCode(@Param("code") String code);
  17. }

接下来是实现类

  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.  
  15. import java.util.List;
  16.  
  17. /**
  18. * ContentCategoryService实现
  19. * Created by Thomson on 2021/01/10.
  20. */
  21. @Service
  22. @Transactional
  23. @BaseService
  24. public class ContentCategoryServiceImpl extends BaseServiceImpl<ContentCategoryMapper, ContentCategory, ContentCategoryExample> implements ContentCategoryService {
  25.  
  26. private static final Logger LOGGER = LoggerFactory.getLogger(ContentCategoryServiceImpl.class);
  27.  
  28. @Autowired
  29. ContentCategoryExtMapper ContentCategoryExtMapper;
  30.  
  31. // @Override
  32. public List<ContentCategory> selectContentCategoryByCode(String code) {
  33. return ContentCategoryExtMapper.selectContentCategoryByCode(code);
  34. }
  35.  
  36. }

在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) {
  10.      Map<String, Object> result = new HashMap<>(2);
  11. String code = "news";
  12. List<ContentCategory> categories = ContentCategoryService.selectContentCategoryByCode(code);
  13. System.out.print("\n"+categories+"\n");
  14.    result.put("rows", categories);     result.put("total", total);
  15. return result;
  16. }

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

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号