西宁网站建设高端,厦门网站建设人才,电商网站前台功能模块,那些影视解析网站怎么做的案例实现方案分析
实体类开发————使用Lombok快速制作实体类
Dao开发————整合MyBatisPlus#xff0c;制作数据层测试类
Service开发————基于MyBatisPlus进行增量开发#xff0c;制作业务层测试类
Controller开发————基于Restful开发#xff0c;使用PostM…案例实现方案分析
实体类开发————使用Lombok快速制作实体类
Dao开发————整合MyBatisPlus制作数据层测试类
Service开发————基于MyBatisPlus进行增量开发制作业务层测试类
Controller开发————基于Restful开发使用PostMan测试接口功能
Controller开发————前后端开发协议制作
页面开发————基于VUEElementUI制作前后端联调页面数据处理页面消息处理
列表、新增、修改、删除、分页、查询
项目异常处理
按条件查询————页面功能调整、Controller修正功能、Service修正功能
1.模块创建 勾选SpringMVC与MySQL坐标 修改配置文件为yml格式 设置端口为80方便访问
2.实体类开发
Lombok一个Java类库提供了一组注解简化POJO实体类开发
dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId
/dependency
lombok版本由SpringBoot提供无需指定版本
常用注解Data
Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
为当前实体类在编译期设置对应的get/set方法toString方法hashCode方法equals方法等
3.数据层面开发
使用技术MyBatis-plusDruid
1.导入相对应的MyBatisPlus与Druid对应的starter
!--mybatis-plus--
dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.3/version
/dependency
!--druid连接池--
dependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.2.16/version
/dependency
2.配置数据源与MyBatisPlus对应的基础配置id生成策略使用数据库自增策略
#配置端口号
server:port: 80
#配置数据源druid
spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_db?useSSLfalseserverTimezoneAsia/ShanghaiallowPublicKeyRetrievaltrueusername: rootpassword: 123456
#mp的配置
mybatis-plus:global-config:db-config:table-prefix: tbl_#数据库id的自增策略id-type: auto#配置日志configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
继承BaseMapper并指定泛型
Mapper
public interface BookDao extends BaseMapperBook {
}
制作测试
SpringBootTest
public class BookTest {Autowiredprivate BookDao bookDao;
Testpublic void testById(){Book byId bookDao.selectById(2);System.out.println(byId);}
Testpublic void testSave(){Book booknew Book();book.setName(明朝那些事);book.setType(有关历史);book.setDescription(nib);bookDao.insert(book);
}
Testpublic void testDelete(){bookDao.deleteById(15);}
Testpublic void testUpdata(){Book booknew Book();book.setId(16);book.setName(明朝那些事);book.setType(有关历史);book.setDescription(nibglss);bookDao.updateById(book);}
Testpublic void testGetAll(){System.out.println( bookDao.selectList(null));}
Testpublic void testGetPage(){//需要拦截器追加sql分页的sql语句/** 参数1当前是第几页* 参数2每页显示的数据条数* */IPage pagenew Page(2,5);bookDao.selectPage(page,null);System.out.println(page.getCurrent());System.out.println(page.getPages());System.out.println(page.getSize());System.out.println(page.getRecords());System.out.println(page.getTotal());}//按条件查询Testpublic void testGetByCondition1(){
QueryWrapperBook queryWrappernew QueryWrapperBook();queryWrapper.like(name,spring);//select*from tbl_book where name like%spring%System.out.println( bookDao.selectList(queryWrapper));
}
//使用QueryWrapper对象封装查询条件推荐使用LambdaQueryWrapper对象所有查询操作封装成方法调用,支持动态拼写查询条件Testpublic void testGetByCondition2(){String nameSpring;LambdaQueryWrapperBook queryWrappernew LambdaQueryWrapperBook();//select*from tbl_book where name like%spring%queryWrapper.like(name!null,Book::getName,name);//select*from tbl_book where name like%spring%System.out.println( bookDao.selectList(queryWrapper));
}
}
分页操作是在MyBatisPlus的常规操作基础上增强得到内部是动态的拼写SQL语句因此需要增强对应的功能使用MyBatisPlus拦截器实现
Configuration//用于指定该类是spring配置类创建容器时会从该类加载注解
public class MPConfig {Bean//标注该方法的返回值会存储到Spring容器中public MybatisPlusInterceptor mybatisPlusInterceptor(){//拦截器的外壳MybatisPlusInterceptor mybatisPlusInterceptornew MybatisPlusInterceptor();
// 用什么拦截器加载什么拦截器mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());return mybatisPlusInterceptor;}
}
4.业务开发层
定义接口
public interface BookService {boolean save(Book book);boolean delete(Integer id);boolean update(Book book);Book getById(Integer id);ListBook getAll();IPageBook getByPage(int currentPage,int pageSize);
}
实现层定义
Service
public class BookServiceImpl implements BookService {Autowiredprivate BookDao bookDao;public Boolean save(Book book) {return bookDao.insert(book) 0;}public Boolean delete(Integer id) {return bookDao.deleteById(id) 0;}public Boolean update(Book book) {return bookDao.updateById(book) 0;}
}
测试类定义
SpringBootTest
public class BookServiceTest {Autowiredprivate BookService bookService;Testvoid testGetById(){bookService.getById(9);}Testvoid testGetAll(){bookService.getAll();}Testvoid testGetByPage(){bookService.getByPage(1,5);}
}
5.业务层———快速开发
使用MyBatisPlus提供有业务层通用接口ISerivceT与业务层通用实现ServiceImplM,T在通用类基础上做功能重载或功能追加 注意重载时不要覆盖原始操作避免原始提供的功能丢失
Service
public class IBookServiceImpl extends ServiceImplBookDao,Book implements IBookService {
}
测试
SpringBootTest
public class BookServiceTest2 {Autowiredprivate IBookServiceImpl bookService;Testpublic void getById(){System.out.println( bookService.getById(1));}Testpublic void selectAll(){System.out.println(bookService.list());}Testpublic void delete(){bookService.removeById(16);}Testpublic void testSave(){Book booknew Book();book.setName(大名王朝);book.setType(有关历史);book.setDescription(nib);bookService.save(book);
}Testpublic void testUpdata(){Book booknew Book();book.setId(14);book.setName(明朝那些事);book.setType(有关历史);book.setDescription(nibglss);bookService.updateById(book);}Testpublic void testGetByPage(){IPageBook page new PageBook(1,5);IPageBook byPage bookService.page(page);ListBook records byPage.getRecords();System.out.println(records);}}
总结 使用通用接口ISerivceT快速开发Service 使用通用实现类ServiceImplM,T快速开发ServiceImpl 可以在通用接口基础上做功能重载或功能追加 注意重载时不要覆盖原始操作避免原始提供的功能丢失