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

spring整合mybatis,springMVC的0配置文件方式的更多相关文章
- 零基础学习java------40---------Maven(maven的概念,安装,maven在eclipse中使用),springboot(spring整合springmvc(注解),spring整合mybatis(常见的配置文件)),前端页面(bootstrap软件)
一 maven 1. Maven的相关概念 1.1 项目开发中遇到的问题 (1)都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行? (2)为什么在我的机器上可以正常打包,而配置管理 ...
- spring 整合 mybatis 中数据源的几种配置方式
因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...
- Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查
之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...
- 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 ...
- SPring整合Mybatis方式一
Spring整合Mybatis 需要maven包: mysql-connector-java 5.1.47, mybatis 3.5.2, spring-webmvc 5.2.2.RELEASE, s ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
- spring整合mybatis(hibernate)配置
一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...
- Mybatis学习(六)————— Spring整合mybatis
一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...
- Spring整合MyBatis 你get了吗?
Spring整合MyBatis 1.整体架构dao,entity,service,servlet,xml 2..引入依赖 <dependencies> <dependency> ...
随机推荐
- scroll、scrollBy和 scrollTo三种方法定位滚动条位置
在默认情况下,页面加载完后默认滚动在最顶端,有些时候我们需要在页面打开后,定位滚动条的位置,比如,横向和纵向滚动条居中,实现页面滚动的方法有三种:scroll.scrollBy和 scrollTo,三 ...
- git:解决The current branch is not configured for pull No value for key branch.master.merge found in config
网上多半都是命令行下的解决方案,我用的是EGit,所以要在eclipse里(我的版本是kepler)把下面这句话添加到配置文件中. Window->Preference->Team-> ...
- zf-关于表单不能提交的bug修改
因为使用onclick="submitForm();" 函数提交的 就表示 这里面有js代码 js代码 里面使用document.from1.submit()提交的 所以from ...
- 【贪心】时空定位I
[贪心]时空定位I 题目描述 张 琪曼已经确定了李旭琳在一个长为20千米,宽为2千米的空间,她要在横中心线上放置半径为Ri的定位装置,每个定位装置的效果都会让以它为中心的半径为实 数Ri(0<R ...
- 开心的金明<0-1背包>
题意:0-1背包经典题: 不多述,直接上代码: 1.二维数组表示法: #include<cstdio> #include<iostream> #include<algor ...
- DOS日期和时间 - Robin Hu的专栏 - 博客频道 - CSDN.NET
body { font-family: Microsoft YaHei UI,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-ser ...
- CSS3的background-size
DEMO一.background-size:auto; 我来看第一个DEMO,在前面的DEMO上加上和个class名为"backgroundSizeAuto",在这个Demo上我们 ...
- 编译内核启用iptables及netfilter
在Network Packet Filtering Framework(Netfilter)一节中还有两个额外的配置节——Core Netfilter Configuration(核心Netfilte ...
- Java基础知识(一) 自增、自减运算符
.d1 { border-style: none } .d2 { border-style: solid } .d3 { border-style: dotted } .d4 { border-sty ...
- selenium自动化测试
在线安装testNg,现在分享一个离线安装的方法,及安装文件,希望能够帮到大家.1.下载附件,并解压.(后面有),或者百度网盘http://pan.baidu.com/s/1i3y1QtR2.将解压后 ...