javascript
Spring框架集成mybatis框架的配置(笔记)
<!-- 0.注解掃描 -->
<!-- 1.導入外部文件 -->
<!-- 2.數(shù)據(jù)源 -->
<!-- 3.session Factory -->
<!-- 4.事務模板 -->
<!-- 5.AOP相關配置 -->
<!-- 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.注解掃描 上下文 構成 掃描 -->
<context:component-scan base-package="com.wxzj.crm"></context:component-scan>
<!-- 1.導入外部文件 上下文 屬性 占位 位置-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.數(shù)據(jù)源 ctrl shift T 搜索datasource 告訴spring初始化時初始化鏈接,銷毀時關閉鏈接
同時需要配置參數(shù),驅動的名字,鏈接,賬號,密碼 name value ${}(在尖括號里寫)
-->
<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的工場是SqlSessionFactoryBean
里面需要配置一個數(shù)據(jù)庫源
和一個 配置config 源location 指向mybatis的主配置文件
第三個給每一個類起一個別名,這是為啥
找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>
<!-- 事務管理器 transaction事務的意思 給他配置數(shù)據(jù)源,讓他在這個數(shù)據(jù)源上建立事務-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.事務模板 他自己沒有事務,得用別人的,這次用阿里巴巴druid 他需要的事務管理器的名字叫做transaction-manager,默認引用
advice: n. 建議;忠告;勸告;通知 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<!-- 配置每一個方法的專屬配置 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 切面相關配置 和事務一塊使用的,他是面向切面編程的配置
事務(id=advice)配置的是怎么管理事務,AOP配置的是在哪執(zhí)行事務
如果只配置事務不配置aop的話應該是沒用的
pointcut切入點 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掃描 配置個mapper掃描的對象,讓他去掃描mapper ,這應該是spring整合其他框架的手法-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wxzj.crm.mapper"></property>
</bean>
</beans>
轉載于:https://www.cnblogs.com/work396/p/7135999.html
總結
以上是生活随笔為你收集整理的Spring框架集成mybatis框架的配置(笔记)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java游戏开发基础Swing之JRad
- 下一篇: 实用JavaScript网页特效编程百宝