网站建设类文章如何提高网站在搜索引擎中的排名
案例实现方案分析
实体类开发————使用Lombok快速制作实体类
Dao开发————整合MyBatisPlus,制作数据层测试类
Service开发————基于MyBatisPlus进行增量开发,制作业务层测试类
Controller开发————基于Restful开发,使用PostMan测试接口功能
Controller开发————前后端开发协议制作
页面开发————基于VUE+ElementUI制作,前后端联调,页面数据处理,页面消息处理
列表、新增、修改、删除、分页、查询
项目异常处理
按条件查询————页面功能调整、Controller修正功能、Service修正功能
1.模块创建
-
勾选SpringMVC与MySQL坐标
-
修改配置文件为yml格式
-
设置端口为80方便访问
2.实体类开发
Lombok,一个Java类库,提供了一组注解,简化POJO实体类开发
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</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-plus+Druid
1.导入相对应的MyBatisPlus与Druid对应的starter
<!--mybatis-plus--> <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3</version> </dependency> <!--druid连接池--> <dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.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?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=trueusername: 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 BaseMapper<Book> { }
制作测试
@SpringBootTest public class BookTest {@Autowiredprivate BookDao bookDao; @Testpublic void testById(){Book byId = bookDao.selectById(2);System.out.println(byId);} @Testpublic void testSave(){Book book=new Book();book.setName("明朝那些事");book.setType("有关历史");book.setDescription("nib");bookDao.insert(book); } @Testpublic void testDelete(){bookDao.deleteById(15);} @Testpublic void testUpdata(){Book book=new 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 page=new 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(){ QueryWrapper<Book> queryWrapper=new QueryWrapper<Book>();queryWrapper.like("name","spring");//select*from tbl_book where name like%spring%System.out.println( bookDao.selectList(queryWrapper)); } //使用QueryWrapper对象封装查询条件,推荐使用LambdaQueryWrapper对象,所有查询操作封装成方法调用,支持动态拼写查询条件@Testpublic void testGetByCondition2(){String name="Spring";LambdaQueryWrapper<Book> queryWrapper=new LambdaQueryWrapper<Book>();//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 mybatisPlusInterceptor=new 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);List<Book> getAll();IPage<Book> 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提供有业务层通用接口(ISerivce<T>)与业务层通用实现(ServiceImpl<M,T>)在通用类基础上做功能重载或功能追加 注意重载时不要覆盖原始操作,避免原始提供的功能丢失
@Service public class IBookServiceImpl extends ServiceImpl<BookDao,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 book=new Book();book.setName("大名王朝");book.setType("有关历史");book.setDescription("nib");bookService.save(book); }@Testpublic void testUpdata(){Book book=new Book();book.setId(14);book.setName("明朝那些事");book.setType("有关历史");book.setDescription("nibglss");bookService.updateById(book);}@Testpublic void testGetByPage(){IPage<Book> page = new Page<Book>(1,5);IPage<Book> byPage = bookService.page(page);List<Book> records = byPage.getRecords();System.out.println(records);}}
总结:
-
使用通用接口(ISerivce<T>)快速开发Service
-
使用通用实现类(ServiceImpl<M,T>)快速开发ServiceImpl
-
可以在通用接口基础上做功能重载或功能追加
-
注意重载时不要覆盖原始操作,避免原始提供的功能丢失