- config包下面的WebMvcConfig,默认的资源访问路径就是templates和static包,一般来说快速生成的spring boot项目包名不会改变,所以是可以直接访问的,但是我经过测试发现还是需要写这个类进行静态资源的映射,才能访问该包下面的资源;如果你的包名改了,就比如我下面这个backend,那么就必须写这个类进行映射
package com.drs.take_out.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Slf4j @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { /** * 设置静态资源映射 * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { log.info("开始进行静态资源映射..."); registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/"); registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/"); } }
- handler(配置数据库自动填充)
package com.drs.take_out.handler; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @Component // 把处理器添加到IOC容器中 @Slf4j public class MyMetaObjectHandler implements MetaObjectHandler { /*插入时对应的填充*/ @Override public void insertFill(MetaObject metaObject) { log.info("start insert fill ...."); /*每写一个自动填充都要在 Handler 里面设置*/ this.strictInsertFill(metaObject,"creatTime", LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject,"updateTime", LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject,"status", Integer.class, 1); } /*更新时对应的填充*/ @Override public void updateFill(MetaObject metaObject) { log.info("start update fill ...."); this.strictInsertFill(metaObject,"updateTime", LocalDateTime.class, LocalDateTime.now()); } }
以下为完整的项目结构
springboot的一些相关配置(通用)
于 2022-06-02 20:03:38 首次发布