导入要使用的jar包
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> <scope>provided</scope> </dependency> <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.22</version> </dependency>
在applycation.yaml文件中配置相关文件
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/XXXXX?serverTimezone=GMT type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.cj.jdbc.Driver username: root password: XXXXX mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl server: port: 端口号
可以配置一个vue文件,我就是vue文件,或者有html也行
导入element-ui和axios
import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); **这里是完整引入,可以按需引入,官网有写** 导入axios npm install axios
到这里所有的准备工作就已经弄完了
@Data @TableName(value = "books") @JsonInclude(JsonInclude.Include.NON_NULL) //jackson 实体转json 为NULL的字段不参加序列化(即不显示) public class Book { @TableId(value = "bookID",type = IdType.AUTO) private Integer bookID; @TableField(value = "bookName") private String bookName; @TableField(value = "bookCounts") private int bookCounts; @TableField(value = "detail") private String detail; }
@Mapper @Repository public interface BookMapper extends BaseMapper<Book> { }
serviece
public interface BookService extends IService<Book> { } //serviceimpl @Service public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService { }
<el-input placeholder="请输入id" v-model="input" clearable style="width: 200px;float: left"> </el-input> <el-button type="primary" class="btn" @click="getClick(input)">查询</el-button> <el-button type="primary" class="btn" @click="resetClick">重置</el-button> <el-button type="primary" class="btn" @click="dialogAddBookVisible = true">添加</el-button>
在这里插入图片描述
<el-table :data="book" border style="width: 100%"> <el-table-column fixed prop="bookID" label="id" width="300"> </el-table-column> <el-table-column prop="bookName" label="书籍名称" width="120"> </el-table-column> <el-table-column prop="bookCounts" label="数据数量" width="120"> </el-table-column> <el-table-column prop="detail" label="书籍评价" width="120"> </el-table-column> <el-table-column fixed="right" label="操作" width="300"> <template slot-scope="scope"> <el-button type="text" @click="updateClick(scope.row)">编辑</el-button> <el-button type="text" size="small" @click="removeClick(scope.row)">删除</el-button> </template> </el-table-column> </el-table>
<el-dialog title="添加书籍信息" :visible.sync="dialogAddBookVisible" :append-to-body="true"> <el-form :model="addBook"> <el-form-item label="id" prop="bookID" :label-width="formLabelWidth" > <el-input v-model="addBook.bookID" placeholder="请输入书籍编号" disabled></el-input> </el-form-item> <el-form-item label="书籍名称" :label-width="formLabelWidth" prop="bookName"> <el-input v-model="addBook.bookName" autocomplete="off" placeholder="请输入书籍名称"></el-input> </el-form-item> <el-form-item label="书籍数量" :label-width="formLabelWidth"> <el-input v-model="addBook.bookCounts" autocomplete="off" placeholder="请输入书籍数量"></el-input> </el-form-item> <el-form-item label="书籍评价" :label-width="formLabelWidth"> <el-input v-model="addBook.detail" autocomplete="off" placeholder="请输入书籍评价"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click=" dialogAddBookVisible= false">取 消</el-button> <el-button type="primary" @click="addClick">确 定</el-button> </div> </el-dialog>
<el-dialog title="修改信息" :visible.sync="dialogFormVisible" :append-to-body="true"> <el-form :model="books"> <el-form-item label="id" prop="bookID" :label-width="formLabelWidth"> <el-input v-model="books.bookID" disabled></el-input> </el-form-item> <el-form-item label="书籍名称" :label-width="formLabelWidth" prop="bookName"> <el-input v-model="books.bookName" autocomplete="off" placeholder="请输入书籍名称"></el-input> </el-form-item> <el-form-item label="书籍数量" :label-width="formLabelWidth"> <el-input v-model="books.bookCounts" autocomplete="off" placeholder="请输入书籍数量"></el-input> </el-form-item> <el-form-item label="书籍评价" :label-width="formLabelWidth"> <el-input v-model="books.detail" autocomplete="off" placeholder="请输入书籍评价"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="cancelClick">取 消</el-button> <el-button type="primary" @click="updateEnsure">确 定</el-button> </div> </el-dialog>
接下来就是功能的实现,CRUD工程师开始
在这里插入图片描述
@PostMapping public void addBook(@RequestBody Book book){ bookService.save(book); }
addApi(data){ axios({ url:'http://localhost:8089/book', method:'post', data:data }) } addClick(){ console.log(this.addBook) this.addApi(this.addBook) //在输入框的时候已经把数据存到了addbook了,直接传就行了 alert("添加成功") this.dialogAddBookVisible= false //让弹窗关闭 this.addBook={} //清零,不然会保存数据 },
@DeleteMapping(value = "/{id}") public void deleteBook(@PathVariable(value = "id") int id){ bookService.removeById(id); }
deleteApi(id){ return axios.request({ url:'http://localhost:8089/book/'+id, method: 'delete', }) }, removeClick(row){ console.log(row) const id=row.bookID; this.deleteApi(id) this.resetClick() },
@PutMapping public void updeteBook(@RequestBody Book book){ bookService.updateById(book); }
updateApi(data){ axios({ url:'http://localhost:8089/book', method:"put", data: data }) }, updateEnsure(){ this.updateApi(this.books) this.books={} this.dialogFormVisible=false; this.resetClick() },
@GetMapping(value = "/{id}") public Book getBook(@PathVariable int id){ return bookService.getById(id); }
geiApi(id){ axios({ url:'http://localhost:8089/book/'+id, method:"get", }).then(res=>{ const arr = []; arr.push(res.data) //这里要转换一下,不然会类型错误 this.book=arr }) },
getClick(id){ this.geiApi(id); },
crud功能已经实现了,还有很多逻辑上的问题需要去修改,下次再改