pom
< ? xml version= "1.0" encoding= "UTF-8" ? >
< project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0 .0 < / modelVersion>
< parent>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- parent< / artifactId>
< version> 2.2 .6 . RELEASE< / version>
< relativePath/ >
< / parent>
< groupId> com. example< / groupId>
< artifactId> demo< / artifactId>
< version> 0.0 .1 - SNAPSHOT< / version>
< name> demo< / name>
< description> Demo project for Spring Boot< / description>
< packaging> ${ packagingMode} < / packaging>
< properties>
< java. version> 1.8 < / java. version>
< / properties>
< profiles>
< profile>
< id> dev< / id>
< properties>
< packagingMode> jar< / packagingMode>
< / properties>
< activation>
< activeByDefault> true < / activeByDefault>
< / activation>
< / profile>
< profile>
< id> test< / id>
< properties>
< packagingMode> war< / packagingMode>
< / properties>
< / profile>
< profile>
< id> prod< / id>
< properties>
< packagingMode> war< / packagingMode>
< / properties>
< / profile>
< / profiles>
< dependencies>
< dependency>
< groupId> org. slf4j< / groupId>
< artifactId> slf4j- api< / artifactId>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- web< / artifactId>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- freemarker< / artifactId>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- jdbc< / artifactId>
< / dependency>
< dependency>
< groupId> org. mybatis. spring. boot< / groupId>
< artifactId> mybatis- spring- boot- starter< / artifactId>
< version> 2.1 .1 < / version>
< / dependency>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql- connector- java< / artifactId>
< scope> runtime< / scope>
< / dependency>
< dependency>
< groupId> com. alibaba< / groupId>
< artifactId> druid- spring- boot- starter< / artifactId>
< version> 1.1 .21 < / version>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- tomcat< / artifactId>
< scope> provided< / scope>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- devtools< / artifactId>
< scope> runtime< / scope>
< optional> true < / optional>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- test< / artifactId>
< scope> test< / scope>
< exclusions>
< exclusion>
< groupId> org. junit. vintage< / groupId>
< artifactId> junit- vintage- engine< / artifactId>
< / exclusion>
< / exclusions>
< / dependency>
< dependency>
< groupId> commons- codec< / groupId>
< artifactId> commons- codec< / artifactId>
< version> 1.6 < / version>
< / dependency>
< dependency>
< groupId> commons- lang< / groupId>
< artifactId> commons- lang< / artifactId>
< version> 2.6 < / version>
< / dependency>
< dependency>
< groupId> com. alibaba< / groupId>
< artifactId> fastjson< / artifactId>
< version> 1.2 .58 < / version>
< / dependency>
< dependency>
< groupId> org. bouncycastle< / groupId>
< artifactId> bcprov- jdk16< / artifactId>
< version> 1.43 < / version>
< / dependency>
< / dependencies>
< build>
< finalName> demo< / finalName>
< plugins>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< / plugin>
< ! -- 热部署需要 -- >
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< configuration>
< fork> true < / fork> < ! -- 如果没有该配置,devtools不会生效 -- >
< / configuration>
< executions>
< execution>
< goals>
< goal> repackage< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>
< / plugins>
< resources>
< resource>
< directory> src/ main/ java< / directory>
< includes>
< include> * * * < / include>
< / includes>
< filtering> true < / filtering>
< / resource>
< / resources>
< / build>
< / project>
build标签下的resources配置影响到mybatis映射文件,静态文件及资源文件的打包
启动类
@SpringBootApplication
@ComponentScan ( basePackages = { "com.example.demo.*" } )
@ServletComponentScan ( basePackages = "com.example.demo.servlet" )
@MapperScan ( basePackages= "com.example.demo.mapper.*" , annotationClass= MyBatisRepository. class )
public class DemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure ( SpringApplicationBuilder application) {
return application. sources ( DemoApplication. class ) ;
}
public static void main ( String[ ] args) {
SpringApplication. run ( DemoApplication. class , args) ;
}
}
@ServletComponentScan扫描自定义servlet(@WebServlet注解的类)的路径 @MapperScan扫描mybatis的映射文件路径 @EnableWebMvc会使spring boot自动配置失效,尤其是静态文件路径会找不到!!!!参考博客 继承父类SpringBootServletInitializer,spring boot打成war包,部署到外部的tomcat服务器
properties配置
# 静态文件请求匹配方式
spring. mvc. static - path- pattern= / static * . xml
mybatis. config- location= classpath: config/ MyBatisConfiguration. xml
mybatis. type- aliases- package = com. example. demo. vo
spring boot 默认静态文件路径是static目录下,如果需要指定static子目录,需要配置spring.mvc.static-path-pattern=/static/**,启动类有@EnableWebMvc注解时,该配置失效,需要用编码方式继承WebMvcConfigurer类配置静态文件扫描路径 mybatis配置文件扫描路径,注意用/分割层级目录
编码方式配置web相关
使用注解@EnableWebMvc,完全忽略spring boot自动配置,由用户自定义web相关配置,编写配置类继承WebMvcConfigurer类
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers ( ViewControllerRegistry registry) {
registry. addViewController ( "/" ) . setViewName ( "login.do" ) ;
}
public void addResourceHandlers ( ResourceHandlerRegistry registry) {
registry. addResourceHandler ( "/static/**" )
. addResourceLocations ( "classpath:/static/" ) ;
}
}
@Retention ( RetentionPolicy. RUNTIME)
@Target ( ElementType. TYPE)
@Documented
@Import ( DelegatingWebMvcConfiguration. class )
public @interface EnableWebMvc {
}
servlet相关注解
@WebServlet @WebFilter @WebListener @ServletComponentScan(basePackages = “com.example.demo.servlet”) @WebInitParam 等同于spring mvc 中web.xml的servlet,filter,listener配置