首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

Spring3+mybatis+mysql整合详解(3)

2013-11-08 
Spring3+mybatis+mysql整合详解(三)下面来看Spring的配置:1、applicationContext.xml?xml version1.0 e

Spring3+mybatis+mysql整合详解(三)

下面来看Spring的配置:

1、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:aop="http://www.springframework.org/schema/aop"  xmlns:tx="http://www.springframework.org/schema/tx"  xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!-- DAO配置 --><import resource="daoContext.xml" /><!-- IoC配置 --><import resource="iocContext.xml" /><!-- MVC配置 --><import resource="mvcContext.xml" /><!-- AOP配置 --><import resource="aopContext.xml" /></beans>

?2、daoContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 配置数据源 --><bean id="DataSource"value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/myfristdb" /><property name="username" value="root" /><property name="password" value="admin" /></bean><!-- Spring、MyBatis的整合,需要在 Spring 应用上下文中定义至少两样东西:一个SqlSessionFactory和至少一个数据映射器类(UserMapper->iocContext.xml)。 --><bean id="SqlSessionFactory" value="classpath:SqlMapConfig.xml" /><property name="dataSource" ref="DataSource" /></bean><!-- 配置事务管理器 --><bean id="TransactionManager"ref="DataSource" /></bean></beans>

?该部分为Mybatis和Spring3的整合配置,包括数据源、SqlSessionFactory及事务管理器的配置。

3、iocContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:aop="http://www.springframework.org/schema/aop"  xmlns:tx="http://www.springframework.org/schema/tx"  xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!-- 数据映射器类  mapper bean --><bean id="UserMapper" ref="SqlSessionFactory" /><!-- 注意指定的映射器类必须是一个接口,而不是具体的实现类  -->    <property name="mapperInterface" value="com.hl.usersmanager.dao.IUserMapper" /></bean><bean id="LoginMapper" ref="SqlSessionFactory" />    <property name="mapperInterface" value="com.hl.usersmanager.dao.ILoginMapper" /></bean></beans>

?这部分是Dao层bean的配置。注意,这里bean的class并非直接指向dao层的类,而是指向org.mybatis.spring.mapper.MapperFactoryBean,并将dao层接口IUserMapper作为MapperFactoryBean的mapperInterface属性。

mybatis会自动帮我们创建一个代理类,执行IUserMapper里面的方法。也就是说,只要我们写好了mybatis的SqlMapConfig.xml,dao层只需要一个接口Mapper就行了,甚至连实现类都不要,因为myBatis自动帮我们完成。强大吧!

当然必须为MapperFactoryBean指定一个sqlSessionFactory,那就是前面我们在daoContext.xml中配置的SqlSessionFactory。

4、mvcContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 参考资料:http://code.google.com/p/bounding/wiki/SpringMVC3 --><!-- 国际化配置 参考:http://hi.baidu.com/sonmeika/blog/item/8069b2dd7db1c9395882dd29.html <bean id="localeResolver" value="/WEB-INF/page/"/>      <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  --><property name="viewClass"><value>org.springframework.web.servlet.view.InternalResourceView</value></property></bean><!-- 注解支持 -->  <mvc:annotation-driven/><!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --><context:component-scan base-package="com.hl.usersmanager"><!-- 允许定义过滤器将基包下的某些类纳入或排除<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --></context:component-scan><!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射     <bean name="code"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:aop="http://www.springframework.org/schema/aop"  xmlns:tx="http://www.springframework.org/schema/tx"  xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!-- aop注解支持 --><aop:aspectj-autoproxy/><!-- <bean />--><!-- 拦截器 --><bean id="UserInterceptor" class="com.hl.usersmanager.controller.aop.UserInterceptor"></bean></beans>
?这里是Spring aop的配置。首先需要开启注解支持。.AnnotationAwareAspectJAutoProxyCreator可以自动完成aop 注解类的加载,但测试时发现跟spring mvc似乎有些冲突,没有测试成功。好吧,只能老老实实的把每个拦截器bean配置起来。

热点排行