友链管理及代码生成器使用

master
barney 2 years ago
parent 9f2d8fb4f6
commit 7de32b26d9
  1. 5
      src/main/java/cc/bnblogs/controller/AdminController.java
  2. 70
      src/main/java/cc/bnblogs/controller/FriendsController.java
  3. 11
      src/main/java/cc/bnblogs/mapper/FriendsMapper.java
  4. 30
      src/main/java/cc/bnblogs/pojo/Friends.java
  5. 62
      src/main/java/cc/bnblogs/service/FriendsService.java
  6. 167
      src/main/resources/static/CodeGenerator/common.html
  7. 70
      src/main/resources/static/CodeGenerator/controller.java
  8. 11
      src/main/resources/static/CodeGenerator/mapper.java
  9. 62
      src/main/resources/static/CodeGenerator/service.java
  10. BIN
      src/main/resources/static/CodeGenerator/代码生成器.exe
  11. 2
      src/main/resources/templates/admin/common.html
  12. 177
      src/main/resources/templates/admin/friends.html

@ -19,4 +19,9 @@ public class AdminController {
public String getCategory() { public String getCategory() {
return "admin/category"; return "admin/category";
} }
@GetMapping("/friends.html")
public String getFriends() {
return "admin/friends";
}
} }

@ -0,0 +1,70 @@
package cc.bnblogs.controller;
import cc.bnblogs.common.Result;
import cc.bnblogs.enums.ResultEnum;
import cc.bnblogs.pojo.Friends;
import cc.bnblogs.service.FriendsService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
@RestController
@RequestMapping("/admin/friends")
public class FriendsController {
private final FriendsService friendsService;
public FriendsController(FriendsService friendsService) {
this.friendsService = friendsService;
}
/**
* 请求所有友链的接口
* @return 返回所有友链
*/
@GetMapping("/")
public Result<List<Friends>> list() {
return Result.success(friendsService.list());
}
/**
* 请求友链详情的接口
* @param id 友链id
* @return 返回友链的详情
*/
@GetMapping("/{id}")
public Result<Friends> detail(@PathVariable Integer id) {
Friends friends = friendsService.detail(id);
if (Objects.isNull(friends)) {
return Result.error(ResultEnum.RESULT_NOT_FOUND);
} else {
return Result.success(friends);
}
}
/**
* 保存修改后的友链数据
* @param friends 修改的友链
* @return 修改结果
*/
@PostMapping("/")
public Result<String> save(Friends friends) {
friendsService.save(friends);
return Result.success();
}
/**
* 删除一个友链
* @param id 删除的友链id
* @return 删除结果
*/
@DeleteMapping("/{id}")
public Result<String> delete(@PathVariable Integer id) {
friendsService.delete(id);
return Result.success();
}
}

@ -0,0 +1,11 @@
package cc.bnblogs.mapper;
import cc.bnblogs.pojo.Friends;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
public interface FriendsMapper extends JpaRepository<Friends,Integer> {
}

@ -0,0 +1,30 @@
package cc.bnblogs.pojo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
@Data
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "blog_friends")
public class Friends {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
// 友链标题
private String title;
// 友链地址
private String link;
}

@ -0,0 +1,62 @@
package cc.bnblogs.service;
import cc.bnblogs.mapper.FriendsMapper;
import cc.bnblogs.pojo.Friends;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
@Service
public class FriendsService {
private final FriendsMapper friendsMapper;
public FriendsService(FriendsMapper friendsMapper) {
this.friendsMapper = friendsMapper;
}
/**
* 获取所有友链
* @return 友链列表
*/
public List<Friends> list() {
return friendsMapper.findAll();
}
/**
* 获取友链总数
* @return 友链总数
*/
public long count() {
return friendsMapper.count();
}
/**
* 根据id查询友链对象
* @param id 友链id
* @return id对应的友链对象
*/
public Friends detail(Integer id) {
return friendsMapper.findById(id).orElse(null);
}
/**
* 保存友链
* @param friends 带保存的友链对象
*/
public void save(Friends friends) {
friendsMapper.save(friends);
}
/**
* 根据id删除友链对象
* @param id 待删除友链的id
*/
public void delete(Integer id) {
friendsMapper.deleteById(id);
}
}

