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

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

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

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

技術小哥再仔細分析了一下兩個表的結構:

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 '關注度',
  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作為關聯(lián),當用戶輸入一個關鍵字,例如 code = "news" 時候,系統(tǒng)需要自動搜索出 system_id = 1 的所有類目信息。

于是技術小哥開始了他的工作,首先是定義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">
    <!-- 定義基礎類型 -->
    <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>
    <!--繼承基礎類型BaseResultMap, association 一對一關聯(lián)查詢 -->
    <resultMap extends="BaseResultMap" id="ClassesResultMap" type="com.thomson.content.dao.model.ContentCategory">
    </resultMap>      <!-- 這一步是關鍵的連表查詢 -->
    <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ù)據(jù)。順利完成了產(chǎn)品經(jīng)理給的任務。

到此這篇關于Mybatis如何使用連表查詢的文章就介紹到這了,更多相關Mybatis連表查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

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

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

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

    Mybatis Generator逆向工程的使用詳細教程

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

    詳解Springboot自定義異常處理

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

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

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

    爬蟲技術詳解

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

    java編譯時與運行時概念與實例詳解

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

    SpringBoot 單元測試JUnit的使用詳解

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

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

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

    Java concurrency之互斥鎖_動力節(jié)點Java學院整理

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

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

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

最新評論