最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis使用連表查詢的操作代碼

 更新時(shí)間:2022年08月22日 11:38:16   作者:日積硅步  
這篇文章主要介紹了Mybatis如何使用連表查詢,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

某天,產(chǎn)品經(jīng)理給了這么一個(gè)需求技術(shù)小哥,能不能幫用戶添加一個(gè)搜索欄,查詢包含某個(gè)關(guān)鍵字的所有類目。技術(shù)小哥稍微想了一下,目前跟類目相關(guān)的表有兩個(gè),一個(gè)是content_category類目表,一個(gè)是content_system內(nèi)容系統(tǒng)表。而用戶要查找的關(guān)鍵字是存在content_system表里面,這樣一來需要連表查詢一下。難度好像不大,也就爽快地答應(yīng)了。

技術(shù)小哥再仔細(xì)分析了一下兩個(gè)表的結(jié)構(gòu):

CREATE TABLE `content_category` (
  `category_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '類目編號(hào)Id',
  `pid` int(10) unsigned DEFAULT NULL COMMENT '上級(jí)編號(hào)id',
  `level` tinyint(4) NOT NULL COMMENT '層級(jí)',
  `name` varchar(20) NOT NULL COMMENT '名稱',
  `description` varchar(200) DEFAULT NULL COMMENT '描述',
  `icon` varchar(50) DEFAULT NULL COMMENT '圖標(biāo)',
  `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)編號(hào)id',
  `ctime` bigint(20) unsigned NOT NULL COMMENT '創(chuàng)建時(shí)間',
  `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)編號(hào)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)建時(shí)間',
  `orders` bigint(20) DEFAULT NULL COMMENT '排序',
  PRIMARY KEY (`system_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='內(nèi)容系統(tǒng)表';

不難看出,兩個(gè)表都有system_id作為關(guān)聯(lián),當(dāng)用戶輸入一個(gè)關(guān)鍵字,例如 code = "news" 時(shí)候,系統(tǒng)需要自動(dò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í)體類的字段名和數(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 一對(duì)一關(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);
}

接下來是實(shí)現(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實(shí)現(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java讀取配置文件(properties)的時(shí)候,unicode碼轉(zhuǎn)utf-8方式

    java讀取配置文件(properties)的時(shí)候,unicode碼轉(zhuǎn)utf-8方式

    這篇文章主要介紹了java讀取配置文件(properties)的時(shí)候,unicode碼轉(zhuǎn)utf-8方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • lambda表達(dá)式解決java后臺(tái)分組排序過程解析

    lambda表達(dá)式解決java后臺(tái)分組排序過程解析

    這篇文章主要介紹了lambda表達(dá)式解決java后臺(tái)分組排序過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Java?Shell?springboot通用Shell啟動(dòng)腳本方式

    Java?Shell?springboot通用Shell啟動(dòng)腳本方式

    這篇文章主要介紹了Java?Shell?springboot通用Shell啟動(dòng)腳本方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot外部化配置使用Plus版的方法示例

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

    這篇文章主要介紹了SpringBoot外部化配置使用Plus版的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 深入理解hibernate的三種狀態(tài)

    深入理解hibernate的三種狀態(tài)

    本篇文章主要介紹了深入理解hibernate的三種狀態(tài) ,主要包括了transient(瞬時(shí)狀態(tài)),persistent(持久化狀態(tài))以及detached(離線狀態(tài)),有興趣的同學(xué)可以了解一下
    2017-05-05
  • 淺析Java中的XML文件處理

    淺析Java中的XML文件處理

    ?XML?是一種用于存儲(chǔ)和傳輸數(shù)據(jù)的標(biāo)記語言,由W3C(萬維網(wǎng)聯(lián)盟)于1998年發(fā)布,本文主要來和大家聊聊Java中XML文件處理的相關(guān)知識(shí),有需要的可以參考下
    2024-11-11
  • Open?Feign之非SpringCloud方式使用示例

    Open?Feign之非SpringCloud方式使用示例

    這篇文章主要為大家介紹了Open?Feign之非SpringCloud方式使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 在Maven下代理服務(wù)器設(shè)定的方式

    在Maven下代理服務(wù)器設(shè)定的方式

    今天小編就為大家分享一篇關(guān)于在Maven下代理服務(wù)器設(shè)定的方式,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java訂單30分鐘未支付自動(dòng)取消該怎么實(shí)現(xiàn)

    Java訂單30分鐘未支付自動(dòng)取消該怎么實(shí)現(xiàn)

    在開發(fā)中往往會(huì)遇到一些關(guān)于延時(shí)任務(wù)的需求,例如生成訂單30分鐘未支付,則自動(dòng)取消,下面這篇文章主要給大家介紹了關(guān)于Java訂單30分鐘未支付自動(dòng)取消該怎么實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • Mybatis-Plus使用@TableField實(shí)現(xiàn)自動(dòng)填充日期的代碼示例

    Mybatis-Plus使用@TableField實(shí)現(xiàn)自動(dòng)填充日期的代碼示例

    數(shù)據(jù)庫中經(jīng)常有create_time,update_time兩個(gè)字段,在代碼中設(shè)置時(shí)間有點(diǎn)太麻煩了?mybatis-plus可以幫我們自動(dòng)填充,本文主要介紹了Mybatis-Plus使用@TableField實(shí)現(xiàn)自動(dòng)填充日期的代碼示例,感興趣的可以了解一下
    2022-04-04

最新評(píng)論

曲阜市| 旺苍县| 台北市| 绍兴市| 勐海县| 黎平县| 巫山县| 米脂县| 丰城市| 类乌齐县| 青州市| 英超| 遂溪县| 收藏| 黄平县| 陆良县| 丰镇市| 平阳县| 茌平县| 修文县| 时尚| 玉屏| 台湾省| 屏东县| 法库县| 青铜峡市| 阜阳市| 汝城县| 仙桃市| 卢湾区| 嫩江县| 安图县| 康定县| 建德市| 株洲县| 烟台市| 宣恩县| 西盟| 茌平县| 包头市| 巴楚县|