spring整合mybatis,springMVC的0配置文件方式
0配置文件的形式主要是采用spring3.0提供的@configuration注解和spring容器在启动的时候会加载实现了WebApplicationInitializer的类,并调用其onStartUp的方法的特性去实现.
具体做法如下:
1.建立MyWebAppInitializer去实现WebApplicationInitializer接口,并且去重写其onStartUp方法.实际上这个类取代了web.xml的配置.代码如下:
public class MyWebAppInitializer implements WebApplicationInitializer{ @Override public void onStartup(ServletContext servletContext) throws ServletException { //相当于在web.xml在配置spring启动用的ContextLoaderListener AnnotationConfigWebApplicationContext rootContext=new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); servletContext.addListener(new ContextLoaderListener(rootContext)); //相当于在web.xml在配置DispatcherServlet AnnotationConfigWebApplicationContext webContext=new AnnotationConfigWebApplicationContext(); webContext.register(WebConfig.class); Dynamic registration = servletContext.addServlet("dispatcher",new DispatcherServlet(webContext)); registration.setLoadOnStartup(1); registration.addMapping("/"); } }
2.建立WebConfig.class.这个类用于取代spring的配置文件springmvc.xml.代码如下:相关注解的解释写在注释中.
@EnableWebMvc //开启springmvc的配置 @Configuration //开启基于Java类的配置 @ComponentScan(basePackages="com.xyy.web") public class WebConfig extends WebMvcConfigurerAdapter{ //配置与dispatcherServlet相关联的bean @Bean//代表这是一个bean.spring容器会将其放在容器中. public ViewResolver viewResolver() { InternalResourceViewResolver resolver=new InternalResourceViewResolver(); resolver.setPrefix("/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true);//使得可以在jsp页面中可以通过${}访问bean return resolver; } //开启静态文件的访问. @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
3.建立AppConfig.class.它用于取代applicationContext.xml.这里我们使用了c3p0数据库连接池配合mybatis框架使用
@EnableAspectJAutoProxy//开启自动注解扫描 @EnableTransactionManagement//配置事务管理 @Configuration @ComponentScan(basePackages="com.xyy")//扫描注解 public class AppConfig { //配置c3p0数据源 @Bean public DataSource dataSource() { ComboPooledDataSource dataSource=new ComboPooledDataSource(); try { dataSource.setDriverClass("com.mysql.jdbc.Driver"); } catch (PropertyVetoException e) { e.printStackTrace(); } dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUser("root"); dataSource.setPassword("root"); return dataSource; } //配置SqlSessionFactoryBean @Bean public SqlSessionFactoryBean sqlSessionFactory() { SqlSessionFactoryBean bean=new SqlSessionFactoryBean(); bean.setConfigLocation(new ClassPathResource("SqlMapConfig.xml")); bean.setDataSource(dataSource()); return bean; } //配置开启Mapper扫描 @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer configurer=new MapperScannerConfigurer(); configurer.setBasePackage("com.xyy.mapper"); return configurer; } //配置开启DataSourceTransactionManager @Bean public DataSourceTransactionManager dataSourceTransactionManager() { DataSourceTransactionManager manager=new DataSourceTransactionManager(); manager.setDataSource(dataSource()); return manager; } }
4.至此,我们就可以正常的建立Controller类书写代码了.不过,不要忘记在对应的类上加上@controller,@service,@repository哦.
5.此外,为了防止会出现乱码的情况,我们最好再配上spring提供的CharacterEncodingFilter去解决乱码问题.方式如下,也是在MyWebAppInitializer类中配置的:
加油