Spring整合Mybatis的三种方式

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
	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:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context 	http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/mvc 		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
		 
<!-- ## DB ## --> 
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>WEB-INF/classes/db/db.properties</value>
		</property>
	</bean>

<!-- #### DataSource	==c3p0配置==   ####-->
	<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">       
	    <property name="driverClass" value="${jdbc.driverClassName}"/>       
	    <property name="jdbcUrl" value="${jdbc.url}"/>       
	    <property name="user" value="${jdbc.username}"/>       
	    <property name="password" value="${jdbc.password}"/>       
	</bean>  -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean> 

<!-- #### SessionFactory ==MYBATIS配置== #### -->	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
	   	<property name="dataSource" ref="dataSource" />  
	   	 <!-- 指定sqlMapConfig总配置文件--> 
	   	<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> 
	   	 <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation 有一个即可,当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
	   	 <!-- <property  name="mapperLocations"  value="classpath*:com/xxt/ibatis/dbcp/**/*.xml"/>  -->   
	</bean> 
	<!-- 1:需要实现Dao,然后用此操作 --> 
	<!-- <bean id="sqlSessionTemplate"  class="org.mybatis.spring.SqlSessionTemplate">
	      <constructor-arg index="0" ref="sqlSessionFactory" />
	</bean> -->
	<!--2:直接用Dao接口,无需实现Dao ,但是每个Dao都需要写一个对应。 -->
	<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
       <property name="mapperInterface"  value="com.k.kpp.dao.UserDao" />  
       <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
    </bean>  
    <!-- 3:直接用Dao接口,此方法可以扫描,无需注入多个Dao -->
   <!--  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
   		<property name="basePackage" value="com.k.kpp.dao" />  
	</bean>   -->

<!-- #### 事务配置   	==== MYBATIS事务配置 ==== #### -->    
    <bean id="myTxManager"    
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
        <property name="dataSource" ref="dataSource" />    
    </bean>   
	<!-- 配置事务策略 --> 
	<tx:advice id="txAdvice" transaction-manager="myTxManager">
		<tx:attributes>
			<tx:method name="find*" propagation="REQUIRED" read-only="true" />
			<tx:method name="save*" propagation="REQUIRED" read-only="false" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false" />
			<tx:method name="del*" propagation="REQUIRED" read-only="false" />
			<tx:method name="*" propagation="REQUIRED" read-only="false" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.k.kpp.service.*.*(..))" />
	</aop:config>


<!-- ## CXF ## -->	
	<!-- <jaxws:endpoint id="helloWorld" implementor="com.k.kpp.service.impl.CXFHelloImpl" address="/HelloWorld"/> -->
	<!-- <jaxws:endpoint id="kayTest"
		implementorClass=" com.k.kpp.service.ICXFHello"
		address="/KayTest">
		<jaxws:implementor>
			<bean id="kayTestImpl"
				class="com.k.kpp.service.impl.CXFHelloImpl">
				<property name="kayTestComponent" ref="kayTestComponent" />
			</bean>
		</jaxws:implementor>
	</jaxws:endpoint> -->
	

<!-- #### qita #### -->
<!-- Custom Spring DI -->
	
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值