MyBatis 整合 Spring
开发环境:
System:Windows
WebBrowser:IE6+、Firefox3+
JavaEE Server:tomcat5.0.2.8、tomcat6
IDE:eclipse、MyEclipse 8
Database:MySQL
开发依赖库:
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 内容,内容如下:
<!-- 加载 Spring 容器配置 -->
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</
listener-class>
</listener>
<!-- 设置 Spring 容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<!-- 配置 Spring 核心控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 解决工程编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-
class>org.springframework.web.filter.CharacterEncodingFilter</
filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、在 WEB-INF 目录中添加 dispatcher.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-
3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-
3.0.xsd">
<!-- 注解探测器 -->
<context:component-scan base-package="com.hoo"/>
<!-- annotation 默认的方法映射适配器 -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnot
ationHandlerMapping" />
<bean id="handlerAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMe
thodHandlerAdapter" />
</beans>
4、在 src 目录下添加 applicationContext-common.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-
3.0.xsd
http://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 ">
<!-- 配置 DataSource 数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource
">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://10.0.0.131:3306/ash2"/>
<property name="username" value="dev"/>
<property name="password" value="dev"/>
</bean>
<!-- 配置 SqlSessionFactoryBean -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation"
value="classpath:mybatis.xml"/>
<!-- mapper
和 resultmap
配置路径 -->
<property name="mapperLocations">
<list>
<!-- 表示在 com.hoo.resultmap 包或以下所有目录中,以-
resultmap.xml 结尾所有文件 -->
<value>classpath:com/hoo/resultmap/**/*-
resultmap.xml</value>
<value>classpath:com/hoo/entity/*-
resultmap.xml</value>
<value>classpath:com/hoo/mapper/**/*-
mapper.xml</value>
</list>
</property>
</bean>
<!-- 单独配置一个 Mapper; 这种模式就是得给每个 mapper
接口配置一个 bean
-->
<!--
<bean id="accountMapper"
class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.hoo.mapper.AccountMapper" />
<property name="sqlSessionFactory"
ref="sqlSessionFactory" />
</bean>
<bean id="companyMapper"
class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.hoo.mapper.CompanyMapper" />
<property name="sqlSessionFactory"
ref="sqlSessionFactory" />
</bean>
-->
<!-- 通过扫描的模式,扫描目录在 com/hoo/mapper
目录下,所有的 mapper
都
继承 SqlMapper 接口的接口, 这样一个 bean 就可以了 -->
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hoo.mapper"/>
<property name="markerInterface"
value="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 内容如下:
package com.hoo.mapper;
/**
* <b>function:</b>所有的 Mapper
继承这个接口
* @author hoojo
* @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
*/
- 1
- 2
前往页