MyBatis整合Spring开发环境:System:WindowsWebBrowser:IE6+、Firefox3+JavaEEServer:tomcat5.0.2.8、tomcat6IDE:eclipse、MyEclipse8Database:MySQL开发依赖库:JavaEE5、Spring3.0.5、Mybatis3.0.4、myBatis-spring-1.0、junit4.8.2Email:hoojo_@126.comBlog:://hoojo.cnblogs.com/1、首先新建一个WebProject命名为MyBatisForSpring,新建项目时,使用JavaEE5的lib库。然后手动添加需要的jar包,所需jar包如下:2、添加spring的监听及springMVC的核心Servlet,web.xml内容,内容如下:!--加载Spring容器配置--listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener!--设置Spring容器加载配置文件路径--context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath*:applicationContext-*.xml/param-value/context-param!--配置Spring核心控制器--servletservlet-namedispatcher/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/dispatcher.xml/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-namedispatcher/servlet-nameurl-pattern*.do/url-pattern/servlet-mapping!--解决工程编码过滤器--filterfilter-namecharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-param/filterfilter-mappingfilter-namecharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping3、在WEB-INF目录中添加dispatcher.xml,内容如下:?xmlversion=1.0encoding=UTF-8?beansxmlns=:context=:xsi=:schemaLocation=://://://!--注解探测器--context:component-scanbase-package=com.hoo/!--annotation默认的方法映射适配器--beanid=handlerMappingclass=org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping/beanid=handlerAdapterclass=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter//beans4、在src目录下添加applicationContext-common.xml,内容如下:?xmlversion=1.0encoding=UTF-8?beansxmlns=:aop=:tx=:xsi=:schemaLocation=://://://://://!--配置DataSource数据源--beanid=dataSourceclass=org.springframework.jdbc.datasource.DriverManagerDataSourcepropertyname=driverClassNamevalue=com.mysql.jdbc.Driver/propertyname=urlvalue=jdbc:mysql://10.0.0.131:3306/ash2/propertyname=usernamevalue=dev/propertyname=passwordvalue=dev//bean!--配置SqlSessionFactoryBean--beanid=sqlSessionFactoryclass=org.mybatis.spring.SqlSessionFactoryBeanpropertyname=dataSourceref=dataSource/propertyname=configLocationvalue=classpath:mybatis.xml/!--mapper和resultmap配置路径--propertyname=mapperLocationslist!--表示在com.hoo.resultmap包或以下所有目录中,以-resultmap.xml结尾所有文件--valueclasspath:com/hoo/resultmap/**/*-resultmap.xml/valuevalueclasspath:com/hoo/entity/*-resultmap.xml/valuevalueclasspath:com/hoo/mapper/**/*-mapper.xml/value/list/property/bean!--单独配置一个Mapper;这种模式就是得给每个mapper接口配置一个bean--!--beanid=accountMapperclass=org.mybatis.spring.mapper.MapperFactoryBeanpropertyname=mapperInterfacevalue=com.hoo.mapper.AccountMapper/propertyname=sqlSessionFactoryref=sqlSessionFactory//beanbeanid=companyMapperclass=org.mybatis.spring.mapper.MapperFactoryBeanpropertyname=mapperInterfacevalue=com.hoo.mapper.CompanyMapper/propertyname=sqlSessionFactoryref=sqlSessionFactory//bean--!--通过扫描的模式,扫描目录在com/hoo/mapper目录下,所有的mapper都继承SqlMapper接口的接口,这样一个bean就可以了--beanclass=org.mybatis.spring.mapper.MapperScannerConfigurerpropertyname=basePackagevalue=com.hoo.mapper/propertyname=markerInterfacevalue=com.hoo.mapper.SqlMapper//bean/beans上面的配置最先配置的是DataSource,这里采用的是jdbc的DataSource;然后是SqlSessionFactoryBean,这个配置比较关键。SqlSessionFactoryBean需要注入DataSource数据源,其次还要设置configLocation也就是mybatis的xml配置文件路径,完成一些关于mybatis的配置,如settings、mappers、plugin等;如果使用mapperCannerConfigurer模式,需要设置扫描根路径也就是你的mybatis的mapper接口所在包路径;凡是markerInterface这个接口的子接口都参与到这个扫描,也就是说所有的mapper接口继承这个SqlMapper。如果你不使用自己的transaction事务,就使用MapperScannerConfigurer来完成SqlSession的打开、关闭和事务的回滚操作。在此期间,出现数据库操作的如何异常都会被转换成DataAccessException,这个异常是一个抽象的类,继承RuntimeException;5、SqlMapper内容如下:packagecom.hoo.mapper;/***bfunction:/b所有的Mapper继承这个接口*@authorhoojo*@createDate2011-4-12下午04:00:31*@fileSql