首页 > 代码库 > spring 配置ibatis和自动分页

spring 配置ibatis和自动分页

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"/>
<property name="url" value="http://www.mamicode.com/${db.bbs.url}"/>
<property name="username" value="http://www.mamicode.com/${db.bbs.username}"/>
<property name="password" value="http://www.mamicode.com/${db.bbs.password}"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="http://www.mamicode.com/2"/>
<property name="minIdle" value="http://www.mamicode.com/2"/>
<property name="maxActive" value="http://www.mamicode.com/${db.bbs.maxPoolSize}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="http://www.mamicode.com/60000"/>

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="http://www.mamicode.com/3000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="http://www.mamicode.com/300000"/>
<property name="validationQuery" value="http://www.mamicode.com/SELECT ‘x‘"/>
<property name="testWhileIdle" value="http://www.mamicode.com/true"/>

<!-- 这里建议配置为true,防止取到的连接不可用 -->
<property name="testOnBorrow" value="http://www.mamicode.com/true"/>
<property name="testOnReturn" value="http://www.mamicode.com/false"/>

<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="http://www.mamicode.com/true"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="http://www.mamicode.com/20"/>

<!-- 获取数据库连接的时候执行sql:set names utf8mb4; -->
<property name="connectionInitSqls" value="http://www.mamicode.com/set names utf8mb4 ;"/>

<!-- 配置监控统计拦截的filters -->
<property name="filters" value="http://www.mamicode.com/stat"/>
<!-- 配置关闭长时间不使用的连接 -->
<!-- 是否清理removeAbandonedTimeout秒没有使用的活动连接,清理后并没有放回连接池(针对未被close的活动连接) -->
<property name="removeAbandoned" value="http://www.mamicode.com/true"/>
<!-- 活动连接的最大空闲时间,1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="http://www.mamicode.com/1800"/>
<!-- 连接池收回空闲的活动连接时是否打印消息 -->
<property name="logAbandoned" value="http://www.mamicode.com/true"/>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="http://www.mamicode.com/com.inborn.inshop.model" />
<!-- mapper and resultmap path -->
<property name="mapperLocations">
<list>
<value>classpath*:mapper/**/*.xml</value>
</list>
</property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="http://www.mamicode.com/com.inborn.inshop.mapper" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

</beans>

spring 配置ibatis和自动分页