欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年08月22日 11:38:16   作者:日積硅步  
這篇文章主要介紹了Mybatis如何使用連表查詢,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(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 一對一關(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java?InputStream實(shí)戰(zhàn)之輕松讀取操作文件流

    Java?InputStream實(shí)戰(zhàn)之輕松讀取操作文件流

    在Java中,輸入輸出是非常重要的基礎(chǔ)功能,其中,InputStream是Java中的一個(gè)重要輸入流類,用于從輸入源讀取數(shù)據(jù),下面我們就來學(xué)習(xí)一下InputStream類的相關(guān)知識(shí)吧
    2023-10-10
  • Mybatis Generator逆向工程的使用詳細(xì)教程

    Mybatis Generator逆向工程的使用詳細(xì)教程

    這篇文章主要介紹了Mybatis Generator逆向工程的使用詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 詳解Springboot自定義異常處理

    詳解Springboot自定義異常處理

    本篇文章主要介紹了詳解Springboot自定義異常處理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Kotlin內(nèi)存陷阱inline使用技巧示例詳解

    Kotlin內(nèi)存陷阱inline使用技巧示例詳解

    這篇文章主要為大家介紹了Kotlin內(nèi)存陷阱inline使用技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 爬蟲技術(shù)詳解

    爬蟲技術(shù)詳解

    本文全面的介紹了爬蟲的原理、技術(shù)現(xiàn)狀、以及目前仍面臨的問題。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • java編譯時(shí)與運(yùn)行時(shí)概念與實(shí)例詳解

    java編譯時(shí)與運(yùn)行時(shí)概念與實(shí)例詳解

    本篇文章通過實(shí)例對 java程序編譯時(shí)與運(yùn)行時(shí)進(jìn)行了詳解,需要的朋友可以參考下
    2017-04-04
  • SpringBoot 單元測試JUnit的使用詳解

    SpringBoot 單元測試JUnit的使用詳解

    這篇文章主要介紹了SpringBoot 單元測試JUnit的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • MyBatis字段名和屬性名不一致的解決方法

    MyBatis字段名和屬性名不一致的解決方法

    本文主要介紹了MyBatis字段名和屬性名不一致的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Java concurrency之互斥鎖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java concurrency之互斥鎖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    本文通過示例代碼給大家介紹了Java concurrency之互斥鎖的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-06-06
  • 簡單的理解java集合中的HashSet和HashTree幾個(gè)重寫方法

    簡單的理解java集合中的HashSet和HashTree幾個(gè)重寫方法

    這篇文章主要介紹了簡單的理解java集合中的HashSet和HashTree幾個(gè)重寫方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評論