applicationContext-mybatis.xml 配置檔案
阿新 • • 發佈:2018-12-11
applicationContext-mybatis.xml 配置檔案
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- spring 管理bean 建立物件的 --> <!--3 讀取外部配置檔案 --> <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:database.properties"/> </bean>--> <context:property-placeholder location="classpath:database.properties"></context:property-placeholder> <!--2 配置資料來源 dbcp - - >dbcp2 c3p0 druid 德魯依 alibaba 最牛的資料來源 ==dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="${jdbc.user}"></property> <property name="url" value="${jdbc.url}"/> <property name="password" value="${jdbc.password}"/> <property name="driverClassName" value="${jdbc.driver}"/> <!-- 省略心跳包 --> </bean> <!-- 1 連線資料庫,需要資料庫連線物件 工廠--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- <property name="mapperLocations" value="classpath:cn/kgc/mapper/*.xml"></property>--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 4 配置mybaits mapper XxxxxTemplate XxxxSupport 開發方式1 傳統開發 ibatis過來,自己寫實現類 2 mapper代理開發 ,沒有實現類 1 一個一個配置 MapperFactoryBean 有id 2 一次性裝配 MapperScannerConfigure 沒有顯式ID,預設將首字母小寫當ID --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.kgc.dao"/> </bean> <!--<bean id="devUserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">--> <!--<property name="mapperInterface" value="cn.kgc.mapper.DevUserMapper"/>--> <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"> </property>--> <!--</bean>--> <!-- 5 告訴spring使用註解--> <context:component-scan base-package="cn.kgc.service"/> <!--<bean id="manServiceImpl" class="cn.kgc.service.impl.ManSerciveImpl"/>--> <!-- 6 宣告式的事務處理 --> <!-- 6.3 配置事務管理類--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 6.2 配置通知--> <tx:advice id="tx" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="NEVER"/> <tx:method name="set*" propagation="NEVER"/> <tx:method name="select*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="show*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 6.1 aop配置事務--> <aop:config> <aop:pointcut id="pc" expression="execution(public * cn.kgc.service..*(..))"></aop:pointcut> <aop:advisor advice-ref="tx" pointcut-ref="pc"></aop:advisor> </aop:config> </beans>