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类中配置的:

 

spring整合mybatis,springMVC的0配置文件方式的更多相关文章

  1. 零基础学习java------40---------Maven(maven的概念,安装,maven在eclipse中使用),springboot(spring整合springmvc(注解),spring整合mybatis(常见的配置文件)),前端页面(bootstrap软件)

    一 maven 1. Maven的相关概念 1.1 项目开发中遇到的问题 (1)都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行? (2)为什么在我的机器上可以正常打包,而配置管理 ...

  2. spring 整合 mybatis 中数据源的几种配置方式

    因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...

  3. Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查

    之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...

  4. spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist

    spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...

  5. SPring整合Mybatis方式一

    Spring整合Mybatis 需要maven包: mysql-connector-java 5.1.47, mybatis 3.5.2, spring-webmvc 5.2.2.RELEASE, s ...

  6. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二

    接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...

  7. spring整合mybatis(hibernate)配置

    一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...

  8. Mybatis学习(六)————— Spring整合mybatis

    一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...

  9. Spring整合MyBatis 你get了吗?

    Spring整合MyBatis 1.整体架构dao,entity,service,servlet,xml 2..引入依赖 <dependencies> <dependency> ...

随机推荐

  1. 业务零影响!如何在Online环境中巧用MySQL传统复制技术【转】

    业务零影响!如何在Online环境中巧用MySQL传统复制技术 这篇文章我并不会介绍如何部署一个MySQL复制环境或keepalived+双主环境,因为此类安装搭建的文章已经很多,大家也很熟悉.在这篇 ...

  2. 将json转化为model

    /// <summary> /// 获取Json的Model /// </summary> /// <typeparam name="T">&l ...

  3. 总结OpenWrt系统基本操作方法

    1.OpenWrt系统编译好的固件位于哪个文件夹?root@ald888:/work/openwrt/trunk/bin/ramips# lsopenwrt-ramips-rt305x-mpr-a2- ...

  4. dotnet webservice处理数据量过大,ajax请求返回500错误解决方案

    ajax请求webservice返回json数据,数据规模过大时ajax请求会得到500的响应,webservice+ajax处理大规模的数据需要在web.config中进行如下配置: <sys ...

  5. dedecms mysql连接错误:#1040 - Too many connections

    mysql能登进去一下,点任何链接又跳出来,然后就登不上了 解决办法:检查mysql所在盘是否还有空间

  6. 最后的配置部分:LNMP+Tomcat

    Nginx与PHP部分 mkdir /www/php -p echo -e "<?php\n\tphpinfo();\n?>" > /www/php/index. ...

  7. Task 编程中的异常处理

    在 .Net 开发中, 使用 Task . Task<T> 进行异步编程是非常方便的, 但是在处理 Task 产生的异常时, 需要注意一个问题, 比如下面的代码: ? 1 2 3 4 5 ...

  8. web前端性能优化指南

    web前端性能优化指南 web前端性能优化指南 概述 1. PC优化手段在Mobile侧同样适用2. 在Mobile侧我们提出三秒种渲染完成首屏指标3. 基于第二点,首屏加载3秒完成或使用Loadin ...

  9. PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)

    树的遍历. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  10. Spring ---annotation (重点)--Resource, Component 重要!!!

    beans.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...