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> ...
随机推荐
- wamp开机自动启动
控制面板--管理工具--服务 将其中的wampapache和wampmysql的属性均改为“自动”即可.
- DP CF 319 div1B
http://codeforces.com/contest/319/problem/B 题目大意: 有删除操作,每次都删除数组右边比自己小的.且紧挨着自己的数字.问最小需要删除几次. 思路: 我们定义 ...
- 滑雪(ski)
滑雪(ski) 题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- Android------三种监听OnTouchListener、OnLongClickListener同时实现即其中返回值true或者false的含义
引自:http://5200415.blog.51cto.com/3851969/1003113 android应用中常用的监听OnTouchListener.OnClickListener.OnLo ...
- Android Studio调试功能使用总结---转
Android Studio调试功能使用总结[转] 这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...
- C#对象序列化笔记
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- python数组查找算法---bisect二分查找插入
1 实例 这个模块只有几个函数, 一旦决定使用二分搜索时,立马要想到使用这个模块 [python] view plaincopyprint? import bisect L = [1,3,3,6,8, ...
- Cow Hopscotch
Cow Hopscotch 题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have inv ...
- ZjDroid工具介绍及脱壳详细示例
前提条件: 1.Root手机一部 2.需要通过Xposed installer(http://dl.xposed.info/latest.apk)安装Xposed Framework; 一.ZjDro ...
- PAT 天梯赛 L1-009 N个数求和
模拟题 题目链接 题解 每次将两个分数进行相加,到最后再将结果化成带分数.主要考察的最大公约数与最小公倍数. 代码如下: #include<cstdio> #include<cstd ...