1.1 自動(dòng)展示所有信息
-
需求描述: 進(jìn)入新聞首頁(yè)portal/findAllType, 自動(dòng)返回所有欄目名稱和id
-
接口描述
url地址:portal/findAllTypes
請(qǐng)求方式:get
請(qǐng)求參數(shù):無(wú)
響應(yīng)數(shù)據(jù):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-821512.html
成功
{
"code":"200",
"message":"OK"
"data":{
[
{
"tid":"1",
"tname":"新聞"
},
{
"tid":"2",
"tname":"體育"
},
{
"tid":"3",
"tname":"娛樂(lè)"
},
{
"tid":"4",
"tname":"科技"
},
{
"tid":"5",
"tname":"其他"
}
]
}
}
- 代碼編寫
PortalController :
package com.sunsplanter.controller;
import com.sunsplanter.service.TypeService;
import com.sunsplanter.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("portal")
public class PortalController {
@Autowired
private TypeService typeService;
@GetMapping("findAllType")
public Result findAllTypes(){
Result result = typeService.findAllTypes();
return result;
}
}
TypeService:
package com.sunsplanter.service;
import com.sunsplanter.pojo.Type;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sunsplanter.utils.Result;
public interface TypeService extends IService<Type>{
Result findAllTypes();
}
TypeServiceImpl:
package com.sunsplanter.service.impl;
import com.sunsplanter.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunsplanter.mapper.TypeMapper;
import com.sunsplanter.pojo.Type;
import com.sunsplanter.service.TypeService;
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService{
@Autowired
private TypeMapper typeMapper;
@Override
public Result findAllTypes() {
//不傳條件構(gòu)造器,即查詢?nèi)?/span>
List<Type> types = typeMapper.selectList(null);
return Result.ok(types);
}
}
達(dá)到的效果是,不需要任何參數(shù), 只要訪問(wèn)portal/findAllType, 就返回news_type表中的所有數(shù)據(jù)(version和is_deleted除外, 因?yàn)橐言趯?shí)體類中注解為版本和邏輯刪除)
1.2 - 查詢頭條詳情
- 需求描述
- 用戶點(diǎn)擊"查看全文"時(shí),向服務(wù)端發(fā)送新聞id
- 后端根據(jù)新聞id查詢完整新聞文章信息并返回
- 后端要同時(shí)讓新聞的瀏覽量+1
- 接口描述
url地址:portal/showHeadlineDetail
請(qǐng)求方式:post
請(qǐng)求參數(shù): Param傳參hid
響應(yīng)數(shù)據(jù):
成功則文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-821512.html
{
"code":"200",
"message":"success",
"data":{
"headline":{
"hid":"1", // 新聞id
"title":"馬斯克宣布 ... ...", // 新聞標(biāo)題
"article":"... ..." // 新聞?wù)?/span>
"type":"1", // 新聞所屬類別編號(hào)
"typeName":"科技", // 新聞所屬類別
"pageViews":"40", // 新聞瀏覽量
"pastHours":"3" , // 發(fā)布時(shí)間已過(guò)小時(shí)數(shù)
"publisher":"1" , // 發(fā)布用戶ID
"author":"張三" // 新聞作者
}
}
}
- 代碼實(shí)現(xiàn)
- controller
@Override
public Result showHeadlineDetail(Integer hid) {
/**注意響應(yīng)的數(shù)據(jù)是雙層嵌套,即data包裹headline,headline包含查詢到的屬性參數(shù)
* 先用一個(gè)名為dataMap的Map以鍵值對(duì)的形式存儲(chǔ)返回的屬性參數(shù)
* 再將名為data的Map是為一個(gè)值,搭配上名為headline的鍵
* 存儲(chǔ)進(jìn)一個(gè)名為headlineMap的Map中,最終將Map作為參數(shù)傳入Result,返回Result
*/
Map dataMap = headlineMapper.queryDetailMap(hid);
Map headlineMap = new HashMap<>();
headlineMap.put("headline",dataMap);
/*樂(lè)觀鎖修改閱讀量+1
*上面已經(jīng)通過(guò)hid查到了所有信息,包括當(dāng)時(shí)的版本號(hào),假設(shè)是2
* 將2直接賦值到新建的headline的Version中
* 在最后一句update中,MP會(huì)幫我們檢查,如果此時(shí)該條記錄的版本號(hào)仍為2,
* 則說(shuō)明這段時(shí)間沒(méi)有人修改過(guò)這條記錄,可以正常修改
*/
Headline headline = new Headline();
headline.setHid(hid);
headline.setPageViews((Integer) headlineMap.get("pageViews")+1); //閱讀量+1
headline.setVersion((Integer) headlineMap.get("version")); //設(shè)置版本
headlineMapper.updateById(headline);
return Result.ok(headlineMap);
}
- HeadlineMapper.java接口
/**
* 分頁(yè)查詢頭條詳情
* @param hid
* @return
*/
Map selectDetailMap(Integer hid);
mapperxml:
<!-- Map selectDetailMap(Integer hid);
查詢目標(biāo)(三表拼接):
"hid":"1", // 新聞id
"title":"馬斯克宣布 ... ...", // 新聞標(biāo)題
"article":"... ..." // 新聞?wù)?/span>
"type":"1", // 新聞所屬類別編號(hào)
"typeName":"科技", // 新聞所屬類別
"pageViews":"40", // 新聞瀏覽量
"pastHours":"3" , // 發(fā)布時(shí)間已過(guò)小時(shí)數(shù)
"publisher":"1" , // 發(fā)布用戶ID
"author":"張三" // 新聞作者
-->
/*
left join news_type t on h.type = t.tid: 這是一個(gè)左連接,將 "news_headline" 表與 "news_type" 表連接。
它的條件是 "news_headline" 表的 "type" 字段與 "news_type" 表的 "tid" 字段相匹配。
news_type中tid匹配的行會(huì)右拼接在headline表中
left join news_user u on h.publisher = u.uid: 這也是一個(gè)左連接,將 "news_headline" 表與 "news_user" 表連接。
連接條件是 "news_headline" 表的 "publisher" 字段與 "news_user" 表的 "uid" 字段相匹配。
news_user中tid匹配的行會(huì)右拼接在headline表中(headline先拼type,再拼user)
左連接確保左表保留所有信息,右表僅提取符合條件的元素匹配左表
*/
<select id="selectDetailMap" resultType="map">
select hid,title,article,type, h.version ,tname typeName ,page_views pageViews
,TIMESTAMPDIFF(HOUR,create_time,NOW()) pastHours,publisher
,nick_name author from news_headline h
left join news_type t on h.type = t.tid
left join news_user u on h.publisher = u.uid
where hid = #{hid}
</select>
到了這里,關(guān)于03 SpringBoot實(shí)戰(zhàn) -微頭條之首頁(yè)門戶模塊(跳轉(zhuǎn)某頁(yè)面自動(dòng)展示所有信息+根據(jù)hid查詢文章全文并用樂(lè)觀鎖修改閱讀量)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!