[spring]xml配置文件中bean属性的两种写法(p:configLocation <=> <property name="configLocation"/>)...

本文详细介绍了如何在Spring环境中配置MyBatis,包括通过`SqlSessionFactoryBean`设置数据源、配置文件路径及Mapper文件的位置。提供了两种配置方式,并解释了`SqlSessionFactoryBean`的作用。
1.当作bean节点的属性:p:configLocation:
<!-- mybatis文件配置,扫描所有mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:configLocation="classpath:mybatis-config.xml"
      p:mapperLocations="classpath:com/eliteams/quick4j/web/dao/*.xml"/>

2.当作bean节点的属性节点:<property name="configLocation"/>:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource"/>
        <!-- mapper和resultmap配置路径 -->
        <property name="classpath:com/eliteams/quick4j/web/dao/*.xml" />
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

 注:org.mybatis.spring.SqlSessionFactoryBean:

SqlSessionTemplate中需要的是SqlSessionFactory,而不是SqlSessionFactoryBean。此处使用SqlSessionFactoryBean是因为SqlSessionFactoryBean继承了FactoryBean<SqlSessionFactory>

转载于:https://www.cnblogs.com/vickylinj/p/9475832.html

<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 对基本包进行注解扫描,开启注解 --> <context:component-scan base-package="com.esms"> <!-- 只对controller进行扫描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- Spring配置文件,这里主要配置和业务逻辑有关的 --> <!--=================== 数据源,事务控制,xxx ================ --> <!-- 加载数据库配置文件dbconfig.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 加载数据库 --> <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${datasource.connection.driver_class}"/> <property name="jdbcUrl" value="${datasource.connection.url}"/> <property name="user" value="${datasource.connection.username}"/> <property name="password" value="${datasource.connection.password}"/> <property name="minPoolSize" value="${datasource.connection.minPoolSize}"/> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="${datasource.connection.maxPoolSize}"/> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="${datasource.connection.maxIdleTime}"/> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="${datasource.connection.acquireIncrement}"/> <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements" value="${datasource.connection.maxStatements}"/> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="${datasource.connection.maxStatementsPerConnection}"/> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="${datasource.connection.initialPoolSize}"/> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="${datasource.connection.idleConnectionTestPeriod}"/> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="${datasource.connection.acquireRetryAttempts}"/> <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false --> <property name="breakAfterAcquireFailure" value="${datasource.connection.breakAfterAcquireFailure}"/> <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false --> <property name="testConnectionOnCheckout" value="${datasource.connection.testConnectionOnCheckout}"/> <property name="checkoutTimeout" value="${datasource.connection.checkoutTimeout}"/> <property name="testConnectionOnCheckin" value="${datasource.connection.testConnectionOnCheckin}"/> <property name="automaticTestTable" value="${datasource.connection.automaticTestTable}"/> <property name="acquireRetryDelay" value="${datasource.connection.acquireRetryDelay}"/> <!--自动超时回收Connection--> <property name="unreturnedConnectionTimeout" value="${datasource.connection.unreturnedConnectionTimeout}"/> <!--超时自动断开--> <property name="maxIdleTimeExcessConnections" value="${datasource.connection.maxIdleTimeExcessConnections}"/> <property name="maxConnectionAge" value="${datasource.connection.maxConnectionAge}"/> </bean> <!--================== 配置和MyBatis的整合=============== --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis全局配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="dataSource" ref="pooledDataSource"></property> <!-- 指定mybatis,mapper文件的位置 --> <property name="mapperLocations" value="classpath:mappers/*.xml"></property> </bean> <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描所有dao接口的实现,加入到ioc容器中 --> <property name="basePackage" value="com.esms.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 配置一个可以执行批量的sqlSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean> <!-- ===============事务控制的配置 ================ --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--控制住数据源 --> <property name="dataSource" ref="pooledDataSource"></property> </bean> <!--配置事务增强,事务如何切入 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="SUPPORTS"/> <tx:method name="list*" read-only="true" propagation="SUPPORTS"/> <tx:method name="find*" read-only="true" propagation="SUPPORTS"/> <tx:method name="query*" read-only="true" propagation="SUPPORTS"/> <tx:method name="count*" read-only="true" propagation="SUPPORTS"/> <tx:method name="update*" read-only="false" propagation="REQUIRED"/> <tx:method name="insert*" read-only="false" propagation="REQUIRED"/> <tx:method name="save*" read-only="false" propagation="REQUIRED"/> <tx:method name="delete*" read-only="false" propagation="REQUIRED"/> <tx:method name="remove*" read-only="false" propagation="REQUIRED"/> <tx:method name="*" read-only="true" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:aspectj-autoproxy proxy-target-class="true"/> <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) --> <aop:config> <!-- 切入点表达式 --> <!-- 需要修改包的扫描 --> <aop:pointcut expression="execution(* com.esms.service.impl.*.*(..))" id="txPoint" /> <!-- 配置事务增强 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" /> </aop:config> <bean class="com.esms.exception.CustomExceptionResolver"/> </beans>
07-19
<?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:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> <!-- 使用缓存annotation 配置 --> <!-- <cache:annotation-driven cache-manager="ehCacheManager" /> --> <mvc:default-servlet-handler/> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="masterSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url.master}</value> </property> <property name="username"> <value>${jdbc.username.master}</value> </property> <property name="password"> <value>${jdbc.password.master}</value> </property> <!-- 连接池最大使用连接数 --> <property name="maxActive"> <value>50</value> </property> <!-- 初始化连接大小 --> <property name="initialSize"> <value>1</value> </property> <!-- 获取连接最大等待时间 --> <property name="maxWait"> <value>60000</value> </property> <!-- 连接池最大空闲 --> <!-- <property name="maxIdle"> <value>20</value> </property> --> <!-- 连接池最小空闲 --> <property name="minIdle"> <value>30</value> </property> <property name="testWhileIdle"> <value>true</value> </property> <property name="validationQuery"> <value>select 1 from dual</value> </property> <!-- 自动清除无用连接 --> <property name="removeAbandoned"> <value>true</value> </property> <!-- 清除无用连接的等待时间 --> <property name="removeAbandonedTimeout"> <value>90</value> </property> <!-- 连接属性 --> <property name="connectionProperties"> <value>clientEncoding=UTF-8</value> </property> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis"> <value>60000</value> </property> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis"> <value>300000</value> </property> </bean> <bean id="slaveSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url.slave}</value> </property> <property name="username"> <value>${jdbc.username.slave}</value> </property> <property name="password"> <value>${jdbc.password.slave}</value> </property> <!-- 连接池最大使用连接数 --> <property name="maxActive"> <value>50</value> </property> <!-- 初始化连接大小 --> <property name="initialSize"> <value>1</value> </property> <!-- 获取连接最大等待时间 --> <property name="maxWait"> <value>60000</value> </property> <!-- 连接池最大空闲 --> <!-- <property name="maxIdle"> <value>20</value> </property> --> <!-- 连接池最小空闲 --> <property name="minIdle"> <value>30</value> </property> <property name="testWhileIdle"> <value>true</value> </property> <property name="validationQuery"> <value>select 1 from dual</value> </property> <!-- 自动清除无用连接 --> <property name="removeAbandoned"> <value>true</value> </property> <!-- 清除无用连接的等待时间 --> <property name="removeAbandonedTimeout"> <value>90</value> </property> <!-- 连接属性 --> <property name="connectionProperties"> <value>clientEncoding=UTF-8</value> </property> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis"> <value>60000</value> </property> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis"> <value>300000</value> </property> </bean> <bean id="dynamicDataSource" class="com.sjsemi.rrc.datasource.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="master" value-ref="masterSource"/> <entry key="slave" value-ref="slaveSource"/> </map> </property> <!-- 默认数据源 --> <property name="defaultTargetDataSource" ref="masterSource"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dynamicDataSource"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dynamicDataSource"/> <!-- 配置myBatis全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="com.sjsemi.rrc.entity"/> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath*:com/sjsemi/rrc/mybatis/**/*Mapper.xml"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helpDialect = oracle </value> </property> </bean> </array> </property> </bean> <context:component-scan base-package="com.sjsemi.rrc"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <task:annotation-driven scheduler="taskScheduler" mode="proxy"/> <task:scheduler id="taskScheduler" pool-size="10"/> <!-- 文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> </bean> <!-- 扫描basePackage下所有的接口类,根据对应的mapper.xml为其生成代理类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.sjsemi.rrc.mybatis.mapper"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <!-- 配置事务管理器 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dynamicDataSource"></property> </bean> <!-- 注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="append*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="creat*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="edit*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> <tx:method name="repair" propagation="REQUIRED"/> <tx:method name="delAndRepair" propagation="REQUIRED"/> <tx:method name="get*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="select*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="load*" read-only="true"/> <tx:method name="search*" read-only="true"/> <tx:method name="datagrid*" read-only="true"/> <tx:method name="*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.sjsemi.rrc.service.impl.*.*(..))"/> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/> </aop:config> <aop:aspectj-autoproxy proxy-target-class="true"/> <bean id="dataSourceAspect" class="com.sjsemi.rrc.datasource.DataSourceAspect"/> <aop:config> <aop:pointcut id="dataSourcePointcut" expression="execution(* com.sjsemi.rrc.service.impl.*.*(..))"/> <aop:advisor pointcut-ref="dataSourcePointcut" advice-ref="dataSourceAspect" order="1"/> </aop:config> </beans>
10-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值