博客地址:ONESTARの客棧
源碼領取方式一:
- 掃一掃文末二維碼,關注公衆號【編程日刊】,後臺回覆【博客】,即可領取源碼
源碼領取方式二:
以jpa爲持久層源碼地址:https://github.com/oneStarLR/myblog-jpa
以mybatis爲持久層源碼地址:https://github.com/oneStarLR/myblog-mybaits
歡迎給star以鼓勵(^_−)☆
由於友鏈管理比較簡單,本文將一次性直接列出所有接口,再逐個分析,功能分別有:刪除、編輯修改、新增
一、友鏈管理持久層接口
同樣,以問答的形式來書寫本篇文章
問:友鏈持久層需要哪些接口?
答:根據友鏈功能來看,需要最基本的新增、刪除、修改、查詢
問:光這些接口夠了嗎?在編輯修改友鏈的時候,爲了方便用戶修改,是不是要將待修改的數據信息也傳遞給修改頁面呢,另外,在新增的時候,如果新增相同的友鏈是不是要作出判斷呢?
答:除了增(saveFriendLink)、刪(deleteFriendLink)、改(updateFriendLink)、查(listFriendLink)之外,還需要getFriendLink、getFriendLinkByBlogaddress用來編輯修改友鏈傳遞數據和做重複判斷
在dao包下創建FriendLinkDao接口,代碼如下:
package com.star.dao;
import com.star.entity.FriendLink;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Description:
* @Date: Created in 11:29 2020/6/14
* @Author: ONESTAR
* @QQ羣: 530311074
* @URL: https://onestar.newstar.net.cn/
*/
@Mapper
@Repository
public interface FriendLinkDao {
//查詢友鏈管理列表
List<FriendLink> listFriendLink();
//新增友鏈
int saveFriendLink(FriendLink friendLink);
//根據網址查詢友鏈
FriendLink getFriendLinkByBlogaddress(String blogaddress);
//根據id查詢友鏈
FriendLink getFriendLink(Long id);
//編輯修改友鏈
int updateFriendLink(FriendLink friendLink);
//刪除友鏈
void deleteFriendLink(Long id);
}
講解:
- listFriendLink:查詢出友鏈管理列表
- saveFriendLink:友鏈新增
- getFriendLinkByBlogaddress:在新增友鏈的時候做重複判斷,查詢到有相同友鏈給出提示
- getFriendLink:編輯修改友鏈時查詢出數據信息並顯示在前端頁面上
- deleteFriendLink:刪除友鏈
二、友鏈管理mapper
友鏈管理的查詢語句中,就是根據持久層接口來設計的,比較簡單,不做分析,直接給出
在mapper文件夾下創建FriendLinkDao.xml文件,編寫SQL,如下:
<?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.star.dao.FriendLinkDao">
<!--查詢所有友鏈-->
<select id="listFriendLink" resultType="com.star.entity.FriendLink">
select * from myblog.t_friend order by t_friend.create_time desc
</select>
<!--添加友鏈-->
<insert id="saveFriendLink" parameterType="com.star.entity.FriendLink">
insert into myblog.t_friend (blogname,blogaddress,pictureaddress,create_time)
values (#{blogname},#{blogaddress},#{pictureaddress},#{createTime})
</insert>
<!--根據網址查詢友鏈-->
<select id="getFriendLinkByBlogaddress" resultType="com.star.entity.FriendLink">
select * from myblog.t_friend f where f.blogaddress = #{blogaddress}
</select>
<!--根據id查詢友鏈-->
<select id="getFriendLink" resultType="com.star.entity.FriendLink">
select * from myblog.t_friend f where f.id = #{id}
</select>
<!--編輯修改友鏈-->
<update id="updateFriendLink" parameterType="com.star.entity.FriendLink">
update myblog.t_friend set blogname = #{blogname}, blogaddress = #{blogaddress}, pictureaddress = #{pictureaddress} where id = #{id};
</update>
<!--刪除友鏈-->
<delete id="deleteFriendLink" >
delete from myblog.t_friend where id = #{id}
</delete>
</mapper>
三、友鏈管理業務層
業務層接口
這裏和持久層接口是一樣的,就不做分析,在service包下創建FriendLinkService接口,代碼如下:
package com.star.service;
import com.star.entity.FriendLink;
import java.util.List;
/**
* @Description: 友鏈業務層接口
* @Date: Created in 11:28 2020/6/14
* @Author: ONESTAR
* @QQ羣: 530311074
* @URL: https://onestar.newstar.net.cn/
*/
public interface FriendLinkService {
//查詢所有友鏈
List<FriendLink> listFriendLink();
//友鏈新增
int saveFriendLink(FriendLink friendLink);
//根據網址查詢友鏈
FriendLink getFriendLinkByBlogaddress(String blogaddress);
//根據id查詢友鏈
FriendLink getFriendLink(Long id);
//編輯修改友鏈
int updateFriendLink(FriendLink friendLink);
//刪除友鏈
void deleteFriendLink(Long id);
}
接口實現類
直接調用持久層接口,不做分析,在Impl包下創建FriendLinkServiceImpl類實現FriendLinkService接口,代碼如下:
package com.star.service.Impl;
import com.star.dao.FriendLinkDao;
import com.star.entity.FriendLink;
import com.star.service.FriendLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Description:
* @Date: Created in 11:28 2020/6/14
* @Author: ONESTAR
* @QQ羣: 530311074
* @URL: https://onestar.newstar.net.cn/
*/
@Service
public class FriendLinkServiceImpl implements FriendLinkService {
@Autowired
private FriendLinkDao friendLinkDao;
@Override
public List<FriendLink> listFriendLink() {
return friendLinkDao.listFriendLink();
}
@Override
public int saveFriendLink(FriendLink friendLink) {
return friendLinkDao.saveFriendLink(friendLink);
}
@Override
public FriendLink getFriendLinkByBlogaddress(String blogaddress) {
return friendLinkDao.getFriendLinkByBlogaddress(blogaddress);
}
@Override
public FriendLink getFriendLink(Long id) {
return friendLinkDao.getFriendLink(id);
}
@Override
public int updateFriendLink(FriendLink friendLink) {
return friendLinkDao.updateFriendLink(friendLink);
}
@Override
public void deleteFriendLink(Long id) {
friendLinkDao.deleteFriendLink(id);
}
}
四、友鏈管理控制器
很多知識點和之前都是重複的,不做講解,直接給代碼:
package com.star.controller.admin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.entity.FriendLink;
import com.star.service.FriendLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
/**
* @Description: 友鏈後臺管理控制器
* @Date: Created in 11:24 2020/6/14
* @Author: ONESTAR
* @QQ羣: 530311074
* @URL: https://onestar.newstar.net.cn/
*/
@Controller
@RequestMapping("/admin")
public class FriendController {
@Autowired
private FriendLinkService friendLinkService;
//查詢所有友鏈
@GetMapping("/friendlinks")
public String friend(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
PageHelper.startPage(pageNum,10);
List<FriendLink> listFriendLink = friendLinkService.listFriendLink();
PageInfo<FriendLink> pageInfo = new PageInfo<FriendLink>(listFriendLink);
model.addAttribute("pageInfo",pageInfo);
return "admin/friendlinks";
}
//跳轉友鏈新增頁面
@GetMapping("/friendlinks/input")
public String input(Model model) {
model.addAttribute("friendlink", new FriendLink());
return "admin/friendlinks-input";
}
//友鏈新增
@PostMapping("/friendlinks")
public String post(@Valid FriendLink friendLink, BindingResult result, RedirectAttributes attributes){
FriendLink type1 = friendLinkService.getFriendLinkByBlogaddress(friendLink.getBlogaddress());
if (type1 != null) {
attributes.addFlashAttribute("message", "不能添加相同的網址");
return "redirect:/admin/friendlinks/input";
}
if(result.hasErrors()){
return "admin/friendlinks-input";
}
friendLink.setCreateTime(new Date());
int F = friendLinkService.saveFriendLink(friendLink);
if (F == 0 ) {
attributes.addFlashAttribute("message", "新增失敗");
} else {
attributes.addFlashAttribute("message", "新增成功");
}
return "redirect:/admin/friendlinks";
}
//跳轉友鏈修改頁面
@GetMapping("/friendlinks/{id}/input")
public String editInput(@PathVariable Long id, Model model) {
model.addAttribute("friendlink", friendLinkService.getFriendLink(id));
return "admin/friendlinks-input";
}
//編輯修改友鏈
@PostMapping("/friendlinks/{id}")
public String editPost(@Valid FriendLink friendLink, RedirectAttributes attributes) {
int t = friendLinkService.updateFriendLink(friendLink);
if (t == 0 ) {
attributes.addFlashAttribute("message", "編輯失敗");
} else {
attributes.addFlashAttribute("message", "編輯成功");
}
return "redirect:/admin/friendlinks";
}
//刪除友鏈
@GetMapping("/friendlinks/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes){
friendLinkService.deleteFriendLink(id);
attributes.addFlashAttribute("message", "刪除成功");
return "redirect:/admin/friendlinks";
}
}
五、前後端交互
前端只給出部分代碼,詳細可以下載項目源碼查看:https://github.com/oneStarLR/myblog-mybatis
- 新增跳轉
<a href="#" th:href="@{/admin/friendlinks/input}">
<button type="button" class="ui teal button m-mobile-wide m-margin-top"><i class="pencil icon"></i>新增</button>
</a>
- 新增提交表單
<form action="#" method="post" th:action="*{id}==null ? @{/admin/friendlinks} : @{/admin/friendlinks/{id}(id=*{id})} " th:object="${friendlink}" class="ui form">
<input type="hidden" name="id" th:value="*{id}">
<div class=" field">
<div class="ui left labeled input m-margin-top">
<label class="ui teal basic label">博客名稱</label>
<input type="text" name="blogname" placeholder="博客名稱" th:value="*{blogname}" >
</div>
<div class="ui left labeled input m-margin-top">
<label class="ui teal basic label">博客地址</label>
<input type="text" name="blogaddress" placeholder="博客地址" th:value="*{blogaddress}" >
</div>
<div class="ui left labeled input m-margin-top">
<label class="ui teal basic label">圖片地址</label>
<input type="text" name="pictureaddress" placeholder="圖片地址" th:value="*{pictureaddress}" >
</div>
</div>
<div class="ui error message"></div>
<div class="ui mini negative message" th:unless="${#strings.isEmpty(message)}" th:text="|提示: ${message}">提示:不能添加重複的友鏈</div>
<div class="ui right aligned container">
<button type="button" class="ui button" onclick="window.history.go(-1)" >返回</button>
<button class="ui teal submit button">提交</button>
</div>
</form>
- 編輯刪除
<a href="#" th:href="@{/admin/friendlinks/{id}/input(id=${friendlink.id})}" class="ui mini teal basic button">編輯</a>
<a href="#" th:href="@{/admin/friendlinks/{id}/delete(id=${friendlink.id})}" onclick="return confirm('確定要刪除該友鏈嗎?三思啊! 刪了可就沒了!')" class="ui mini red basic button">刪除</a>
- 查詢友鏈列表
<tr align="center" th:each="friendlink,iterStat : ${pageInfo.list}">
<td th:text="${iterStat.count}">1</td>
<td th:text="${friendlink.blogname}">ONESTAR</td>
<td th:text="${friendlink.blogaddress}">https://onestar.newstar.net.cn/</td>
<td th:text="${friendlink.pictureaddress}">https://onestar.newstar.net.cn/</td>
<td th:text="${#dates.format(friendlink.createTime,'yyyy-MM-dd HH:mm')}">2020-01-02 09:45</td>
<!--th:text="${#dates.format(message.createTime,'yyyy-MM-dd HH:mm')}"-->
<td>
<a href="#" th:href="@{/admin/friendlinks/{id}/input(id=${friendlink.id})}" class="ui mini teal basic button">編輯</a>
<a href="#" th:href="@{/admin/friendlinks/{id}/delete(id=${friendlink.id})}" onclick="return confirm('確定要刪除該友鏈嗎?三思啊! 刪了可就沒了!')" class="ui mini red basic button">刪除</a>
</td>
</tr>
- 分頁查詢
<div class="ui inverted divided stackable grid">
<div class="three wide column" align="center">
<a class="item" th:href="@{/admin/friendlinks(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}" th:unless="${pageInfo.isFirstPage}">上一頁</a>
</div>
<div class="ten wide column" align="center">
<p>第 <span th:text="${pageInfo.pageNum}"></span> 頁,共 <span th:text="${pageInfo.pages}"></span> 頁,有 <span th:text="${pageInfo.total}"></span> 個分類</p>
</div>
<div class="three wide column" align="center">
<a class="item" th:href="@{/admin/friendlinks(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}" th:unless="${pageInfo.isLastPage}">下一頁</a>
</div>
</div>
六、運行訪問
運行項目,訪問 http://localhost:8080/admin, 登錄後點擊友鏈管理,可以對友鏈進行增、刪、改、查
至此,SpringBoot搭建個人博客的友鏈管理開發完成,下一篇將講述相冊管理
【點關注,不迷路,歡迎持續關注本站!】