@ -0,0 +1,167 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head th:replace="admin/common::header(~{::title},~{},~{})">
<title>##{name-zh}##管理</title>
</head>
<body>
<th:block th:include="admin/common::nav('##{name-en}##')"></th:block>
<div class="container lw-main lw-banner">
<div class="btn-group" role="group" style="margin-bottom: 20px" aria-label="...">
<button type="button" id="create-##{name-en}##-btn" class="btn btn-default"><i class="fa fa-plus"></i>
新增</button>
</div>
<table id="data-table"></table>
</div>
<!-- 编辑窗口 -->
<div class="modal fade" id="save-window" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form id="data-form">
<input type="hidden" name="##{id}##">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="window-title">Modal Title</h4>
</div>
<div class="modal-body">
##{each}##
<div class="form-group">
<label>##{zname}##</label>
<input type="text" name="##{ename}##" class="form-control"
placeholder="请输入##{zname}##...">
</div>
##{endeach}##
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="submit" class="btn btn-primary">保存</button>
</div>
</form>
</div>
</div>
</div>
<th:block th:include="admin/common::footer"></th:block>
<script>
$(function () {
// 显示表单所有##{name-zh}##数据
$('#data-table').bootstrapTable({
url: "/admin/##{name-en}##/",
columns: [
{
title: "序号",
width: 50,
align: 'center',
// 生成自增的序号
formatter: function (value, row, index) {
return index + 1
}
} ,
##{each}##
{
title: "##{zname}##",
field: "##{ename}##",
},
##{endeach}##
{
field: "##{id}##",
title: '操作',
align: "center",
width: 180,
formatter: function (value) {
return `<button type="button" data-id="${value}" class="btn btn-info btn-sm ##{name-en}##-edit-btn"><i class="fa fa-edit"></i> 编辑</button>
<button type="button" data-id="${value}" class="btn btn-danger btn-sm ##{name-en}##-delete-btn"><i class="fa fa-trash"></i> 删除</button>`
}
}
],
responseHandler: function (res) {
return res.data
}
})
// 编辑对应##{name-zh}##
$("#data-table").on("click", ".##{name-en}##-edit-btn", function () {
let id = $(this).data("id")
$.ajax({
url: "/admin/##{name-en}##/" + id,
method: 'GET',
dataType: 'json',
success: res => {
if (res.code === 200) {
$("#window-title").text('编辑##{name-zh}##')
$("#data-form").initForm(res.data)
$('#save-window').modal('show')
}
else {
layer.msg(res.message, { icon: 2 })
}
}
})
})
// 新增##{name-zh}##
$("#create-##{name-en}##-btn").on("click", function () {
$("#window-title").text('新增##{name-zh}##')
// 清空之前表单的数据
$("#data-form").initForm({##{id}##: "",##{each}## ##{ename}##: "",##{endeach}##})
$('#save-window').modal('show')
})
// 删除##{name-zh}##
$("#data-table").on("click", ".##{name-en}##-delete-btn", function () {
let id = $(this).data('id')
let idx = layer.confirm('是否要删除该数据?', {
btn: ['确认', '取消'] //按钮
}, function () { // 点击确认后删除
$.ajax({
url: '/admin/##{name-en}##/' + id,
method: 'delete',
dataType: 'json',
success: res => {
if (res.code === 200) {
layer.msg("删除成功", { icon: 1, time: 700 })
$('#data-table').bootstrapTable('refresh', { silent: true })
} else {
layer.msg(res.message, { icon: 2 })
}
}
})
layer.close(idx); // 关闭提示框
})
})
// 更新##{name-zh}##数据
$("#data-form").on("submit", function () {
let data = $(this).serialize();
$.ajax({
url: "/admin/##{name-en}##/",
method: "POST",
data: data,
dataType: "json",
success: res => {
if (res.code === 200) {
// 显示成功信息
layer.msg("保存成功", { icon: 1, time: 600 }, function () {
// 关闭模态框
$('#save-window').modal('hide')
// 重载表格数据
$('#data-table').bootstrapTable('refresh', { silent: true })
})
}
else {
layer.msg(res.message, { icon: 2 })
}
}
})
return false; // 阻止表单的提交行为
})
})
</script>
</body>
</html>

@ -0,0 +1,70 @@
package cc.bnblogs.controller;
import cc.bnblogs.common.Result;
import cc.bnblogs.enums.ResultEnum;
import cc.bnblogs.pojo.##{name-en-up}##;
import cc.bnblogs.service.##{name-en-up}##Service;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
@RestController
@RequestMapping("/admin/##{name-en}##")
public class ##{name-en-up}##Controller {
private final ##{name-en-up}##Service ##{name-en}##Service;
public ##{name-en-up}##Controller(##{name-en-up}##Service ##{name-en}##Service) {
this.##{name-en}##Service = ##{name-en}##Service;
}
/**
* 请求所有##{name-zh}##的接口
* @return 返回所有##{name-zh}##
*/
@GetMapping("/")
public Result<List<##{name-en-up}##>> list() {
return Result.success(##{name-en}##Service.list());
}
/**
* 请求##{name-zh}##详情的接口
* @param id ##{name-zh}##id
* @return 返回##{name-zh}##的详情
*/
@GetMapping("/{id}")
public Result<##{name-en-up}##> detail(@PathVariable Integer id) {
##{name-en-up}## ##{name-en}## = ##{name-en}##Service.detail(id);
if (Objects.isNull(##{name-en}##)) {
return Result.error(ResultEnum.RESULT_NOT_FOUND);
} else {
return Result.success(##{name-en}##);
}
}
/**
* 保存修改后的##{name-zh}##数据
* @param ##{name-en}## 修改的##{name-zh}##
* @return 修改结果
*/
@PostMapping("/")
public Result<String> save(##{name-en-up}## ##{name-en}##) {
##{name-en}##Service.save(##{name-en}##);
return Result.success();
}
/**
* 删除一个##{name-zh}##
* @param id 删除的##{name-zh}##id
* @return 删除结果
*/
@DeleteMapping("/{id}")
public Result<String> delete(@PathVariable Integer id) {
##{name-en}##Service.delete(id);
return Result.success();
}
}

@ -0,0 +1,11 @@
package cc.bnblogs.mapper;
import cc.bnblogs.pojo.##{name-en-up}##;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
public interface ##{name-en-up}##Mapper extends JpaRepository<##{name-en-up}##,Integer> {
}

