• 论文管理系统(增删查改功能的实现)


    目录

    一、后端

    1.1实体类

    1.2paperMapper类

    1.3 PaperMapper类 

    1.4Service层 

    1.5 Controller层 

    二、前端

    源代码

     


    我们已经实现列表数据了,接下来我们将实现增删查改功能,新增和修改还能够回显

    一、后端

    1.1实体类

    实体类还是我们上一版的列表功能的实现的paper实体类

     

    代码附在上一版中 

    1.2paperMapper类

    1. "1.0" encoding="UTF-8"?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="com.woniu.paper.mapper.PaperMapper">
    4. <resultMap id="paperResult" type="Paper">
    5. <result column="id" property="id"/>
    6. <result column="title" property="title"/>
    7. <result column="type_id" property="typeId"/>
    8. <result column="paper_summary" property="paperSummary"/>
    9. <result column="paper_path" property="paperPath"/>
    10. <result column="created_by" property="createdBy"/>
    11. <result column="creation_data" property="creationData"/>
    12. <result column="modify_by" property="modifyBy"/>
    13. <result column="modify_data" property="modifyData"/>
    14. <result column="file_name" property="fileName"/>
    15. <result column="type_name" property="typeName"/>
    16. resultMap>
    17. <select id="selectPaperListByCondition" resultMap="paperResult">
    18. select p.*, t.type_name from t_paper as p LEFT JOIN t_type as t on
    19. p.type_id=t.id
    20. <where>
    21. <if test="title!=null and title!='null'">
    22. and title like '%${title}%'
    23. if>
    24. <if test="typeName!=null and typeName!='null'">
    25. and type_name=#{typeName}
    26. if>
    27. where>
    28. limit #{start},#{size}
    29. select>
    30. <select id="count" resultType="int">
    31. select count(t.type_name) from t_paper as p LEFT JOIN t_type as t on
    32. p.type_id=t.id
    33. <where>
    34. <if test="title!=null and title!=''">
    35. and title like '%${title}%'
    36. if>
    37. <if test="typeName!=null and typeName!=''">
    38. and type_name=#{typeName}
    39. if>
    40. where>
    41. select>
    42. <select id="getPaperByid" resultMap="paperResult">
    43. select p.*, t.type_name from t_paper as p left join t_type as t on p.type_id=t.id where p.id=#{id}
    44. select>
    45. <delete id="deletePaperById">
    46. delete from t_paper where id=#{id}
    47. delete>
    48. <insert id="insertPaper">
    49. insert into t_paper (title, type_id,created_by) values ( #{title}, #{typeId}, #{createdBy})
    50. insert>
    51. <update id="updatePaperById"> update t_paper set title=#{title}, type_id=#{typeId}, created_by=#{createdBy} where id=#{id} update>
    52. mapper>

    1.3 PaperMapper类 

    1. package com.woniu.paper.mapper;
    2. import com.woniu.paper.entity.HttpResult;
    3. import com.woniu.paper.entity.Paper;
    4. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    5. import java.net.HttpCookie;
    6. import java.util.List;
    7. /**
    8. *

    9. * Mapper 接口
    10. *

    11. *
    12. * @author zhang
    13. * @since 2022-11-18
    14. */
    15. public interface PaperMapper extends BaseMapper {
    16. List selectPaperListByCondition(String title, String typeName, int start, int size);
    17. int count(String title, String typeName);//查询总数,用于列表分页
    18. //根据id获取论文信息的接口
    19. Paper getPaperByid(Long id);
    20. //根据id删除论文的接口
    21. int deletePaperById(Long id);
    22. //添加论文
    23. int insertPaper(Paper paper);
    24. //根据id修改论文
    25. int updatePaperById(Paper paper);
    26. }

    1.4Service层 


    接口 

    1. package com.woniu.paper.service;
    2. import com.woniu.paper.entity.HttpResult;
    3. import com.woniu.paper.entity.Paper;
    4. import com.baomidou.mybatisplus.extension.service.IService;
    5. /**
    6. *

    7. * 服务类
    8. *

    9. *
    10. * @author zhang
    11. * @since 2022-11-18
    12. */
    13. public interface IPaperService {
    14. public HttpResult selectPaperListByCondition(String title, String typeName, int pageIndex, int pageSize);
    15. // public HttpResult insertPaper(String title,Long typeId,String paperSummary,String paperPath,String createdBy,String modifyBy,String fileName);
    16. HttpResult getPaperByid(Long id);
    17. HttpResult deletePaperById(Long id);
    18. HttpResult insertPaper(Paper paper);
    19. HttpResult updatePaperById(Paper paper);
    20. }

     


    实现类

     

    1. package com.woniu.paper.service.impl;
    2. import com.woniu.paper.entity.HttpResult;
    3. import com.woniu.paper.entity.Paper;
    4. import com.woniu.paper.mapper.PaperMapper;
    5. import com.woniu.paper.mapper.UserMapper;
    6. import com.woniu.paper.service.IPaperService;
    7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.stereotype.Service;
    10. import java.util.List;
    11. /**
    12. *

    13. * 服务实现类
    14. *

    15. *
    16. * @author zhang
    17. * @since 2022-11-18
    18. */
    19. @Service
    20. public class PaperServiceImpl implements IPaperService {
    21. @Autowired(required = false)
    22. private PaperMapper paperMapper;//实例化DAO对象
    23. @Override
    24. public HttpResult selectPaperListByCondition(String title, String typeName, int pageIndex, int pageSize) {
    25. List papers = paperMapper.selectPaperListByCondition(title, typeName, (pageIndex - 1) * pageSize, pageSize);
    26. int count = paperMapper.count(title, typeName);
    27. HttpResult result=null;
    28. if (papers!=null&&papers.size()>0){
    29. result= new HttpResult(papers,count,200,null);
    30. }else{
    31. result= new HttpResult(null,0,500,"没有更多数据");
    32. }
    33. return result;
    34. }
    35. @Override
    36. public HttpResult getPaperByid(Long id) {
    37. Paper paper = paperMapper.getPaperByid(id);
    38. HttpResult result=null;
    39. if (paper!=null){
    40. result= new HttpResult(paper,0,200,null);
    41. }else{
    42. result= new HttpResult(null,0,500,"没有更多数据");
    43. }
    44. return result;
    45. }
    46. @Override
    47. public HttpResult deletePaperById(Long id) {
    48. int count = paperMapper.deletePaperById(id);
    49. HttpResult result=null;
    50. if (count>0){
    51. result=new HttpResult(null,0,200,"删除论文成功");
    52. }else{
    53. result=new HttpResult(null,0,500,"删除论文失败");
    54. }
    55. return result;
    56. }
    57. @Override
    58. public HttpResult insertPaper(Paper paper) {
    59. int count = paperMapper.insertPaper(paper);
    60. HttpResult result=null;
    61. if (count>0){
    62. result=new HttpResult(null,0,200,"添加论文成功");
    63. }else{
    64. result=new HttpResult(null,0,500,"添加论文失败");
    65. }
    66. return result;
    67. }
    68. @Override
    69. public HttpResult updatePaperById(Paper paper) {
    70. int count = paperMapper.updatePaperById(paper);
    71. HttpResult result = null;
    72. if(count > 0){
    73. result = new HttpResult(null, 0, 200, "修改论文成功");
    74. } else {
    75. result = new HttpResult(null, 0, 500, "修改论文失败");
    76. }
    77. return result;
    78. }
    79. }

    1.5 Controller层 

     

     

    1. package com.woniu.paper.controller;
    2. import com.woniu.paper.entity.HttpResult;
    3. import com.woniu.paper.entity.Paper;
    4. import com.woniu.paper.service.IPaperService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.boot.origin.Origin;
    7. import org.springframework.web.bind.annotation.CrossOrigin;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import org.springframework.stereotype.Controller;
    10. import org.springframework.web.bind.annotation.RestController;
    11. /**
    12. *

    13. * 前端控制器
    14. *

    15. *
    16. * @author zhang
    17. * @since 2022-11-18
    18. */
    19. @RestController
    20. @RequestMapping("/paper/paper")
    21. public class PaperController {
    22. @Autowired
    23. private IPaperService paperService;//实例化Service对象
    24. /**
    25. * 根据分页查询论文列表
    26. * @return
    27. */
    28. @RequestMapping("/list")
    29. @CrossOrigin(origins = "*")
    30. public HttpResult selectPaperListByCondition(String title,String typeName,int pageIndex,int pageSize) {
    31. return paperService.selectPaperListByCondition(title,typeName,pageIndex,pageSize);
    32. }
    33. /**
    34. * 根据id查询论文信息
    35. */
    36. @RequestMapping("/info")
    37. @CrossOrigin(origins = "*")
    38. public HttpResult getPaperByid(Long id){
    39. return paperService.getPaperByid(id);
    40. }
    41. /**
    42. * 根据id删除论文
    43. * @param id
    44. * @return
    45. */
    46. @RequestMapping("/delete")
    47. @CrossOrigin(origins = "*")
    48. public HttpResult deletePaperById(Long id){
    49. return paperService.deletePaperById(id);
    50. }
    51. /**
    52. * 增加论文数量
    53. */
    54. @RequestMapping("/add")
    55. @CrossOrigin(origins = "*")
    56. public HttpResult insertPaper(String title,String createdBy,Long typeId){
    57. Paper paper = new Paper();
    58. paper.setTitle(title);
    59. paper.setCreatedBy(createdBy);
    60. paper.setTypeId(typeId);
    61. return paperService.insertPaper(paper);
    62. }
    63. @RequestMapping("/update")
    64. @CrossOrigin(origins = "*")
    65. public HttpResult updatePaperById(Long id,String title,String createdBy,Long typeId){
    66. Paper paper = new Paper();
    67. paper.setId(id);
    68. paper.setTitle(title);
    69. paper.setCreatedBy(createdBy);
    70. paper.setTypeId(typeId);
    71. return paperService.updatePaperById(paper);
    72. }
    73. }

     这就是我们增删改查的后端部分,接下来,我们将来写前端部分

    二、前端

    源代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. <link href="assets/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    7. <script src="assets/jquery-3.5.1.min.js">script>
    8. <script src="assets/bootstrap-3.3.7-dist/js/bootstrap.min.js">script>
    9. <script src="assets/vue.min-v2.5.16.js">script>
    10. <script src="assets/vue-router.min-2.7.0.js">script>
    11. <script src="assets/axios.min.js">script>
    12. head>
    13. <body>
    14. <template id="template_insert_or_update">
    15. <div style="width: 500px; position: fixed; top: 50px; left: 50px; background-color: white; border: 1px solid black">
    16. <div class="row">
    17. <div class="col-md-8 col-md-offset-2">
    18. <label>论文主题label>
    19. <input type="text" class="form-control" v-model="adTitle">
    20. div>
    21. div>
    22. <div class="row">
    23. <div class="col-md-8 col-md-offset-2">
    24. <label>作者label>
    25. <input type="text" class="form-control" v-model="adCreateBy">
    26. div>
    27. div>
    28. <div class="row">
    29. <div class="col-md-8 col-md-offset-2">
    30. <label>论文类型label>
    31. <select v-model="adTypeId">
    32. <option value="1">软件option>
    33. <option value="2">财务option>
    34. select>
    35. div>
    36. div>
    37. <div class="row">
    38. <div class="col-md-8 col-md-offset-2">
    39. <button @click="saveOrUpdate">保存button>
    40. div>
    41. div>
    42. div>
    43. template>
    44. <div id="app">
    45. <router-view v-if="isRouterViewShow">router-view>
    46. <div style="margin-left: auto; margin-right: auto;">
    47. <div style="width: 1000px; height: 100px; border: 1px solid black; text-align: center;font-size: 18px; line-height: 100px;">
    48. 论文管理系统
    49. div>
    50. <div style="width: 200px; height: 700px; border: 1px solid black; text-align: center; font-size: 18px; line-height: 50px; float: left">
    51. <div>
    52. <button class="btn btn-link" @click="doUserManager">用户管理button>
    53. div>
    54. <div>
    55. <button class="btn btn-link" @click="doPaperManager">论文管理button>
    56. div>
    57. div>
    58. <div style="width: 800px; height: 700px; border: 1px solid black; float: left">
    59. <div v-if="isShow">
    60. 论文主题:
    61. <input type="text" v-model="title">
    62. 论文类型:
    63. <select v-model="typeName">
    64. <option>软件option>
    65. <option>财务option>
    66. select>
    67. <button @click="doQuery">查询button>
    68. <button @click="doAddPaper">添加论文button>
    69. <table class="table table-striped">
    70. <caption>论文列表caption>
    71. <thead>
    72. <tr>
    73. <th>论文主题th>
    74. <th>作者th>
    75. <th>论文类型th>
    76. <th>发表时间th>
    77. <th>修改时间th>
    78. <th>操作th>
    79. tr>
    80. thead>
    81. <tbody>
    82. <tr v-for="p in papers">
    83. <td>{{p.title}}td>
    84. <td>{{p.createdBy}}td>
    85. <td>{{p.typeName}}td>
    86. <td>{{p.creationData}}td>
    87. <td>{{p.modifyData}}td>
    88. <td>
    89. <button class="btn btn-link" @click="doUpdate(p.id)">修改button>
    90. <button class="btn btn-link" @click="doDelete(p.id)">删除button>
    91. td>
    92. tr>
    93. tbody>
    94. table>
    95. <ul class="pagination" v-for="p in pageNum">
    96. <li v-if="p == pageIndex" class="active">
    97. <a @click="doGo(p)">{{p}}a>
    98. li>
    99. <li v-else="p == pageIndex">
    100. <a @click="doGo(p)">{{p}}a>
    101. li>
    102. ul>
    103. div>
    104. div>
    105. div>
    106. div>
    107. <script>
    108. var _self = null; //用于等量代换vue对象的this
    109. //创建添加或删除页面的模板的实例对象
    110. var myTemplate = {
    111. template: '#template_insert_or_update',
    112. //要绑定的数据
    113. data() {
    114. return {
    115. adTitle: null, //主题,用于添加或修改
    116. adCreateBy: null, //作者,用于添加或修改
    117. adTypeId: null //类型的ID,用于添加或修改
    118. }
    119. },
    120. //要绑定的函数
    121. methods: {
    122. //点击新增页面的保存按钮执行
    123. saveOrUpdate() {
    124. if (_self.isUpdate) { //修改跳转来的
    125. //收集表单数据并调用后台修改论文的接口提交到后台
    126. var url = "http://localhost:8080/paper/paper/update?title=" + this.adTitle + "&createdBy=" + this.adCreateBy + "&typeId=" + this.adTypeId + "&uid=" + localStorage.getItem("uid") + "&id=" + _self.paperId;
    127. axios.get(url).then(response => {
    128. console.log(response.data);
    129. if (response.data.code == 200) { //修改成功
    130. //刷新列表
    131. var url = "http://localhost:8080/paper/paper/list?pageIndex=1&pageSize=" + _self.pageSize + "&uid=" + localStorage.getItem("uid");
    132. _self.pageIndex = 1;
    133. _self.requestPapers(url); //调用请求论文列表的函数发送请求
    134. } else { //修改失败
    135. alert(response.data.msg);
    136. }
    137. });
    138. } else { //添加按钮跳转来的
    139. //收集表单数据并调用后台添加论文的接口提交到后台
    140. var url = "http://localhost:8080/paper/paper/add?title=" + this.adTitle + "&createdBy=" + this.adCreateBy + "&typeId=" + this.adTypeId + "&uid=" + localStorage.getItem("uid");
    141. axios.get(url).then(response => {
    142. console.log(response.data);
    143. if (response.data.code == 200) { //添加成功
    144. //刷新列表
    145. var url = "http://localhost:8080/paper/paper/list?pageIndex=1&pageSize=" + _self.pageSize + "&uid=" + localStorage.getItem("uid");
    146. _self.pageIndex = 1;
    147. _self.requestPapers(url); //调用请求论文列表的函数发送请求
    148. } else { //添加失败
    149. alert(response.data.msg);
    150. }
    151. });
    152. }
    153. //关闭当前页面(让router-view的显示状态为false)
    154. _self.isRouterViewShow = false;
    155. }
    156. },
    157. created: function () {
    158. //判断是否要回显,当通过修改按钮跳转进来时,需要回显
    159. if (_self.isUpdate) { //说明是通过修改按钮跳转进来的
    160. //通过axios发送请求
    161. axios.get("http://localhost:8080/paper/paper/info?id=" + _self.paperId + "&uid=" + localStorage.getItem("uid")).then(response => {
    162. console.log(response.data.data);
    163. this.adTitle = response.data.data.title;
    164. this.adCreateBy = response.data.data.createdBy;
    165. this.adTypeId = response.data.data.typeId;
    166. });
    167. }
    168. }
    169. }
    170. //创建router实力,维护路由(点击不同链接,显示不同的模板)
    171. var rt = new VueRouter({
    172. routes: [
    173. {path: '/add', component: myTemplate}
    174. ]
    175. });
    176. //创建vue实例对象
    177. new Vue({
    178. //将id=app的div的管理权交给vue
    179. el: '#app',
    180. router: rt, //将自定义的路由绑定到vue实例
    181. //需要绑定的数据
    182. data: {
    183. papers: null, //论文列表
    184. isShow: false, //控制列表是否显示
    185. isRouterViewShow: false, //控制router-view是否显示
    186. isUpdate: false, //是否是修改页面
    187. paperId: null, //论文的id,用于传参
    188. //用于条件查询
    189. title: null,
    190. typeName: null,
    191. //用于分页
    192. pageIndex: 1, //当前页面
    193. pageSize: 10, //每页显示的条数
    194. pageTotle: 0, //总条数
    195. pageNum: 0 //页数
    196. },
    197. //需要绑定的函数
    198. methods: {
    199. //请求论文列表
    200. requestPapers(url) {
    201. //通过axios发送请求get请求,then响应
    202. axios.get(url).then(respones => {
    203. console.log(respones.data);
    204. this.papers = respones.data.data; //给论文列表赋值
    205. this.pageTotle = respones.data.pageTotle; //给总条数赋值
    206. //Math.ceril函数=》小数取整,向上取整
    207. this.pageNum = Math.ceil(this.pageTotle / this.pageSize); //计算页数
    208. });
    209. },
    210. //分页器跳转
    211. doGo(p) {
    212. this.pageIndex = p;
    213. var url = "http://localhost:8080/paper/paper/list?pageIndex=" + p + "&pageSize=" + this.pageSize + "&uid=" + localStorage.getItem("uid");
    214. this.requestPapers(url); //调用请求论文列表的函数发送请求
    215. },
    216. //用户管理
    217. doUserManager() {
    218. this.isShow = false; //让列表不显示
    219. },
    220. //论文管理
    221. doPaperManager() {
    222. this.isShow = true; //让列表显示
    223. },
    224. //点击查询按钮执行
    225. doQuery() {
    226. var url = "http://localhost:8080/paper/paper/list?pageIndex=" + this.pageIndex + "&pageSize=" + this.pageSize + "&uid=" + localStorage.getItem("uid") + "&title=" + this.title + "&typeName=" + this.typeName;
    227. this.requestPapers(url); //调用请求论文列表的函数发送请求
    228. },
    229. //添加添加论文按钮时执行
    230. doAddPaper() {
    231. //设置是否为修改页面的变量状态
    232. this.isUpdate = false;
    233. //让router-view控件显示出来
    234. this.isRouterViewShow = true;
    235. //通过roter对象的push函数跳转指定的模板
    236. this.$router.push("/add");
    237. },
    238. //点击表格修改按钮时执行
    239. doUpdate(id) {
    240. //设置是否为修改页面的变量状态
    241. this.isUpdate = true;
    242. //让router-view控件显示出来
    243. this.isRouterViewShow = true;
    244. //通过roter对象的push函数跳转指定的模板
    245. this.$router.push("/add");
    246. this.paperId = id;
    247. },
    248. //点击表格修改按钮时执行
    249. doDelete(id) {
    250. //通过axios发送请求
    251. axios.get("http://localhost:8080/paper/paper/delete?id=" + id + "&uid=" + localStorage.getItem("uid")).then(response => {
    252. console.log(response.data);
    253. if (response.data.code == 200) { //删除成功
    254. var url = "http://localhost:8080/paper/paper/list?pageIndex=1&pageSize=" + this.pageSize + "&uid=" + localStorage.getItem("uid");
    255. this.requestPapers(url); //调用请求论文列表的函数发送请求
    256. } else { //删除失败
    257. alert(response.data.msg);
    258. }
    259. });
    260. }
    261. },
    262. //页面加载完成后执行
    263. created: function () {
    264. _self = this;
    265. //通过localStorage的getItem函数获取缓存的uid
    266. var url = "http://localhost:8080/paper/paper/list?pageIndex=" + this.pageIndex + "&pageSize=" + this.pageSize + "&uid=" + localStorage.getItem("uid");
    267. this.requestPapers(url); //调用请求论文列表的函数发送请求
    268. }
    269. });
    270. script>
    271. body>
    272. html>

     

     

     

     

     

     

  • 相关阅读:
    基于Linux上MySQL8.*版本的安装-参考官网
    【JUC】Java并发编程从挖坑到入土全解(3-线程池源码分析)
    数据仓库概念
    流程系统的设计与实现
    网络钓鱼技术
    番外篇 | 利用华为2023最新Gold-YOLO中的Gatherand-Distribute对特征融合模块进行改进
    SpringBoot集成Swagger的方法
    C#/VB.NET 将PDF转为PDF/X-1a:2001
    回归分析:多项式回归与广义线性回归,非线性问题的拟合
    「Dr. Bomkus 的试炼」——最详尽的通关资讯!
  • 原文地址:https://blog.csdn.net/magic_818/article/details/128010084