突然想起刚开始学习JavaEE的跟着视频做了一个黑马旅游网的小项目,项目本身没啥特点,之后自己部署到了服务器的tomcat上,但是由于里面用到了redis缓存,他的配置文件一直报错,最近寻思这用Spring Boot把他集成进去
这是我们现在的目录,新建一个Spring Boot的项目
改吧改吧,把原来的dao层用mybatis-plus
通过一上午的努力
现在已经初见雏形
在此期间我遇到了一个问题,由于之前是javaee的项目用的是Servlet
所以在启动类上加上了
@ServletComponentScan(basePackages = "com.wangbing.springboottravel.web")
开启mvc对于原生Servlet的支持
现在又遇到问题了,被@Mapper注解的Dao通过 @Autowired注入但是并没有注入
这时考虑到@Mapper注解的应该是有实现类的所以自动注入的话会报错,于是我用到了
extends ServiceImpl<UserDao,User>
来解决这个问题
记得要在他的继承类上
extends IService
还是不行后来深入的了解才发现,原来是原项目中使用
//3.调用service查询
UserService service = new UserServiceImpl();
这种方式获取Uservice而不是交给spring管理
之后经过全部把new的方式改为@Autowired完成了这个项目的重写
因为在之前的路线实体类中附带了表中没有的列
private Category category;//所属分类
private Seller seller;//所属商家
private List<RouteImg> routeImgList;//商品详情图片列
比如这三个现在要写一个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;//商品详情图片列表
}
现在面临一个问题,列表展示页好了,但是不分页。
Page<Route> page = new Page<>(start, pageSize);
Page selectPage = routeDao.selectPage(page, queryWrapper);
List<Route> list = selectPage.getRecords();
因为我们需要给他加一个分页插件
@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);
}
};
}
}
改了改现在在详情页面,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);
}
现在好了
大功告成,代码在结尾