基于注解的SpringMVC大致开发流程
首先我们需要明确,基于注解的开发,Maven项目对于web.xml文件不是必要的,所以,我们需要再pom.xml中进行配置:
1 <properties> 2 <failOnMissingWebXml>false</failOnMissingWebXml> 3 <maven.compiler.source>1.8</maven.compiler.source> 4 <maven.compiler.target>1.8</maven.compiler.target> 5 </properties>
基于注解的开发是比较简易的,首先是mvc开发过程中需要初始加载的内容写在spring初始化的类中:
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[] { SpringMvcConfigurer.class }; } @Override protected String[] getServletMappings() { return new String[] { "*.do"}; } @Override protected Filter[] getServletFilters() { // CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); // characterEncodingFilter.setEncoding("utf-8"); return new Filter[] { new CharacterEncodingFilter("utf-8") }; } }
其中:
-
getRootConfigClasses()
-
getServletConfigClasses()
:获取SpringMVC的配置类,SpringMVC的配置类可以自定义,且必须实现WebMvcConfigurer
接口; -
getServletMappings()
:获取框架所处理的请求的路径,假设所有以.do
为后缀的请求都需要被SpringMVC框架处理,则可以配置为*.do
。
其次,我们需要做就是MVC的配置类的开发:
1 @ComponentScan("cn.tedu.spring") 2 @Configuration 3 @EnableWebMvc 4 public class SpringMvcConfigurer implements WebMvcConfigurer { 5 private String characterEncoding = "utf-8"; 6 7 /** 8 * 添加拦截器链 9 */ 10 public void addInterceptors(InterceptorRegistry registry) { 11 // 注意:配置拦截器作用的相关路径时,路径值必须使用 / 作为第1个字符 12 LoginInterceptor interceptor = new LoginInterceptor(); 13 registry.addInterceptor(interceptor) 14 .addPathPatterns("/hello.do") 15 .excludePathPatterns(); 16 } 17 18 @Bean 19 public ViewResolver configViewResolver() { 20 // ------------------------------------------------ 21 // 通常使用的模版解析器有ClassLoaderTemplateResolver和ServletContextTemplateResolver,在实际使用时,任选其一即可! 22 // 当使用ClassLoaderTemplateResolver时,会自动到项目的src/main/resources下查找视图文件 23 // 当使用ServletContextTemplateResolver时,会自动到项目的webapp下查找视图文件 24 // ------------------------------------------------ 25 27 //以下代码中,前缀的配置值是/templates/,后缀的配置值是.html,当方法返回的字符串 demo 时,将拼接得到 /templates/demo.html,则会以这个文件作为视图文件 28 ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); 29 templateResolver.setCharacterEncoding(characterEncoding); 30 templateResolver.setTemplateMode("HTML"); 31 templateResolver.setCacheable(false);
//setPrefix()方法表示配置前缀 32 templateResolver.setPrefix("/templates/");
//setSuffix()表示配置后缀 33 templateResolver.setSuffix(".html"); 34 35 SpringTemplateEngine templateEngine = new SpringTemplateEngine(); 36 templateEngine.setTemplateResolver(templateResolver); 37 38 ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 39 viewResolver.setCharacterEncoding(characterEncoding); 40 viewResolver.setTemplateEngine(templateEngine); 41 return viewResolver; 42 } 43 }
下面就需要我们来配置spring整合数据库的java类了:
1 @PropertySource("jdbc.properties") 2 @Configuration 3 @MapperScan("cn.tedu.yonghe.mapper") 4 public class SpringConfigurer { 5 6 @Value("${mysql.url}") 7 private String url; 8 @Value("${mysql.driver}") 9 private String driver; 10 @Value("${mysql.username}") 11 private String username; 12 @Value("${mysql.password}") 13 private String password; 14 @Value("${mysql.initialSize}") 15 private Integer initialSize; 16 @Value("${mysql.maxTotal}") 17 private Integer maxTotal; 18 @Value("${mybatis.mapper-locations}") 19 private Resource[] mapperLocations; 20 21 @Bean 22 public DataSource dataSource() { 23 BasicDataSource dataSource = new BasicDataSource(); 24 dataSource.setUrl(url); 25 dataSource.setDriverClassName(driver); 26 dataSource.setUsername(username); 27 dataSource.setPassword(password); 28 dataSource.setInitialSize(initialSize); 29 dataSource.setMaxTotal(maxTotal); 30 return dataSource; 31 } 32 33 @Bean 34 public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) { 35 SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 36 bean.setDataSource(dataSource); 37 bean.setMapperLocations(mapperLocations); 38 return bean; 39 } 40 }
其中,jdbc.properties文件内容:
1 mysql.url=jdbc:mysql://localhost:3306/tedu_ums?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai 2 mysql.driver=com.mysql.cj.jdbc.Driver 3 mysql.username=root 4 mysql.password=root 5 mysql.initialSize=2 6 mysql.maxTotal=10 7 8 mybatis.mapper-locations=classpath:mappers/*.xml
附注:SpringMVC开发过程中需要的jar包依赖
<!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.2.RELEASE</version> </dependency> <!-- Spring JDBC依赖,必须与其它Spring依赖使用完全相同的版本 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.2.RELEASE</version> </dependency> <!-- Mybatis框架 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.0</version> </dependency> <!-- MyBatis整合Spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.1</version> </dependency> <!-- Thymeleaf --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency> <!-- Thymeleaf整合Spring --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.11.RELEASE</version> </dependency> <!-- 连接MySQL数据库的依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
setPrefix()方法表示配置“前缀”