从0搭建一个Springboot+vue前后端分离项目(六)后台编写配置类与接口
生活随笔
收集整理的這篇文章主要介紹了
从0搭建一个Springboot+vue前后端分离项目(六)后台编写配置类与接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
新建一個包,里面放入一些常用的配置類
引入mybatis-plus包與插件
引入包
前往官網查看
https://baomidou.com/pages/226c21/#%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B7%A5%E7%A8%8B <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3.4</version> </dependency>MybatisPlusConfig----MybatisPlus配置文件
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration @MapperScan("com.example.demo.mapper") public class MybatisPlusConfig {/*** mybatis-plus.分頁插件* @return*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));return interceptor;} }配置result包裝返回與前臺交互數據對象
code 代表成功或者失敗;
msg即返回的信息
編寫用戶接口
新建一個包entity實體類
建一個java對象與數據庫進行一 一對應
package com.example.demo.entity;import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data;//與數據庫的表名一一對應 @TableName("user") //使用lombok插件簡化快速寫代碼 @Data public class User {//定義自增@TableId(type = IdType.AUTO)private Integer id;private String username;private Integer age;private String sex;private String address;private String password;}編寫用戶接口UserControlller
新建一個包Controller
package com.example.demo.controller;import com.example.demo.common.Result; import com.example.demo.entity.User; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;//定義返回json數據 @RestController //定義路由 @RequestMapping("/user") public class UserController {//定義post接口@PostMapping//前端網頁的新增用戶接口,@RequestBody作用是使前臺傳來的json格式的代碼轉化為java對象public Result save(@RequestBody User user) {}}
拿到user數據之后就可以把它插入到數據庫當中,編寫mapper部分
創建一個UserMapper接口類
前往mybatis-plus官網參考
https://baomidou.com/pages/49cc81/#mapper-crud-%E6%8E%A5%E5%8F%A3
繼承BaseMapper 就可以實現一些數據庫單表的增刪改查
最后完善一下UserConler接口
package com.example.demo.controller;import com.example.demo.common.Result; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;//定義返回json數據 @RestController //定義路由 @RequestMapping("/user") public class UserController {//將mapper引入@ResourceUserMapper userMapper;//定義post接口@PostMapping//前端網頁的新增用戶接口,@RequestBody作用是使前臺傳來的json格式的代碼轉化為java對象//<?>泛型表示任何一種類型public Result<?> save(@RequestBody User user) {userMapper.insert(user);return Result.success();}}總結
以上是生活随笔為你收集整理的从0搭建一个Springboot+vue前后端分离项目(六)后台编写配置类与接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从0搭建一个Springboot+vue
- 下一篇: This dependency was