• 利用SpringBoot重写黑马旅游网


    突然想起刚开始学习JavaEE的跟着视频做了一个黑马旅游网的小项目,项目本身没啥特点,之后自己部署到了服务器的tomcat上,但是由于里面用到了redis缓存,他的配置文件一直报错,最近寻思这用Spring Boot把他集成进去
    这是我们现在的目录,新建一个Spring Boot的项目
    在这里插入图片描述
    改吧改吧,把原来的dao层用mybatis-plus
    通过一上午的努力
    在这里插入图片描述
    现在已经初见雏形
    在此期间我遇到了一个问题,由于之前是javaee的项目用的是Servlet
    所以在启动类上加上了

    @ServletComponentScan(basePackages = "com.wangbing.springboottravel.web")
    
    • 1

    开启mvc对于原生Servlet的支持
    现在又遇到问题了,被@Mapper注解的Dao通过 @Autowired注入但是并没有注入
    这时考虑到@Mapper注解的应该是有实现类的所以自动注入的话会报错,于是我用到了
    extends ServiceImpl<UserDao,User>
    来解决这个问题
    记得要在他的继承类上
    extends IService
    还是不行后来深入的了解才发现,原来是原项目中使用

            //3.调用service查询
            UserService service = new UserServiceImpl();
    
    • 1
    • 2

    这种方式获取Uservice而不是交给spring管理
    之后经过全部把new的方式改为@Autowired完成了这个项目的重写
    因为在之前的路线实体类中附带了表中没有的列

       private Category category;//所属分类
      private Seller seller;//所属商家
      private List<RouteImg> routeImgList;//商品详情图片列
    
    • 1
    • 2
    • 3

    比如这三个现在要写一个VO类来映射同时使用
    BeanUtils.copyProperties(routeVo, route);
    来对属性进行复制

    @Data
    public class RouteVo {
       private int rid;//线路id,必输
       private String rname;//线路名称,必输
       private double price;//价格,必输
       private String routeIntroduce;//线路介绍
       private String rflag;   //是否上架,必输,0代表没有上架,1代表是上架
       private String rdate;   //上架时间
       private String isThemeTour;//是否主题旅游,必输,0代表不是,1代表是
       private int count;//收藏数量
       private int cid;//所属分类,必输
       private String rimage;//缩略图
       private int sid;//所属商家
       private String sourceId;//抓取数据的来源id
    
       private Category category;//所属分类
       private Seller seller;//所属商家
       private List<RouteImg> routeImgList;//商品详情图片列表
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述
    现在面临一个问题,列表展示页好了,但是不分页。

            Page<Route> page = new Page<>(start, pageSize);
            Page selectPage = routeDao.selectPage(page, queryWrapper);
            List<Route> list = selectPage.getRecords();
    
    • 1
    • 2
    • 3

    因为我们需要给他加一个分页插件

    @Configuration
    public class MybatisPlusConfig {
    
        /**
         * 防止 修改与删除时对全表进行操作
         * @return
         */
        @Bean
        public BlockAttackInnerInterceptor blockAttackInnerInterceptor(){
            return new BlockAttackInnerInterceptor();
        }
    
    
        /**
         * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            return interceptor;
        }
    
    
        /**
         * ConfigurationCustomizer,这里引用的是MyBatisPlus自定义的一个和MyBatis同名的接口,com.baomidou.mybatisplus.spring.boot.starter.ConfigurationCustomizer,
         * 因此必须使用MyBatisPlus的ConfigurationCustomizer才行
         * @return
         */
        public ConfigurationCustomizer configurationCustomizer(){
            return new ConfigurationCustomizer() {
                @Override
                public void customize(MybatisConfiguration configuration) {
                    configuration.setCacheEnabled(true);
                    configuration.setMapUnderscoreToCamelCase(true);
                    configuration.setCallSettersOnNulls(true);
                    configuration.setJdbcTypeForNull(JdbcType.NULL);
                }
            };
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    改了改现在在详情页面,findOne方法查询不到数据了
    在这里插入图片描述
    Debug方法发现原因是
    BeanUtils.copyProperties(route, routeVo);
    位置反了。
    现在还有一个小bug,无法显示还有多少条数据
    在这里插入图片描述
    通过debug发现应该是不为空才做这样的判断

            if (!ObjectUtils.isEmpty(cid) && cid != 0) {
                queryWrapper.eq("cid", cid);
            }
            if (!StringUtils.isEmpty(rname)) {
                queryWrapper.eq("rname", rname);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    现在好了
    在这里插入图片描述
    大功告成,代码在结尾
    在这里插入图片描述

    新版黑马旅游网

    旧版黑马旅游网

  • 相关阅读:
    springboot启动流程是什么?
    和鲸 × 北中医:高规格、高并发,一场真正的人工智能分析应用临场实践考核
    API网关那些事【架构新知系列】
    《向量数据库指南》——火山引擎向量数据库对正式外开放服务
    基于STM32CUBEMX驱动TMOS模块STHS34PF80(5)----配置嵌入式函数
    LeetCode 45. 跳跃游戏 II
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java高校饭堂管理系统8gmjo
    vue接口封装 路由拦截处理
    numpy(np) np 文件存储与读取
    谷粒学苑_第六天
  • 原文地址:https://blog.csdn.net/weixin_53227758/article/details/124990069