Mybatis使用連表查詢的操作代碼
某天,產(chǎn)品經(jīng)理給了這么一個需求技術(shù)小哥,能不能幫用戶添加一個搜索欄,查詢包含某個關(guān)鍵字的所有類目。技術(shù)小哥稍微想了一下,目前跟類目相關(guān)的表有兩個,一個是content_category類目表,一個是content_system內(nèi)容系統(tǒng)表。而用戶要查找的關(guān)鍵字是存在content_system表里面,這樣一來需要連表查詢一下。難度好像不大,也就爽快地答應(yīng)了。
技術(shù)小哥再仔細分析了一下兩個表的結(jié)構(gòu):
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 '系統(tǒng)編號id', `ctime` bigint(20) unsigned NOT NULL COMMENT '創(chuàng)建時間', `orders` bigint(255) unsigned NOT NULL COMMENT '排序', `attention` bigint(20) unsigned NOT NULL COMMENT '關(guān)注度', 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='內(nèi)容類目表';

CREATE TABLE `content_system` ( `system_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '系統(tǒng)編號id', `name` varchar(20) NOT NULL COMMENT '系統(tǒng)名稱', `code` varchar(20) DEFAULT NULL COMMENT '別名', `description` varchar(300) DEFAULT NULL COMMENT '描述', `ctime` bigint(20) DEFAULT NULL COMMENT '創(chuàng)建時間', `orders` bigint(20) DEFAULT NULL COMMENT '排序', PRIMARY KEY (`system_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='內(nèi)容系統(tǒng)表';

不難看出,兩個表都有system_id作為關(guān)聯(lián),當用戶輸入一個關(guān)鍵字,例如 code = "news" 時候,系統(tǒng)需要自動搜索出 system_id = 1 的所有類目信息。
于是技術(shù)小哥開始了他的工作,首先是定義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">
<!-- 定義基礎(chǔ)類型 -->
<resultMap id="BaseResultMap" type="com.thomson.content.dao.model.ContentCategory">
<!-- 實體類的字段名和數(shù)據(jù)表的字段名映射 -->
<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>
<!--繼承基礎(chǔ)類型BaseResultMap, association 一對一關(guān)聯(lián)查詢 -->
<resultMap extends="BaseResultMap" id="ClassesResultMap" type="com.thomson.content.dao.model.ContentCategory">
</resultMap> <!-- 這一步是關(guān)鍵的連表查詢 -->
<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.
* 根據(jù)content_system 的 code 獲取類目
*/
public interface ContentCategoryExtMapper {
int up(String code);
int down(String code);
List<ContentCategory> selectContentCategoryByCode(@Param("code") String code);
}接下來是實現(xiàn)類
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實現(xiàn)
* 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下獲取數(shù)據(jù)
@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;
}至此,技術(shù)小哥獲取到了自己想要的數(shù)據(jù)。順利完成了產(chǎn)品經(jīng)理給的任務(wù)。
到此這篇關(guān)于Mybatis如何使用連表查詢的文章就介紹到這了,更多相關(guān)Mybatis連表查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java讀取配置文件(properties)的時候,unicode碼轉(zhuǎn)utf-8方式
這篇文章主要介紹了java讀取配置文件(properties)的時候,unicode碼轉(zhuǎn)utf-8方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Java?Shell?springboot通用Shell啟動腳本方式
這篇文章主要介紹了Java?Shell?springboot通用Shell啟動腳本方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Mybatis-Plus使用@TableField實現(xiàn)自動填充日期的代碼示例
數(shù)據(jù)庫中經(jīng)常有create_time,update_time兩個字段,在代碼中設(shè)置時間有點太麻煩了?mybatis-plus可以幫我們自動填充,本文主要介紹了Mybatis-Plus使用@TableField實現(xiàn)自動填充日期的代碼示例,感興趣的可以了解一下2022-04-04

