(三)基于springBoot实现SSM整合
a.整合JUnit
b.整合MyBatis
创建新模块,选择Spring初始化,并配置模块相关基础信息
创建一个dao接口
@Mapper public interface UserDao { select( "select *from user") public List<User> getAll(); }
在test中测试
@SpringBootTest class Springboot08MybatisApplicationTests { @Autowired private BookDao bookDao; @Test public void testGetById() { Book book = bookDao.getById(1); system.out.println(book); } }
1.MySQL 8.X驱动强制要求设置时区
修改url,添加serverTimezone设定
修改MySQL数据库配置(略)
2.驱动类过时,提醒更换为com.mysql.cj.jdbc.Driver
c.整合MyBatis-Plus
MyBatis-Plus与MyBatis区别
导入坐标不同
数据层实现简化
d.整合Druid
1.整合Druid需要导入Druid对应的starter
2.根据Druid提供的配置方式进行配置
整合第三方技术通用方式
导入对应的starter
根据提供的配置格式,配置非默认值对应的配置项
(四)基于SpringBoot的SSMP整合案例
实体类开发——使用Lombok快速制作实体类
@Data @NoArgsConstructor @AllArgsConstructor public class Book { private Integer id; private String type; private String name; private String description; }
Dao开发—-—-整合MyBatisPlus
@Mapper public interface BookDao extends BaseMapper<Book> { }
Service开发—-—基于MyBatisPlus进行增量开发
public interface IBookService extends IService<Book> { //分页查询 IPage<Book> getPage(int currentPage,int pageSize); }
BookServiceImpl开发—-—实现service中的方法
@Service public class BookServiceImpl extends ServiceImpl<BookDao,Book> implements IBookService { @Autowired private BookDao bookDao; @Override public IPage<Book> getPage(int currentPage, int pageSize) { IPage<Book> page=new Page<Book>(currentPage,pageSize); bookDao.selectPage(page,null); return page; } }
Controller开发—-—基于Restful开发,使用PostMan测试接口功能
@RestController @RequestMapping("/books") public class BookController { @Autowired private IBookService bookService; //查询全部 @GetMapping public List<Book> getAll(){ return bookService.list(); } //根据id删除 @DeleteMapping("/{id}") public Boolean delete(@PathVariable Integer id){ return bookService.removeById(id); } //新增 @PostMapping public Boolean save(@RequestBody Book book){ return bookService.save(book); } //修改 @PutMapping public Boolean update(@RequestBody Book book){ return bookService.updateById(book,null); } //根据id查询 @GetMapping("/{id}") public Book getById(@PathVariable Integer id){ return bookService.getById(id); } //分页查询 @GetMapping("/{currentPage}/{pageSize}") public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){ return bookService.getPage(currentPage,pageSize); } }
controller开发——前后端开发协议制作
@RestController @RequestMapping("/books") public class BookController { @Autowired private IBookService bookService; //查询全部 @GetMapping public R getAll(){ return new R(true, bookService.list()); } //根据id删除 @DeleteMapping("/{id}") public R delete(@PathVariable Integer id){ return new R(bookService.removeById(id)) ; } //新增 @PostMapping public R save(@RequestBody Book book){ Boolean falg= bookService.save(book); return new R(falg,falg?"添加成功^_^":"添加失败-_-!"); } //修改 @PutMapping public R update(@RequestBody Book book){ return new R( bookService.updateById(book)); } //根据id查询 @GetMapping("/{id}") public R getById(@PathVariable Integer id){ return new R(true,bookService.getById(id)); } 分页查询 // @GetMapping("/{currentPage}/{pageSize}") // public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){ // IPage<Book> page=bookService.getPage(currentPage,pageSize); 如果当前页码值大于总页码值,那么重新执行查询操作,使最大页码值为当前页码值 // if (currentPage>page.getPages()){ // page=bookService.getPage((int)page.getPages(),pageSize); // } // // return new R(true,page ); // } //条件查询 @GetMapping("{currentPage}/{pageSize}") public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Book book){ System.out.println(book.toString()); IPage<Book> page=bookService.getPage(currentPage,pageSize,book); //如果当前页码值大于总页码值,那么重新执行查询操作,使最大页码值为当前页码值 if (currentPage>page.getPages()){ page=bookService.getPage((int)page.getPages(),pageSize); } return new R(null!=page,page); } }
页面开发―-—基于VUE+ElementuI制作,前后端联调,页面数据处理,页面消息处理,列表、新增、修改、删除、分页、查询,按条件查询。
methods: { //列表 // getAll() { // //发送异步请求 // axios.get("/books").then((res)=>{ // //console.log(res.data); // this.dataList=res.data.data; // }); // // }, //分页查询 getAll() { //组织参数,拼接url请求地址 param="?name="+this.pagination.name; param+="&type="+this.pagination.type; param+="&description="+this.pagination.description; //发送异步请求 axios.get("/books/"+this.pagination.currentPage+"/"+this.pagination.pageSize+param).then((res)=>{ this.pagination.pageSize=res.data.data.size; this.pagination.currentPage=res.data.data.current; this.pagination.total=res.data.data.total; this.dataList=res.data.data.records; }); }, //切换页码 handleCurrentChange(currentPage) { //修改当前页码值为当前选中页码值 this.pagination.currentPage=currentPage; //重新查询 this.getAll(); }, //弹出添加窗口 handleCreate() { this.dialogFormVisible=true this.resetForm(); }, //重置表单 resetForm() { this.formData={}; }, //添加 handleAdd () { axios.post("/books",this.formData).then((res)=>{ //判断当前操作是否成功 if (res.data.flag){ //1.关闭弹层 this.dialogFormVisible=false; this.$message.success(res.data.msg); }else { this.$message.error(res.data.msg); } }).finally(()=>{ //2.重新加载数据 this.getAll(); }); }, //取消 cancel(){ this.dialogFormVisible=false; this.dialogFormVisible4Edit=false; this.$message.info("当前操作取消"); }, // 删除 handleDelete(row) { this.$confirm("此操作永久删除当前信息,是否继续","提示",{type:"info"}).then(()=>{ axios.delete("/books/"+row.id).then((res)=>{ if (res.data.flag){ this.$message.success("删除成功"); }else { this.$message.error("数据同步失败,自动刷新"); } }).finally(()=>{ this.getAll(); }); }).catch(()=>{ this.$message.success("取消删除"); }); }, //弹出编辑窗口 handleUpdate(row) { axios.get("/books/"+row.id).then((res)=>{ if (res.data.flag && res.data.data!=null){ this.dialogFormVisible4Edit=true; this.formData=res.data.data; }else { this.$message.error("数据同步失败,自动刷新"); } }).finally(()=>{ this.getAll(); }); }, //修改 handleEdit() { axios.put("/books",this.formData).then((res)=>{ if (res.data.flag){ this.dialogFormVisible4Edit=false; this.$message.success("修改成功"); }else { this.$message.error("修改失败"); } }).finally(()=>{ this.getAll(); }); },
项目异常处理,新增异常处理器ProjectExceptionAdvice类
//作为springmvc的异常处理器 @RestControllerAdvice public class ProjectExceptionAdvice { @ExceptionHandler(Exception.class) public R daException(Exception ex){ //记录日志 ex.printStackTrace(); return new R(false,"服务器故障,请稍后再试"); } }
📝📝最后附上SpringBoot的SSMP整合案例的完整代码
实体类
@Data @NoArgsConstructor @AllArgsConstructor public class Book { private Integer id; private String type; private String name; private String description; }
Dao层
@Mapper public interface BookDao extends BaseMapper<Book> { }
service层
public interface IBookService extends IService<Book> { //分页查询 IPage<Book> getPage(int currentPage, int pageSize); IPage<Book> getPage(int currentPage, int pageSize, Book book); }
serviceimpl层
@Service public class BookServiceImpl extends ServiceImpl<BookDao,Book> implements IBookService { @Autowired private BookDao bookDao; @Override public IPage<Book> getPage(int currentPage, int pageSize,Book book) { IPage<Book> page=new Page<Book>(currentPage,pageSize); LambdaQueryWrapper<Book> lqw=new LambdaQueryWrapper<Book>(); lqw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType()); lqw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName()); lqw.like(Strings.isNotEmpty(book.getDescription()),Book::getDescription,book.getDescription()); // if(book.getType()!=null&&!book.getType().equals("")){ // lqw.like(Book::getType,book.getType()); // } return bookDao.selectPage(page,lqw); } @Override public IPage<Book> getPage(int currentPage, int pageSize) { IPage<Book> page=new Page<Book>(currentPage,pageSize); bookDao.selectPage(page,null); return page; } }
controller层同上
💕💕美好的一天到此结束,下次继续努力!💕💕