javascript
Spring框架集成mybatis框架的配置(笔记)
<!-- 0.注解掃描 -->
<!-- 1.導(dǎo)入外部文件 -->
<!-- 2.數(shù)據(jù)源 -->
<!-- 3.session Factory -->
<!-- 4.事務(wù)模板 -->
<!-- 5.AOP相關(guān)配置 -->
<!-- Mapper掃描 -->
?
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 0.注解掃描 上下文 構(gòu)成 掃描 -->
<context:component-scan base-package="com.wxzj.crm"></context:component-scan>
<!-- 1.導(dǎo)入外部文件 上下文 屬性 占位 位置-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.數(shù)據(jù)源 ctrl shift T 搜索datasource 告訴spring初始化時(shí)初始化鏈接,銷毀時(shí)關(guān)閉鏈接
同時(shí)需要配置參數(shù),驅(qū)動(dòng)的名字,鏈接,賬號(hào),密碼 name value ${}(在尖括號(hào)里寫)
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3.session Factory mybatis獲取session的工場(chǎng)是SqlSessionFactoryBean
里面需要配置一個(gè)數(shù)據(jù)庫源
和一個(gè) 配置config 源location 指向mybatis的主配置文件
第三個(gè)給每一個(gè)類起一個(gè)別名,這是為啥
找mapper 這次找的是路徑 選擇路徑下的所有mapper (映射)
-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
<property name="typeAliasePackage" value="com.wxzj.crm.domain"></property>
<property name="mapperLocations" value="com/wxzj/crm/mapper/*Mapper.xml"></property>
</bean>
<!-- 事務(wù)管理器 transaction事務(wù)的意思 給他配置數(shù)據(jù)源,讓他在這個(gè)數(shù)據(jù)源上建立事務(wù)-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.事務(wù)模板 他自己沒有事務(wù),得用別人的,這次用阿里巴巴druid 他需要的事務(wù)管理器的名字叫做transaction-manager,默認(rèn)引用
advice: n. 建議;忠告;勸告;通知 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<!-- 配置每一個(gè)方法的專屬配置 attributes 屬性 -->
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 5.AOP 切面相關(guān)配置 和事務(wù)一塊使用的,他是面向切面編程的配置
事務(wù)(id=advice)配置的是怎么管理事務(wù),AOP配置的是在哪執(zhí)行事務(wù)
如果只配置事務(wù)不配置aop的話應(yīng)該是沒用的
pointcut切入點(diǎn) execution執(zhí)行-->
<aop:config>
<aop:pointcut expression="execution(* com.xyz.myapp.service.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointCut"/>
</aop:config>
<!-- Mapper掃描 配置個(gè)mapper掃描的對(duì)象,讓他去掃描mapper ,這應(yīng)該是spring整合其他框架的手法-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wxzj.crm.mapper"></property>
</bean>
</beans>
轉(zhuǎn)載于:https://www.cnblogs.com/work396/p/7135999.html
總結(jié)
以上是生活随笔為你收集整理的Spring框架集成mybatis框架的配置(笔记)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java游戏开发基础Swing之JRad
- 下一篇: lua-5.2.3编译问题记录quot;