Springboot+Vue實(shí)現(xiàn)發(fā)表文章功能
點(diǎn)擊上方[全棧開(kāi)發(fā)者社區(qū)]→右上角[...]→[設(shè)為星標(biāo)?]

效果圖
前端編輯頁(yè)面

文章列表頁(yè)面
文章詳情頁(yè)面

環(huán)境介紹
JDK:1.8
數(shù)據(jù)庫(kù):Mysql5.6
前端:Vue
后端:SpringBoot
核心代碼介紹
AtricleCtrle.class
public class ArticleCtrler{??private?ArticleService?articleService;public Object addarticle( Article vo){try{??????articleService.insert(vo);?return Result.success(null);}catch (Exception e){e.printStackTrace();}return Result.error(CodeMsg.SERVER_ERROR);??}public Object loadPage( Article_Condit vo){try{??????PageInfo?loadPage?=?articleService.loadPage(vo); return Result.success(loadPage);}catch (Exception e){e.printStackTrace();}return Result.error(CodeMsg.SERVER_ERROR);}}
ArticleService.interface
public interface ArticleService{??void?insert(Article?vo);PageInfoloadPage(Article_Condit vo) ;}
ArticleServiceImpl.class
public class ArticleServiceImpl implements ArticleService{private ArticleDao articleDao;public void insert(Article vo){vo.setCreatedatetime(new Timestamp(new Date().getTime()));vo.setCreateuserid("system");articleDao.save(vo);??}public PageInfoloadPage(Article_Condit vo) {????PageHelper.startPage(vo.getPageIndex()==null?0:vo.getPageIndex(),?vo.getPageSize()==null?0:vo.getPageSize());????List?findByCondit?=?articleDao.findByCondit(vo); return new PageInfo(findByCondit); ??}}
ArticleDao.class
public interface ArticleDao{??void?save(Article?vo);ListfindByCondit(Article_Condit vo) ;}
Helloworld.vue
(value="發(fā)布文章", description="發(fā)布文章")public class Article implements Serializable{??private?static?final?long?serialVersionUID?=?1L;(value = "文章編號(hào)",hidden = true)??private?Long?id;(value = "文章標(biāo)題",required = true)(message = "標(biāo)題不能為空")??private?String?title;(value = "文章描述",required = true)(message = "文章描述不能為空")??private?String?description;(value = "文章內(nèi)容",required = true)(message = "文章內(nèi)容不能為空")??private?String?content;(value = "文章類(lèi)型",required = false)??private?String?articletype;(value = "創(chuàng)建時(shí)間",hidden = true)(pattern="yyyy-MM-dd HH:mm", timezone="GMT+8")??private?Timestamp?createdatetime;(value = "創(chuàng)建人",hidden = true)??private?String?createuserid;public Long getId() {return id;??}public void setId(Long id) {this.id = id;??}public String getTitle() {return title;??}public void setTitle(String title) {this.title = title;??}public String getDescription() {return description;??}public void setDescription(String description) {this.description = description;??}public String getContent() {return content;??}public void setContent(String content) {this.content = content;??}public Timestamp getCreatedatetime() {return createdatetime;??}public void setCreatedatetime(Timestamp createdatetime) {this.createdatetime = createdatetime;??}public String getCreateuserid() {return createuserid;??}public void setCreateuserid(String createuserid) {this.createuserid = createuserid;??}public String getArticletype() {return articletype;??}public void setArticletype(String articletype) {this.articletype = articletype;??}public String toString() {return "Article [id=" + id + ", title=" + title + ", description=" + description + ", content=" + content+ ", articletype=" + articletype + ", createdatetime=" + createdatetime + ", createuserid="+ createuserid + "]";??}}
<mapper namespace="com.yxyz.dao.ArticleDao"><insert id="save" parameterType="com.yxyz.vo.Article">insertinto t_article(title,description,content,articletype,createdatetime,createuserid)values(#{title,jdbcType=VARCHAR},#{description,jdbcType=VARCHAR},#{content,jdbcType=LONGVARCHAR},#{articletype,jdbcType=VARCHAR},#{createdatetime,jdbcType=TIMESTAMP},#{createuserid,jdbcType=VARCHAR})insert><select id="findByCondit" parameterType="com.yxyz.condit.Article_Condit" resultType="com.yxyz.vo.Article">select t1.* from t_article t1 where 1=1<if test="articletype != null and articletype !=''">and t1.articletype like concat('%',#{articletype},'%')if><if test="createuserid != null and createuserid !=''">and t1.createuserid = #{createuserid}if>select>mapper>
main.js
import Vue from 'vue'import App from './App.vue'import router from './router'import ElementUI from 'element-ui'import 'element-ui/lib/theme-chalk/index.css'Vue.use(ElementUI)Vue.config.productionTip = falsenew Vue({router,render: (h) => h(App),}).$mount('#app')
About.vue
<template><div class><h3>編輯頁(yè)面h3><el-form ref="form" :model="form" label-width="80px"><el-form-item label="標(biāo)題"><el-input v-model="form.title">el-input>el-form-item><el-form-item label="描述"><el-input v-model="form.description">el-input>el-form-item><el-form-item label="正文"><quill-editorref="myQuillEditor"class="editor"v-model="form.content":options="editorOption"@blur="onEditorBlur($event)"@focus="onEditorFocus($event)"@ready="onEditorReady($event)"/>el-form-item><el-button class="btn" block type="primary" @click="submit">提交el-button>el-form>div>template><script>import 'quill/dist/quill.core.css'import 'quill/dist/quill.snow.css'import 'quill/dist/quill.bubble.css'import { quillEditor } from 'vue-quill-editor'import axios from 'axios'export default {components: { quillEditor },props: {},data() {return {form: {title: '',description: '',content: '',},editorOption: {modules: {toolbar: [['bold', 'italic', 'underline', 'strike'], //加粗,斜體,下劃線(xiàn),刪除線(xiàn)['blockquote', 'code-block'], //引用,代碼塊[{ header: 1 }, { header: 2 }], // 標(biāo)題,鍵值對(duì)的形式;1、2表示字體大小[{ list: 'ordered' }, { list: 'bullet' }], //列表[{ script: 'sub' }, { script: 'super' }], // 上下標(biāo)[{ indent: '-1' }, { indent: '+1' }], // 縮進(jìn)[{ direction: 'rtl' }], // 文本方向[{ size: ['small', false, 'large', 'huge'] }], // 字體大小[{ header: [1, 2, 3, 4, 5, 6, false] }], //幾級(jí)標(biāo)題[{ color: [] }, { background: [] }], // 字體顏色,字體背景顏色[{ font: [] }], //字體[{ align: [] }], //對(duì)齊方式['clean'], //清除字體樣式// ['image', 'video'], //上傳圖片、上傳視頻],},theme: 'snow',},}},computed: {},created() {},mounted() {},watch: {},methods: {onEditorBlur(quill) {console.log('editor blur!', quill)},onEditorFocus(quill) {console.log('editor focus!', quill)},onEditorReady(quill) {console.log('editor ready!', quill)},submit() {if (!this.form.title) {this.$message('請(qǐng)輸入標(biāo)題')}if (!this.form.description) {this.$message('請(qǐng)輸入描述')}if (!this.form.content) {this.$message('請(qǐng)輸入正文')}let formData = new FormData()formData.append('title', this.form.title)formData.append('description', this.form.description)formData.append('content', this.form.content)// 發(fā)送 POST 請(qǐng)求axios({method: 'post',url: 'http://139.159.147.237:8080/yxyz/article/addarticle',data: formData,headers: {'Content-Type': 'application/x-www-form-urlencoded',},}).then(function(response) {if (res.code === 0) {this.$message('提交成功')}// this.form = {// title: '',// description: '',// content: '',// }this.$router.goBack()}).catch(function(error) {console.log(error)})},},}script><style scoped lang="less">.editor {height: 500px;}.btn {margin-top: 100px;}style>
Detail.vue
<template><div class=""><h2>文章詳情h2><el-card><div class="title">{{ detail.title }}div><div class="des">{{ detail.description }}div><div class="con" v-html="detail.content">div><div class="time">{{ detail.createdatetime }}div>el-card>div>template><script>export default {components: {},props: {},data() {return {detail: {},}},computed: {},created() {console.log(this.$route)let item = this.$route.params.itemthis.detail = item},mounted() {},watch: {},methods: {},}script><style scoped lang="less">.title {font-weight: bold;font-size: 16px;text-align: left;margin-bottom: 10px;}.des {font-size: 14px;text-align: left;margin-bottom: 10px;}.con {font-size: 14px;text-align: left;margin-bottom: 10px;}.time {font-size: 14px;text-align: left;}style>
Home.vue
<template><div class=""><h2>文章詳情h2><el-card><div class="title">{{ detail.title }}div><div class="des">{{ detail.description }}div><div class="con" v-html="detail.content">div><div class="time">{{ detail.createdatetime }}div>el-card>div>template><script>export default {components: {},props: {},data() {return {detail: {},}},computed: {},created() {console.log(this.$route)let item = this.$route.params.itemthis.detail = item},mounted() {},watch: {},methods: {},}script><style scoped lang="less">.title {font-weight: bold;font-size: 16px;text-align: left;margin-bottom: 10px;}.des {font-size: 14px;text-align: left;margin-bottom: 10px;}.con {font-size: 14px;text-align: left;margin-bottom: 10px;}.time {font-size: 14px;text-align: left;}style>
application.yml
spring:profiles:active: dev
application-dev.yml
server:port: 8080servlet:: /yxyzspring:datasource:name: yxyzurl: jdbc:mysql://localhost/test?serverTimezone=GMT%2b8&characterEncoding=UTF8username: rootpassword: 123# 使用druid數(shù)據(jù)源type: com.alibaba.druid.pool.DruidDataSource: com.mysql.cj.jdbc.Driverfilters: statmaxActive: 20initialSize: 1maxWait: 60000minIdle: 1timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: select 'x'testWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truemaxOpenPreparedStatements: 20## 該配置節(jié)點(diǎn)為獨(dú)立的節(jié)點(diǎn)mybatis:: classpath:mapping/*Mapper.xml #注意:一定要對(duì)應(yīng)mapper映射xml文件的所在路徑: com.yxyz.vo # 注意:對(duì)應(yīng)實(shí)體類(lèi)的路徑#打印sql最終填充的參數(shù)值: org.apache.ibatis.logging.stdout.StdOutImpl
覺(jué)得本文對(duì)你有幫助?請(qǐng)分享給更多人
關(guān)注「全棧開(kāi)發(fā)者社區(qū)」加星標(biāo),提升全棧技能
本公眾號(hào)會(huì)不定期給大家發(fā)福利,包括送書(shū)、學(xué)習(xí)資源等,敬請(qǐng)期待吧!
如果感覺(jué)推送內(nèi)容不錯(cuò),不妨右下角點(diǎn)個(gè)在看轉(zhuǎn)發(fā)朋友圈或收藏,感謝支持。
好文章,留言、點(diǎn)贊、在看和分享一條龍吧??
評(píng)論
圖片
表情
