电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

SpringBoot+Vue實現數據添加功能

瀏覽:52日期:2023-03-21 10:00:04
一、添加代碼生成器

用來自動為數據庫映射類建立:mapper、service、controller

注:代碼生成器的寫法,參考官方文檔:https://mp.baomidou.com/

package com.hanmh.utils;import com.baomidou.mybatisplus.core.toolkit.StringPool;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.*;import com.baomidou.mybatisplus.generator.config.po.TableInfo;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import com.hanmh.pojo.BasePojo; import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; public class HanGenerator { public static void main(String[] args) { // 代碼生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); //這樣獲取到的是父項目的目錄 String projectPath = System.getProperty('user.dir'); String pojoProject = 'pojo' + '/src/main/java/com/hanmh/pojo'; String otherProject = 'admin'; //gc.setOutputDir(projectPath + '/src/main/java'); gc.setAuthor('hanmh'); gc.setOpen(false); // gc.setSwagger2(true); 實體屬性 Swagger2 注解 mpg.setGlobalConfig(gc); // 數據源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl('jdbc:mysql://39.105.231.52:3306/db?useUnicode=true&useSSL=false&characterEncoding=utf8'); // dsc.setSchemaName('public'); dsc.setDriverName('com.mysql.jdbc.Driver'); dsc.setUsername('root'); dsc.setPassword('123456'); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent('com.hanmh'); //設置父包 //自定義生成路徑 Map<String,String> pathInfo = new HashMap<String,String>(); pathInfo.put('entity_path', projectPath + '/' + pojoProject); //pojo位置 pathInfo.put('controller_path', projectPath + '/' + otherProject + '/src/main/java/com/hanmh/controller'); pathInfo.put('service_impl_path', projectPath + '/' + otherProject + '/src/main/java/com/hanmh/service/impl'); pathInfo.put('service_path', projectPath + '/' + otherProject + '/src/main/java/com/hanmh/service'); pathInfo.put('mapper_path', projectPath + '/' + otherProject + '/src/main/java/com/hanmh/mapper'); pathInfo.put('xml_path', projectPath + '/' + otherProject + '/src/main/resources/com/hanmh/mapper'); pc.setEntity('pojo'); //實體類 pc.setPathInfo(pathInfo); mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() {// to do nothing } }; // 如果模板引擎是 freemarker String templatePath = '/templates/mapper.xml.ftl'; // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); //生成時,繼承的父類 strategy.setSuperEntityClass(BasePojo.class); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // 公共父類 strategy.setSuperControllerClass('你自己的父類控制器,沒有就不用設置!'); // 寫于父類中的公共字段 strategy.setSuperEntityColumns('id'); strategy.setInclude('ums_admin'); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + '_'); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }}二、導入需要的jar包

前期需要導入的包有:mybatis-plus、mysql、duracloud(工具包)、pojo、spring-boot-starter-web

<dependency> <groupId>com.hanmh</groupId> <artifactId>pojo</artifactId></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId></dependency><dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency><dependency> <groupId>org.duracloud</groupId> <artifactId>common</artifactId></dependency>三、創建啟動類

啟動類必須創建在父包下面,注意在SpringBoot中,并不是不掃包,而是框架幫助完成了這件事,它會掃描啟動類所在包下的所有類及其子包中的類

package com.hanmh; import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication@MapperScan('com.hanmh.mapper')public class AdminRun { public static void main(String[] args) { SpringApplication.run(AdminRun.class); }}四、創建配置文件(application.yml)

server: port: 8080spring: application: name: admin datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://39.105.231.52:3306/db?useUnicode=true&useSSL=false&characterEncoding=utf8 username: root password: 123456 hikari: maximum-pool-size: 20 minimum-idle: 10 servlet: #文件傳輸配置 multipart: max-file-size: 5MB max-request-size: 10MB#運行的過程中輸出sql語句(日志信息)logging: level: com.hanmh.mapper: debug五、返回值統一定義1、ResultJson

package com.hanmh.pojo; import lombok.Data; @Datapublic class ResultJson { private Integer code; //200成功,500錯誤 private Object obj; private String message; public ResultJson(ResultCode resultCode, Object obj) { this.code = resultCode.getCode(); this.message = resultCode.getMessage(); this.obj = obj; } public ResultJson(ResultCode resultCode, Object obj, String message) { this.code = resultCode.getCode(); this.message = message; this.obj = obj; } public static ResultJson success(Object obj) { return new ResultJson(ResultCode.SUCCESS, obj); } public static ResultJson success(Object obj, String message) { return new ResultJson(ResultCode.SUCCESS, obj, message); } public static ResultJson error(Object obj) { return new ResultJson(ResultCode.ERROR, obj); } public static ResultJson error() { return new ResultJson(ResultCode.ERROR, null); } public static ResultJson error(String message) { return new ResultJson(ResultCode.ERROR, null, message); }}2、ResultCode

定義4種返回代號和返回信息,使用枚舉類進行實現

package com.hanmh.pojo; import lombok.Data;import lombok.Getter; @Getterpublic enum ResultCode { SUCCESS(200, '請求成功'), ERROR(500, '請求失敗'), NOAUTH(401, '用戶未登錄或者登錄超時'), //操作時 NOPREMISSION(403, '沒有此權限'); private Integer code; private String message; //枚舉類型的構造默認為私有 private ResultCode(Integer code, String message) { this.code = code; this.message = message; }}六、創建基礎pojo

在所有的數據庫表種,共有的字段是ID,現在將id獨立出來

1、導入 mybatis-plus-annotation包

為了使用該包內部的IdType等類內部提供的注解,以告訴BasePojo中某些字段在數據庫表中的存在與否

<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-annotation</artifactId> <version>3.0-RELEASE</version></dependency>2、BasePojo類

package com.hanmh.pojo; import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;import org.omg.CORBA.IDLType; @Datapublic class BasePojo { @TableId(type = IdType.AUTO) private Integer id; //做分頁操作需要的字段 @TableField(exist = false) private Integer pageNO; @TableField(exist = false) private Integer pageSize;}七、后端添加數據1、密碼加密

(1)需要使用安全包提供加密服務(security)

<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId></dependency>2、將加密類(BCryptPasswordEncoder)放入IOC容器

在SpringBoot環節,需要將某個類加入IOC容器,就需要在配置類中,配置@Bean節點

@Configurationpublic class AdminConfig { @Bean //將BCryptPasswordEncoder放進IOC容器 BCryptPasswordEncoder getPasswordEncoder() { return new BCryptPasswordEncoder(); }}

注:使用此方法對數據進行加密的原因:此加密方法相同明文密碼多次可以生成不同的密文,而MD5相同密碼則會生成相同的密文

3、后端添加數據簡單實現

@PostMapping('/add')ResultJson add(UmsAdmin umsAdmin) throws InterruptedException, IOException { //對密碼加密 umsAdmin.setPassword(passwordEncoder.encode(umsAdmin.getPassword())); //TimeUnit.SECONDS.sleep(2); return ResultJson.success(adminService.save(umsAdmin), '添加用戶成功');}八、前端頁面添加功能1、添加用戶(按鈕和彈窗)

<el-button>:elementUI按鈕標簽<el-button type='primary' plain @click='add'>添加用戶</el-button><!-- <el-dialog> 代表彈窗 :visible.sync表示顯示或隱藏--><!-- close-on-click-modal代表點擊對話框以外區域是否可以關閉 --><el-dialog :title='dialog.title' :visible.sync='dialog.show':close-on-click-modal='false'width='450px'> <AdminEdit :show.sync='dialog.show' v-if='dialog.show'></AdminEdit></el-dialog>

(1)添加用戶功能

add() { this.dialog.show = true this.dialog.title = '添加用戶'}

(2)添加內容彈窗

<template> <div> <el-form :model='forms' :rules='rules' ref='ruleForm' label-width='100px'> <el-form-item label='登錄名' prop='loginName'> <el-input v-model='forms.loginName' placeholder='請輸入登錄名'></el-input> </el-form-item> <el-form-item label='昵稱' prop='name'> <el-input v-model='forms.name' placeholder='請輸入昵稱'></el-input> </el-form-item> <el-form-item label='密碼' prop='password'> <el-input v-model='forms.password' placeholder='請輸入密碼' show-password></el-input> </el-form-item> <el-form-item label='郵箱' prop='email'> <el-input v-model='forms.email' placeholder='請輸入郵箱'></el-input> </el-form-item> <el-form-item label='手機號' prop='phone'> <el-input v-model='forms.phone' placeholder='請輸入手機號'></el-input> </el-form-item> <el-form-item label='頭像' prop='imgobj'> </el-form-item> <el-form-item> <el-button size='small' type='primary' plain @click='save'>保存</el-button> </el-form-item> </el-form> </div></template> <script> export default{ name: ’AdminEdit’, props:{ show:{ type: Boolean } }, data(){ return { forms: { loginName: ’’, name: ’’, password: ’’, email: ’’, phone: ’’, imgobj: ’這是一張圖片’ }, rules:{ loginName:[ {required: true, message: ’請輸入登錄名’, trigger: ’blur’}, {min : 1, max: 20, message: ’長度在1-20之間’, trigger: ’change’} ], name:[ {required: true, message: ’請輸入昵稱’, trigger: ’blur’}, {min : 1, max: 20, message: ’長度在1-20之間’, trigger: ’change’} ], password:[ {required: true, message: ’請輸入密碼’, trigger: ’blur’}, {min : 1, max: 100, message: ’長度在1-100之間’, trigger: ’change’} ], email:[ {required: true, message: ’請輸入郵箱’, trigger: ’blur’}, {min : 1, max: 50, message: ’長度在1-50之間’, trigger: ’change’}, {type: ’email’, message: ’請輸入正確格式的郵箱’, trigger: ’blur’} ], phone:[ {required: true, message: ’請輸入電話號’, trigger: ’blur’}, {min : 1, max: 20, message: ’長度在1-20之間’, trigger: ’change’}, {pattern: /^1([38][0-9]|4[5-9]|5[0-3,5-9]|66|7[0-8]|9[89])[0-9]{8}$/, message: ’請輸入正確的手機號’, trigger: ’blur’} ], } } }, methods:{ save() { //提交表單前需要對表單再次進行驗證 //獲取表單對象//表單二次驗證 this.$refs[’ruleForm’].validate((flag) => { //如果通過驗證,則進行表單數據提交 if(flag === true) { this.request(’/umsadmin/add’, ’post’, this.forms, response => { this.$message.success(response.message) }) } }) }, changeimg(file, fileList) { this.forms.imgobj = file.raw }, removeimg() { this.forms.imgobj = null } } }</script> <style></style>2、此時前端給后端發post請求會出現跨域錯誤

跨域錯誤解決需要在后端植入跨域過濾器(Bean節點)

//跨域問題解決@BeanCorsFilter getCorsFilter() { UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedHeader('*'); corsConfiguration.addAllowedMethod('*'); corsConfiguration.addAllowedOrigin('*'); //域名 configurationSource.registerCorsConfiguration('/**', corsConfiguration); return new CorsFilter(configurationSource);}

到此這篇關于SpringBoot+Vue實現數據添加功能的文章就介紹到這了,更多相關SpringBoot Vue數據添加內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 | 发电机价格|发电机组价格|柴油发电机价格|柴油发电机组价格网 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 货车视频监控,油管家,货车油管家-淄博世纪锐行电子科技 | 冷却塔减速机器_冷却塔皮带箱维修厂家_凉水塔风机电机更换-广东康明冷却塔厂家 | 一体式钢筋扫描仪-楼板测厚仪-裂缝检测仪-泰仕特(北京) | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 | 河南中专学校|职高|技校招生-河南中职中专网 | 高防护蠕动泵-多通道灌装系统-高防护蠕动泵-www.bjhuiyufluid.com慧宇伟业(北京)流体设备有限公司 | 商标转让-购买商标专业|放心的商标交易网-蜀易标商标网 | 广东风淋室_广东风淋室厂家_广东风淋室价格_广州开源_传递窗_FFU-广州开源净化科技有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 济南网站建设_济南网站制作_济南网站设计_济南网站建设公司_富库网络旗下模易宝_模板建站 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 流水线电子称-钰恒-上下限报警电子秤-上海宿衡实业有限公司 | 南京兰江泵业有限公司-水解酸化池潜水搅拌机-絮凝反应池搅拌机-好氧区潜水推进器 | 长沙网站建设制作「网站优化推广」-网页设计公司-速马科技官网 | 接地电阻测试仪[厂家直销]_电缆故障测试仪[精准定位]_耐压测试仪-武汉南电至诚电力设备 | 北京四合院出租,北京四合院出售,北京平房买卖 - 顺益兴四合院 | 通风气楼_通风天窗_屋顶风机-山东美创通风设备有限公司 | 海日牌清洗剂-打造带电清洗剂、工业清洗剂等清洗剂国内一线品牌 海外整合营销-独立站营销-社交媒体运营_广州甲壳虫跨境网络服务 | 北京包装设计_标志设计公司_包装设计公司-北京思逸品牌设计 | 泰国试管婴儿_泰国第三代试管婴儿费用|成功率|医院—新生代海外医疗 | 小港信息港-鹤壁信息港 鹤壁老百姓便民生活信息网站 | 背压阀|减压器|不锈钢减压器|减压阀|卫生级背压阀|单向阀|背压阀厂家-上海沃原自控阀门有限公司 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 干洗店加盟_洗衣店加盟_干洗店设备-伊蔻干洗「武汉总部」 | 河南砖机首页-全自动液压免烧砖机,小型砌块水泥砖机厂家[十年老厂] | 碳纤维布-植筋胶-灌缝胶-固特嘉加固材料公司 | 物和码官网,物和码,免费一物一码数字化营销SaaS平台 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 马尔表面粗糙度仪-MAHR-T500Hommel-Mitutoyo粗糙度仪-笃挚仪器 | 桐城新闻网—桐城市融媒体中心主办 | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 铁素体测量仪/检测仪/铁素体含量测试仪-苏州圣光仪器有限公司 | 不锈钢螺丝,不锈钢螺栓,不锈钢标准件-江苏百德特种合金有限公司 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 上海网站建设-上海网站制作-上海网站设计-上海做网站公司-咏熠软件 | 「银杏树」银杏树行情价格_银杏树种植_山东程锦园林 |