Spring与Mybatis的整合之Mapper代理的整合方式

来源:互联网 发布:潍坊行知学校是高中吗 编辑:程序博客网 时间:2024/06/05 11:33

1.项目的目录


2.将Mapper和Mapper.xml放在同一个目录

                                          

3.applicationContext.xml的配置文件

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.   
  16.     <!-- 加载配置文件 -->  
  17.     <context:property-placeholder location="classpath:db.properties" />  
  18.     <!-- 数据库连接池 -->  
  19.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  20.         destroy-method="close">  
  21.         <property name="driverClassName" value="${jdbc.driver}" />  
  22.         <property name="url" value="${jdbc.url}" />  
  23.         <property name="username" value="${jdbc.username}" />  
  24.         <property name="password" value="${jdbc.password}" />  
  25.         <property name="maxActive" value="10" />  
  26.         <property name="maxIdle" value="5" />  
  27.     </bean>  
  28.   
  29.   
  30.     <!-- SqlsessionFactory -->  
  31.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  32.         <!-- 数据源 -->  
  33.         <property name="dataSource" ref="dataSource" />  
  34.         <!-- mybatis配置文件 -->  
  35.         <property name="configLocation" value="classpath:SqlMapConfig.xml" />  
  36.     </bean>  
  37.   
  38.   
  39.     <!-- 配置mapper -->  
  40.     <!-- MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象, 自动创建到spring容器中,bean的id是mapper的类名(首字母小写) -->  
  41.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  42.         <!-- 配置扫描包的路径 如果要扫描多个包,中间使用半角逗号分隔 -->  
  43.         <property name="basePackage" value="com.mybatis.mapper" />  
  44.         <!-- 使用sqlSessionFactoryBeanName,这个要注意 -->  
  45.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  
  46.     </bean>  
  47.   
  48.     <!--配置Service -->  
  49.     <bean id="userService" class="com.mybatis.service.UserService">  
  50.         <property name="userMapper" ref="userMapper" />  
  51.     </bean>  
  52.   
  53.     <!-- 加入事务管理 -->  
  54.     <!-- #############5. Spring声明式事务管理配置############### -->  
  55.     <!-- 5.1 配置事务管理器类 -->  
  56.     <bean id="txManager"  
  57.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  58.         <property name="dataSource" ref="dataSource"></property>  
  59.     </bean>  
  60.   
  61.     <!-- 5.2 配置事务增强(如果管理事务?) -->  
  62.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  63.         <tx:attributes>  
  64.             <tx:method name="find*" read-only="true" />  
  65.             <tx:method name="insert*" propagation="REQUIRED"  />  
  66.             <tx:method name="delete*" propagation="REQUIRED" />  
  67.             <tx:method name="update*" propagation="REQUIRED" />  
  68.             <tx:method name="*" read-only="true" />  
  69.         </tx:attributes>  
  70.     </tx:advice>  
  71.   
  72.     <!-- 5.3 Aop配置: 拦截哪些方法(切入点表表达式) + 应用上面的事务增强配置 -->  
  73.     <aop:config>  
  74.         <aop:pointcut expression="execution(* com.mybatis.service.*.*(..))"  
  75.             id="pt" />  
  76.         <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />  
  77.     </aop:config>  
  78.   
  79. </beans>  

4.db.properties

[javascript] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. jdbc.driver=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://localhost:3306/mybatistest  
  3. jdbc.username=root  
  4. jdbc.password=1234  

5.源码下载


1 0
原创粉丝点击