@ -0,0 +1,62 @@
package cc.bnblogs.service;
import cc.bnblogs.mapper.##{name-en-up}##Mapper;
import cc.bnblogs.pojo.##{name-en-up}##;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/17
*/
@Service
public class ##{name-en-up}##Service {
private final ##{name-en-up}##Mapper ##{name-en}##Mapper;
public ##{name-en-up}##Service(##{name-en-up}##Mapper ##{name-en}##Mapper) {
this.##{name-en}##Mapper = ##{name-en}##Mapper;
}
/**
* 获取所有##{name-zh}##
* @return ##{name-zh}##列表
*/
public List<##{name-en-up}##> list() {
return ##{name-en}##Mapper.findAll();
}
/**
* 获取##{name-zh}##总数
* @return ##{name-zh}##总数
*/
public long count() {
return ##{name-en}##Mapper.count();
}
/**
* 根据id查询##{name-zh}##对象
* @param id ##{name-zh}##id
* @return id对应的##{name-zh}##对象
*/
public ##{name-en-up}## detail(Integer id) {
return ##{name-en}##Mapper.findById(id).orElse(null);
}
/**
* 保存##{name-zh}##
* @param ##{name-en}## 带保存的##{name-zh}##对象
*/
public void save(##{name-en-up}## ##{name-en}##) {
##{name-en}##Mapper.save(##{name-en}##);
}
/**
* 根据id删除##{name-zh}##对象
* @param id 待删除##{name-zh}##的id
*/
public void delete(Integer id) {
##{name-en}##Mapper.deleteById(id);
}
}

@ -42,7 +42,7 @@
<li th:class="${active eq 'category'} ? 'active': ''"><a th:href="@{/admin/category.html}">分类管理</a></li> <li th:class="${active eq 'category'} ? 'active': ''"><a th:href="@{/admin/category.html}">分类管理</a></li>
<li th:class="${active eq 'views'} ? 'active': ''"><a href="#">轮播图管理</a></li> <li th:class="${active eq 'views'} ? 'active': ''"><a href="#">轮播图管理</a></li>
<li><a href="#">页面管理</a></li> <li><a href="#">页面管理</a></li>
<li><a href="#">友链管理</a></li> <li th:class="${active eq 'friends'} ? 'active': ''"><a th:href="@{/admin/friends.html}">友链管理</a></li>
<li><a href="#">评论管理</a></li> <li><a href="#">评论管理</a></li>
</ul> </ul>
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">

