SpringMybatis整合文档

更新时间:2023-11-05 06:23:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

MyBatis整合Spring

开发环境:

System:Windows

WebBrowser:IE6+、Firefox3+

JavaEE Server:tomcat5.0.2.8、tomcat6 IDE:eclipse、MyEclipse 8 Database:MySQL 11

开发依赖库:

JavaEE5、Spring 3.0.5、Mybatis 3.0.4、myBatis-spring-1.0、junit4.8.2

Email:hoojo_@126.com

Blog:http://blog.csdn.net/IBM_hoojo http://hoojo.cnblogs.com/

1、 首先新建一个WebProject 命名为MyBatisForSpring,新建项目时,使用JavaEE5

的lib库。然后手动添加需要的jar包,所需jar包如下:

2、 添加spring的监听及springMVC的核心Servlet,web.xml内容,内容如下: org.springframework.web.context.ContextLoaderListener

characterEncodingFilter /*

characterEncodingFilter

org.springframework.web.filter.CharacterEncoding

encoding UTF-8 dispatcher *.do

dispatcher

org.springframework.web.servlet.DispatcherServl

contextConfigLocation /WEB-INF/dispatcher.xml contextConfigLocation

classpath*:applicationContext-*.xml

et

1

Filter

3、 在WEB-INF目录中添加dispatcher.xml,内容如下: .mvc.annotation.DefaultAnnotationHandlerMapping\/> 4、 在src目录下添加applicationContext-common.xml,内容如下:

http://www.springframework.org/schema/beans/spring-beans-3.0.xhttp://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd \>

xmlns:aop=\ xmlns:tx=\ xmlns:xsi=\

xsi:schemaLocation=\

.DriverManagerDataSource\>

nFactoryBean\>

/> />

-->

\/> 上面的配置最先配置的是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内容如下: package com.hoo.mapper; /** * function:所有的Mapper继承这个接口 * @authorhoojo * @createDate 2011-4-12 下午04:00:31 * @file SqlMapper.java * @package com.hoo.mapper * @project MyBatisForSpring * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ publicinterfaceSqlMapper { } 6、 实体类和ResultMap.xml package com.hoo.entity; import java.io.Serializable; import javax.persistence.Entity; @Entity publicclass Account implements Serializable {

本文来源:https://www.bwwdw.com/article/p7a2.html

Top