SpringMVC
2.1MVC配置类
-
1.Spring配置类
@Configuration @ComponentScan({"com.itheima.service"}) @PropertySource("classpath:jdbc.properties") //扫描resources配置文件 @Import({JdbcConfig.class,MyBatisConfig.class}) //扫描读取配置文件的配置类 @EnableTransactionManagement //开启事务管理 public class SpringConfig { }
-
2.SpringMvc配置类
@Configuration @ComponentScan({"com.example.controller","com.example.config"}) @EnableWebMvc //开启mvc支持功能,例如json转换 public class SpringMvcConfig { }
-
3.Mvc加载入口配置类
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { //加载Spring配置类 protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringConfig.class}; } //加载SpringMVC配置类 protected Class<?>[] getServletConfigClasses() { return new Class[]{SpringMvcConfig.class}; } //设置SpringMVC请求地址拦截规则 protected String[] getServletMappings() { return new String[]{"/"}; } //设置post请求中文乱码过滤器 @Override protected Filter[] getServletFilters() { CharacterEncodingFilter filter = new CharacterEncodingFilter(); filter.setEncoding("utf-8"); return new Filter[]{filter}; } }
-
4.Mvc资源支持配置类
@Configuration public class SpringMvcSupport extends WebMvcConfigurationSupport { @Autowired private ProjectInterceptor projectInterceptor; //放行静态资源 @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/pages/**").addResourceLocations("/pages/"); } //添加拦截器 @Override protected void addInterceptors(InterceptorRegistry registry) { //配置拦截器 registry.addInterceptor(projectInterceptor).addPathPatterns("/books" ,"/books/*"); } }
-
5.Jdbc配置类
public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public DataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } //添加事务管理平台 @Bean public PlatformTransactionManager transactionManager(DataSource dataSource){ DataSourceTransactionManager ds = new DataSourceTransactionManager(); ds.setDataSource(dataSource); return ds; } }
-
6.Mybatis配置类
public class MyBatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setTypeAliasesPackage("com.itheima.domain"); return factoryBean; } @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.itheima.dao"); return msc; } }
-
7.Jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm_db jdbc.username=root jdbc.password=root
2.2MVC层注解的使用
-
ComponentScan:excludeFilters属性:设置扫描加载bean时,排除的过滤规则
@ComponentScan(value="com.example", excludeFilters=@ComponentScan.Filter( type = FilterType.ANNOTATION, classes = Controller.class ) )