`

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

阅读更多

下面来看Spring的配置:

1、applicationContext.xml

 

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xmlns:aop="http://www.springframework.org/schema/aop"  
  5.   xmlns:tx="http://www.springframework.org/schema/tx"  
  6.   xsi:schemaLocation="   
  7.   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  9.   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">      
  10.     <!-- DAO配置 -->  
  11.     <import resource="daoContext.xml" />  
  12.        
  13.     <!-- IoC配置 -->  
  14.     <import resource="iocContext.xml" />  
  15.        
  16.     <!-- MVC配置 -->  
  17.     <import resource="mvcContext.xml" />  
  18.        
  19.     <!-- AOP配置 -->  
  20.     <import resource="aopContext.xml" />  
  21. </beans>  
<?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代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xsi:schemaLocation="   
  8.   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  9.   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  10.   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  11.   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  12.   http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  13.   
  14.     <!-- 配置数据源 -->  
  15.     <bean id="DataSource"  
  16.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  17.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  18.         <property name="url" value="jdbc:mysql://localhost:3306/myfristdb" />  
  19.         <property name="username" value="root" />  
  20.         <property name="password" value="admin" />  
  21.     </bean>  
  22.   
  23.     <!-- Spring、MyBatis的整合,需要在 Spring 应用上下文中定义至少两样东西:一个SqlSessionFactory和至少一个数据映射器类(UserMapper->iocContext.xml)。 -->  
  24.     <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  25.         <property name="configLocation" value="classpath:SqlMapConfig.xml" />  
  26.         <property name="dataSource" ref="DataSource" />  
  27.     </bean>  
  28.        
  29.     <!-- 配置事务管理器 -->  
  30.     <bean id="TransactionManager"  
  31.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  32.         <property name="dataSource" ref="DataSource" />  
  33.     </bean>  
  34. </beans>  
<?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"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" 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" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<property name="dataSource" ref="DataSource" />
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="TransactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="DataSource" />
	</bean>
</beans>

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

3、iocContext.xml

 

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xmlns:aop="http://www.springframework.org/schema/aop"  
  5.   xmlns:tx="http://www.springframework.org/schema/tx"  
  6.   xsi:schemaLocation="   
  7.   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  9.   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.     <!-- 数据映射器类  mapper bean -->  
  11.     <bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  12.         <property name="sqlSessionFactory" ref="SqlSessionFactory" />  
  13.         <!-- 注意指定的映射器类必须是一个接口,而不是具体的实现类  -->  
  14.         <property name="mapperInterface" value="com.hl.usersmanager.dao.IUserMapper" />  
  15.     </bean>  
  16.        
  17.     <bean id="LoginMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  18.         <property name="sqlSessionFactory" ref="SqlSessionFactory" />  
  19.         <property name="mapperInterface" value="com.hl.usersmanager.dao.ILoginMapper" />  
  20.     </bean>  
  21. </beans>  
<?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" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="SqlSessionFactory" />
		<!-- 注意指定的映射器类必须是一个接口,而不是具体的实现类  -->
	    <property name="mapperInterface" value="com.hl.usersmanager.dao.IUserMapper" />
	</bean>
	
	<bean id="LoginMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" 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代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.     xsi:schemaLocation="   
  9.   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  10.   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  11.   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  12.   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  13.   http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  14.   
  15.     <!-- 参考资料:http://code.google.com/p/bounding/wiki/SpringMVC3 -->  
  16.        
  17.     <!-- 国际化配置 参考:http://hi.baidu.com/sonmeika/blog/item/8069b2dd7db1c9395882dd29.html    
  18.     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">  
  19.     </bean> -->  
  20.        
  21.     <!--配置视图解析器-->  
  22.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  23.         <property name="prefix" value="/WEB-INF/page/"/>     
  24.         <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->  
  25.         <property name="viewClass">  
  26.             <value>org.springframework.web.servlet.view.InternalResourceView</value>  
  27.         </property>  
  28.     </bean>  
  29.        
  30.     <!-- 注解支持 -->     
  31.     <mvc:annotation-driven/>  
  32.        
  33.     <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->  
  34.     <context:component-scan base-package="com.hl.usersmanager">  
  35.         <!-- 允许定义过滤器将基包下的某些类纳入或排除   
  36.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->  
  37.     </context:component-scan>  
  38.        
  39.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射    
  40.     <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> -->  
  41.   
  42. </beans>  
<?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" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
	</bean> -->
	
	<!--配置视图解析器-->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" 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 class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> -->

</beans>

 这部分是Spring mvc的配置。

viewResolver视图解析器是必须的。其属性prefix表示某个视图指向到那个路径,suffix属性表示视图的后缀(如果配置为jsp则表示该视图使用jsp页面进行渲染)。例如某个Controller返回一个login视图,根据当前的配置,表示该视图使用/WEB-INF/page/login.jsp进行渲染。这一点跟struts2有点像.

由于我们使用了注解的方式声明Controller,所以我们不再需要在Spring中配置Controller的bean。首先,开启注解支持,然后知道需要扫描注解的路径。

5、aopContext.xml

 

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xmlns:aop="http://www.springframework.org/schema/aop"  
  5.   xmlns:tx="http://www.springframework.org/schema/tx"  
  6.   xsi:schemaLocation="   
  7.   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  9.   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.        
  11.     <!-- aop注解支持 -->  
  12.     <aop:aspectj-autoproxy/>  
  13.        
  14.     <!--   
  15.     <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />-->  
  16.        
  17.     <!-- 拦截器 -->  
  18.     <bean id="UserInterceptor" class="com.hl.usersmanager.controller.aop.UserInterceptor"></bean>  
  19. </beans>  
<?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 class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />-->
	
	<!-- 拦截器 -->
	<bean id="UserInterceptor" class="com.hl.usersmanager.controller.aop.UserInterceptor"></bean>
</beans>
 这里是Spring aop的配置。首先需要开启注解支持。.AnnotationAwareAspectJAutoProxyCreator可以自动完成aop 注解类的加载,但测试时发现跟spring mvc似乎有些冲突,没有测试成功。好吧,只能老老实实的把每个拦截器bean配置起来。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics