spring-mybatis整合(配置文件)

spring-context.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans  

           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  

           http://www.springframework.org/schema/aop   

           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  

           http://www.springframework.org/schema/tx  

           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  

           http://www.springframework.org/schema/context  

           http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<!-- 采用注释的方式配置bean -->  

<context:annotation-config/>

<!-- 配置要扫描的包 -->

<context:component-scanbase-package="com.xxx"/>

<!-- 读入配置属性文件 -->

<context:property-placeholderlocation="classpath:db.properties"/>

<!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->  

<aop:aspectj-autoproxyproxy-target-class="true"/>

<importresource="classpath:spring/spring-mybatis.xml"/>

</beans>


spring-mybatis.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"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:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<!-- 基于Druid数据库链接池的数据源配置 -->

<beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="clone">

<!-- 基本属性driverClassName、url、user、password -->

<propertyname="driverClassName"value="${jdbc.driverClassName}"/>

<propertyname="url"value="${jdbc.url}"/>

<propertyname="username"value="${jdbc.username}"/>

<propertyname="password"value="${jdbc.password}"/>

<!-- 配置初始化大小、最小、最大 -->

<!-- 通常来说,只需要修改initialSize、minIdle、maxActive -->

<!-- 初始化时建立物理连接的个数,缺省值为0 -->

<propertyname="initialSize"value="3"/>

<!-- 最小连接池数量 -->

<propertyname="minIdle"value="3"/>

<!-- 最大连接池数量,缺省值为8 -->

<propertyname="maxActive"value="20"/>


<!-- 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。 -->

<propertyname="maxWait"value="60000"/>

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒(3600000:为1小时) -->

<propertyname="timeBetweenEvictionRunsMillis"value="3600000"/>

<!-- 配置一个连接在池中最小生存的时间,单位是毫秒(300000:为5分钟) -->

<propertyname="minEvictableIdleTimeMillis"value="300000"/>

<!-- 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。 -->

<propertyname="validationQuery"value="${jdbc.pool.validationQuery}"/>

<!-- 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。建议配置为true,不影响性能,并且保证安全性。-->

      <propertyname="testWhileIdle"value="true"/>

      <!-- 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。缺省值:true -->

      <propertyname="testOnBorrow"value="false"/>

      <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。缺省值:false -->

      <propertyname="testOnReturn"value="false"/>

      

      <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->

<!-- 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。5.5及以上版本有PSCache,建议开启。缺省值:false -->

<propertyname="poolPreparedStatements"value="true"/>

<propertyname="maxPoolPreparedStatementPerConnectionSize"value="20"/>

<!-- 解密密码必须要配置的项 -->

<propertyname="filters"value="config"/>

<propertyname="connectionProperties"value="config.decrypt=true"/>

</bean>

<!-- 将数据源映射到sqlSessionFactory中 -->

<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

<propertyname="configLocation"value="classpath:mybatis/mybatis-config.xml"/>

<propertyname="dataSource"ref="dataSource"/>

</bean>


<!-- SqlSession模板类实例 -->

<beanid="sessionTemplate"class="org.mybatis.spring.SqlSessionTemplate"destroy-method="close">

<constructor-argindex="0"ref="sqlSessionFactory"/>

</bean>


<!--======= 事务配置 Begin ================= -->

<!-- 事务管理器(由Spring管理MyBatis的事务) -->

<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!-- 关联数据源 -->

<propertyname="dataSource"ref="dataSource"></property>

</bean>

<!-- 注解事务 -->

<tx:annotation-driventransaction-manager="transactionManager"/>

<!--======= 事务配置 End =================== -->

</beans>


mybatis-config.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!-- Copyright 2009-2012 The MyBatis Team Licensed under the Apache License, 

Version 2.0 (the "License"); you may not use this file except in compliance 

with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software distributed 

under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 

OR CONDITIONS OF ANY KIND, either express or implied. See the License for 

the specific language governing permissions and limitations under the License. -->

<!DOCTYPE configuration

    PUBLIC"-//mybatis.org//DTD Config 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-config.dtd">


<configuration>


<settings>

<!-- 这个配置使全局的映射器启用或禁用 缓存 -->

<settingname="cacheEnabled"value="true"/>

<!-- 全局启用或禁用延迟加载。当禁用时, 所有关联对象都会即时加载 -->

<settingname="lazyLoadingEnabled"value="true"/>

<!-- 允许或不允许多种结果集从一个单独 的语句中返回(需要适合的驱动) -->

<settingname="multipleResultSetsEnabled"value="true"/>

<!-- 使用列标签代替列名。 不同的驱动在这 方便表现不同。 参考驱动文档或充分测 试两种方法来决定所使用的驱动 -->

<settingname="useColumnLabel"value="true"/>

<!-- 允许 JDBC 支持生成的键。 需要适合的 驱动。 如果设置为 true 则这个设置强制 生成的键被使用, 尽管一些驱动拒绝兼 容但仍然有效(比如 

Derby) -->

<settingname="useGeneratedKeys"value="false"/>

<!-- 配置默认的执行器。SIMPLE 执行器没 有什么特别之处。REUSE 执行器重用 预处理语句。BATCH 执行器重用语句 和批量更新 -->

<settingname="defaultExecutorType"value="SIMPLE"/>

<!-- 设置超时时间, 它决定驱动等待一个数 据库响应的时间 -->

<settingname="defaultStatementTimeout"value="100"/>

<settingname="safeRowBoundsEnabled"value="false"/>

<settingname="mapUnderscoreToCamelCase"value="false"/>

<settingname="localCacheScope"value="SESSION"/>

<settingname="jdbcTypeForNull"value="OTHER"/>

<settingname="lazyLoadTriggerMethods"value="equals,clone,hashCode,toString"/>

</settings>


<mappers>

<mapperresource="mybatis/mapper/xxx.xml"/>

</mappers>


</configuration>




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值