@ -0,0 +1,177 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head th:replace="admin/common::header(~{::title},~{},~{})">
<title>友链管理</title>
</head>
<body>
<th:block th:include="admin/common::nav('friends')"></th:block>
<div class="container lw-main lw-banner">
<div class="btn-group" role="group" style="margin-bottom: 20px" aria-label="...">
<button type="button" id="create-friends-btn" class="btn btn-default"><i class="fa fa-plus"></i>
新增
</button>
</div>
<table id="data-table"></table>
</div>
<!-- 编辑窗口 -->
<div class="modal fade" id="save-window" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form id="data-form">
<input type="hidden" name="id">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="window-title">Modal Title</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>友链标题</label>
<input type="text" name="title" class="form-control"
placeholder="请输入友链标题...">
</div>
<div class="form-group">
<label>友链地址</label>
<input type="text" name="link" class="form-control"
placeholder="请输入友链地址...">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="submit" class="btn btn-primary">保存</button>
</div>
</form>
</div>
</div>
</div>
<th:block th:include="admin/common::footer"></th:block>
<script>
$(function () {
// 显示表单所有友链数据
$('#data-table').bootstrapTable({
url: "/admin/friends/",
columns: [
{
title: "序号",
width: 50,
align: 'center',
// 生成自增的序号
formatter: function (value, row, index) {
return index + 1
}
},
{
title: "友链标题",
field: "title",
},
{
title: "友链地址",
field: "link",
},
{
field: "id",
title: '操作',
align: "center",
width: 180,
formatter: function (value) {
return `<button type="button" data-id="${value}" class="btn btn-info btn-sm friends-edit-btn"><i class="fa fa-edit"></i> 编辑</button>
<button type="button" data-id="${value}" class="btn btn-danger btn-sm friends-delete-btn"><i class="fa fa-trash"></i> 删除</button>`
}
}
],
responseHandler: function (res) {
return res.data
}
})
// 编辑对应友链
$("#data-table").on("click", ".friends-edit-btn", function () {
let id = $(this).data("id")
$.ajax({
url: "/admin/friends/" + id,
method: 'GET',
dataType: 'json',
success: res => {
if (res.code === 200) {
$("#window-title").text('编辑友链')
$("#data-form").initForm(res.data)
$('#save-window').modal('show')
} else {
layer.msg(res.message, {icon: 2})
}
}
})
})
// 新增友链
$("#create-friends-btn").on("click", function () {
$("#window-title").text('新增友链')
// 清空之前表单的数据
$("#data-form").initForm({id: "", title: "", link: "",})
$('#save-window').modal('show')
})
// 删除友链
$("#data-table").on("click", ".friends-delete-btn", function () {
let id = $(this).data('id')
let idx = layer.confirm('是否要删除该数据?', {
btn: ['确认', '取消'] //按钮
}, function () { // 点击确认后删除
$.ajax({
url: '/admin/friends/' + id,
method: 'delete',
dataType: 'json',
success: res => {
if (res.code === 200) {
layer.msg("删除成功", {icon: 1, time: 700})
$('#data-table').bootstrapTable('refresh', {silent: true})
} else {
layer.msg(res.message, {icon: 2})
}
}
})
layer.close(idx); // 关闭提示框
})
})
// 更新友链数据
$("#data-form").on("submit", function () {
let data = $(this).serialize();
$.ajax({
url: "/admin/friends/",
method: "POST",
data: data,
dataType: "json",
success: res => {
if (res.code === 200) {
// 显示成功信息
layer.msg("保存成功", {icon: 1, time: 600}, function () {
// 关闭模态框
$('#save-window').modal('hide')
// 重载表格数据
$('#data-table').bootstrapTable('refresh', {silent: true})
})
} else {
layer.msg(res.message, {icon: 2})
}
}
})
return false; // 阻止表单的提交行为
})
})
</script>
</body>
</html>
Loading…
Cancel
Save