转载:
1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
(1)Spring配置文件:
- <!-- 引入jdbc配置文件 -->
- <context:property-placeholder location="jdbc.properties"/>
- <!--创建jdbc数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${driver}"/>
- <property name="url" value="${url}"/>
- <property name="username" value="${username}"/>
- <property name="password" value="${password}"/>
- <property name="initialSize" value="${initialSize}"/>
- <property name="maxActive" value="${maxActive}"/>
- <property name="maxIdle" value="${maxIdle}"/>
- <property name="minIdle" value="${minIdle}"/>
- </bean>
- <!-- 创建SqlSessionFactory,同时指定数据源-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!--创建数据映射器,数据映射器必须为接口-->
- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="com.xxt.ibatis.dbcp.dao.UserMapper" />
- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
- </bean>
- <bean id="userDaoImpl2" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl2">
- <property name="userMapper" ref="userMapper"/>
- </bean>
- <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <beans:property name="dataSource" ref="simpleDataSource" />
- <beans:property name="configLocation"
- value="classpath:conf/core/mybatis-config.xml" />
- </beans:bean>
- <beans:bean id="sqlSessionFactory_contact" class="org.mybatis.spring.SqlSessionFactoryBean">
- <beans:property name="dataSource" ref="simpleDataSource_contact" />
- <beans:property name="configLocation"
- value="classpath:conf/core/mybatis-config-contact.xml" />
- </beans:bean>
- <beans:bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <beans:property name="dataSource" ref="simpleDataSource" />
- </beans:bean>
- <beans:bean id="transactionManager_contact"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <beans:property name="dataSource" ref="simpleDataSource_contact" />
- </beans:bean>
- <!-- <tx:annotation-driven transaction-manager="transactionManager" />
- <tx:annotation-driven transaction-manager="transactionManager_contact" />
- -->
- <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="com.mybatis.UserDao"></property>
- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
- </bean>
- <bean id="userService" class="com.mybatis.UserServiceImpl">
- <property name="userDao" ref="userDao"></property>
- </bean>
- <!-- 加载配置文件 -->
- <beans:bean name="mapperScannerConfigurer"
- class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <beans:property name="basePackage" value="com.elong.hotel.crm.data.mapper" />
- <beans:property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></beans:property>
- </beans:bean>
- <beans:bean name="mapperScannerConfigurer_contact"
- class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <beans:property name="basePackage"
- value="com.elong.hotel.crm.data.contact.mapper" />
- <beans:property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_contact"></beans:property>
- </beans:bean>
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <!-- name表示以什么开始的方法名,比如 add*表示add开头的方法 propagation表示事务传播属性,不写默认有 -->
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="del*" />
- <tx:method name="update*" />
- <tx:method name="find*" read-only="true" />
- <tx:method name="get*" read-only="true" />
- <tx:method name="search*" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <tx:advice id="txAdvice_contact" transaction-manager="transactionManager_contact">
- <tx:attributes>
- <!-- name表示以什么开始的方法名,比如 add*表示add开头的方法 propagation表示事务传播属性,不写默认有 -->
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="del*" />
- <tx:method name="update*" />
- <tx:method name="find*" read-only="true" />
- <tx:method name="get*" read-only="true" />
- <tx:method name="search*" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <!-- 配置事务切面 -->
- <aop:config>
- <aop:pointcut expression="execution(* com.elong.hotel.crm.service..*.*(..))"
- id="pointcut" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
- <aop:advisor advice-ref="txAdvice_contact" pointcut-ref="pointcut" />
- </aop:config>