Spring与Mybatis的整合之Mapper代理的整合方式
来源:互联网 发布:潍坊行知学校是高中吗 编辑:程序博客网 时间:2024/06/05 11:33
1.项目的目录
2.将Mapper和Mapper.xml放在同一个目录
3.applicationContext.xml的配置文件
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- 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-3.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
-
-
- <context:property-placeholder location="classpath:db.properties" />
-
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- <property name="maxActive" value="10" />
- <property name="maxIdle" value="5" />
- </bean>
-
-
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="dataSource" ref="dataSource" />
-
- <property name="configLocation" value="classpath:SqlMapConfig.xml" />
- </bean>
-
-
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
- <property name="basePackage" value="com.mybatis.mapper" />
-
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- </bean>
-
-
- <bean id="userService" class="com.mybatis.service.UserService">
- <property name="userMapper" ref="userMapper" />
- </bean>
-
-
-
-
- <bean id="txManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
-
-
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="find*" read-only="true" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="*" read-only="true" />
- </tx:attributes>
- </tx:advice>
-
-
- <aop:config>
- <aop:pointcut expression="execution(* com.mybatis.service.*.*(..))"
- id="pt" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
- </aop:config>
-
- </beans>
4.db.properties
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql:
- jdbc.username=root
- jdbc.password=1234
5.源码下载
1 0