<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.22version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.4version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.4version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.19version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
dependencies>
getRootConfigClasses Spring配置
getServletConfigClasses SpringMvc配置
getServletMappings SpringMvc的DispathServlet映射路径
getServletFilters SpringMvc字符过滤器
package com.config;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
public class ApplicationConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{ServletConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return new Filter[]{characterEncodingFilter};
}
}
@Configuration 标识注解类
@EnableWebMvc 开启注解扫描@ComponentScan(basePackages = “com”, includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)}) 只扫描Controller注解
viewResolver 视图解析器
addViewControllers 静态映射
extendMessageConverters 处理中文乱码
package com.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.nio.charset.Charset;
import java.util.List;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com", includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)})
public class ServletConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver jsp = new InternalResourceViewResolver();
jsp.setPrefix("/WEB-INF/view/");
jsp.setSuffix(".jsp");
return jsp;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (int i = 0;i<converters.size();i++){
if(converters.get(i) instanceof StringHttpMessageConverter){
StringHttpMessageConverter httpMessageConverter = (StringHttpMessageConverter) converters.get(i);
httpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
}
}
}
}
package com.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.io.IOException;
import java.util.ResourceBundle;
@SuppressWarnings("all")
@ComponentScan(basePackages = "com", excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)})
@PropertySource("classpath:datasource.properties")
@EnableTransactionManagement
public class RootConfig {
/**
* 数据源
*
* @return
*/
@Bean("druidDataSource")
public DruidDataSource druidDataSource(@Value("${spring.datasource.username}") String username,
@Value("${spring.datasource.password}") String password,
@Value("${spring.datasource.driver-class-name}") String drivreclassname,
@Value("${spring.datasource.url}") String url) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(drivreclassname);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Autowired @Qualifier("druidDataSource") DruidDataSource druidDataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(druidDataSource);
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource[] mapperResources = patternResolver.getResources("classpath:mapper/*.xml");
sqlSessionFactoryBean.setMapperLocations(mapperResources);
return sqlSessionFactoryBean.getObject();
}
@Bean("mapperScannerConfigurer")
public MapperScannerConfigurer mapperScannerConfigurer(@Autowired @Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws IOException {
ResourceBundle bundle = ResourceBundle.getBundle("datasource.properties");
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactory(sqlSessionFactory);
mapperScannerConfigurer.setBasePackage("com.mapper");
return mapperScannerConfigurer;
}
@Bean("transactionManager")
public TransactionManager transactionManager(@Autowired @Qualifier("druidDataSource") DruidDataSource druidDataSource){
TransactionManager transactionManager = new DataSourceTransactionManager(druidDataSource);
return transactionManager;
}
}