springboot+mybaitsplus+vue簡單的CURD
環境:
jdk1.8 、maven3.3.6+、MySQL5.7
開發工具:ieda
后端創建
創建springboot項目,填寫項目名稱。
添加依賴,這里不添加也可以,后面在pom文件添加也行。
next——>finish
下載依賴完成以后得到這樣一個目錄,啟動項目看看是否成功。
如果依賴下載不成功,設置自己阿里云的maven
已經配置的可以忽略此步驟
阿里云配置如下
<mirror><id>aliyunmaven
</id><mirrorOf>*,!jeecg,!jeecg-snapshots,!getui-nexus
</mirrorOf><name>aliyun maven
</name><url>https://maven.aliyun.com/repository/public
</url>
</mirror>
倉庫位置
<localRepository>D:\Repository\ideaRepository
</localRepository>
配置數據庫,刪掉application.properties,新建application.yarm和application-dev.yaml文件
配置數據源
application-dev.yaml
#開發環境
spring:#數據庫連接信息datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/zoousername: rootpassword: 123456
#端口
# context-path 默認根目錄
server:port: 8089
# servlet:
# context-path: /api
#掃描mapper
mybatis-plus:mapper-locations: classpath:/mapper/*.xml
application.yarm
spring:application:name: user#啟用配置文件profiles:active: dev
數據庫配置:
- 打開navicat測試連接
- 連接成功創建數據庫
點擊新建數據庫,填寫數據庫名稱和字符集、排序規則。然后保存
8.啟動項目
看控制臺信息
成功!
開始編寫curd代碼
<dependencies><dependency><groupId>org.springframework.boot
</groupId><artifactId>spring-boot-starter-web
</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot
</groupId><artifactId>mybatis-spring-boot-starter
</artifactId><version>2.2.2
</version></dependency><dependency><groupId>org.springframework.boot
</groupId><artifactId>spring-boot-devtools
</artifactId><scope>runtime
</scope><optional>true
</optional></dependency><dependency><groupId>mysql
</groupId><artifactId>mysql-connector-java
</artifactId><scope>runtime
</scope></dependency><dependency><groupId>org.projectlombok
</groupId><artifactId>lombok
</artifactId><optional>true
</optional></dependency><dependency><groupId>org.springframework.boot
</groupId><artifactId>spring-boot-starter-test
</artifactId><scope>test
</scope></dependency><dependency><groupId>com.baomidou
</groupId><artifactId>mybatis-plus-generator
</artifactId><version>3.5.1
</version></dependency><dependency><groupId>org.freemarker
</groupId><artifactId>freemarker
</artifactId><version>2.3.30
</version></dependency><dependency><groupId>com.baomidou
</groupId><artifactId>mybatis-plus-boot-starter
</artifactId><version>3.4.2
</version></dependency><dependency><groupId>io.swagger
</groupId><artifactId>swagger-annotations
</artifactId><version>1.5.22
</version></dependency></dependencies>
- 簡化開發使用mybatis-plus代碼生成器
- 步驟:
1.創建utils包
2.GenerateTool類(生成器代碼類)
package com.cjy.mybatis_plus.utils;import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.extern.slf4j.Slf4j;import java.util.Collections;
@Slf4j
public class GenerateTool {public static void main(String[] args
) {FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/zoo", "root", "123456").globalConfig(builder
-> {builder
.author("xieyixu") .enableSwagger() .fileOverride() .outputDir("D:\\IDEA\\mybatis_plus\\src\\main\\java"); }).packageConfig(builder
-> {builder
.parent("com.cjy") .moduleName("mybatis_plus") .pathInfo(Collections.singletonMap(OutputFile.mapperXml
, "D:\\IDEA\\mybatis_plus\\src\\main\\resources\\mapper")); }).strategyConfig(builder
-> {builder
.addInclude("admin").addInclude("anima").addInclude("breeder").addInclude("doctor").addInclude("support").addTablePrefix("t_", "c_"); }).templateEngine(new FreemarkerTemplateEngine()) .execute();}
}
3.數據庫表設計
SET
NAMES utf8mb4
;
SET FOREIGN_KEY_CHECKS
= 0;-- ----------------------------
-- Table structure
for admin
-- ----------------------------
DROP TABLE IF EXISTS `admin`
;
CREATE TABLE `admin`
(`id`
varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT
'編號',`name`
varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'姓名',`sex`
char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'性別',`age`
int(3) NULL DEFAULT NULL COMMENT
'年齡',`phone`
varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'電話',PRIMARY KEY
(`id`
) USING BTREE
) ENGINE
= InnoDB CHARACTER SET
= utf8mb4 COLLATE
= utf8mb4_general_ci COMMENT
= '管理員表' ROW_FORMAT
= Dynamic;-- ----------------------------
-- Table structure
for anima
-- ----------------------------
DROP TABLE IF EXISTS `anima`
;
CREATE TABLE `anima`
(`id`
varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT
'動物編號',`name`
varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'動物名',`species`
varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'動物品種',`price`
float(10, 2) NULL DEFAULT NULL COMMENT
'價格',PRIMARY KEY
(`id`
) USING BTREE
) ENGINE
= InnoDB CHARACTER SET
= utf8mb4 COLLATE
= utf8mb4_general_ci COMMENT
= '動物表' ROW_FORMAT
= Dynamic;-- ----------------------------
-- Table structure
for breeder
-- ----------------------------
DROP TABLE IF EXISTS `breeder`
;
CREATE TABLE `breeder`
(`id`
varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT
'編號',`name`
varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'姓名',`sex`
char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'性別',`age`
int(3) NULL DEFAULT NULL COMMENT
'年齡',`phone`
varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'電話',PRIMARY KEY
(`id`
) USING BTREE
) ENGINE
= InnoDB CHARACTER SET
= utf8mb4 COLLATE
= utf8mb4_general_ci COMMENT
= '飼養員表' ROW_FORMAT
= Dynamic;-- ----------------------------
-- Table structure
for doctor
-- ----------------------------
DROP TABLE IF EXISTS `doctor`
;
CREATE TABLE `doctor`
(`id`
varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT
'編號',`name`
varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'姓名',`sex`
char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'性別',`age`
int(3) NULL DEFAULT NULL COMMENT
'年齡',`phone`
varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'電話',PRIMARY KEY
(`id`
) USING BTREE
) ENGINE
= InnoDB CHARACTER SET
= utf8mb4 COLLATE
= utf8mb4_general_ci COMMENT
= '醫生表' ROW_FORMAT
= Dynamic;-- ----------------------------
-- Table structure
for support
-- ----------------------------
DROP TABLE IF EXISTS `support`
;
CREATE TABLE `support`
(`id`
varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT
'編號',`name`
varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'姓名',`sex`
char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'性別',`age`
int(3) NULL DEFAULT NULL COMMENT
'年齡',`phone`
varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT
'電話',PRIMARY KEY
(`id`
) USING BTREE
) ENGINE
= InnoDB CHARACTER SET
= utf8mb4 COLLATE
= utf8mb4_general_ci COMMENT
= '后勤人員表' ROW_FORMAT
= Dynamic;SET FOREIGN_KEY_CHECKS
= 1;
- 得到如圖所示目錄:
- 到這里基本OK,自動生成不會自動加注解自己手動加
- 添加主鍵刪除使用Lombok工具刪除get和set方法
- 編輯接口數據,簡單的curd就這樣實現了
AdminController類
package com.cjy.mybatis_plus.controller;import com.cjy.mybatis_plus.entity.Admin;
import com.cjy.mybatis_plus.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.stereotype.Controller;
@Controller
@RequestMapping("/mybatis_plus/admin")
public class AdminController {@Autowiredprivate IAdminService iAdminService
;@PutMapping("/save")public void save(Admin admin
){iAdminService
.save(admin
);}}
MybatisPlusApplicationTests類
package com.cjy.mybatis_plus;import com.cjy.mybatis_plus.entity.Admin;
import com.cjy.mybatis_plus.service.IAdminService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestclass MybatisPlusApplicationTests {
@Autowired
private IAdminService iAdminService
;@Testvoid save() {for (int i
= 0; i
<20 ; i
++) {Admin admin
= new Admin();admin
.setId("0"+i
);admin
.setAge(25);admin
.setName("李四");admin
.setPhone("15487956123");admin
.setSex("男");iAdminService
.save(admin
);}}@Testvoid delete() {iAdminService
.removeById(00);}@Testvoid update() {Admin admin
= new Admin();admin
.setId("01");admin
.setAge(25);admin
.setName("張三");admin
.setPhone("15487956123");admin
.setSex("男");iAdminService
.updateById(admin
);}@Testvoid find() {Admin byId
= iAdminService
.getById(01);System.out
.println(byId
);}}
結果:
OK!
vue搭建看我這篇文章cmd搭建vue手腳架
或者idea搭建vue項目
總結
以上是生活随笔為你收集整理的springboot+mybaitsplus+vue简单的CURD的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。