1. 程式人生 > >Spring集成Mybatis

Spring集成Mybatis

不可 auto 連接不可用 enabled TP mov 使用 spa psc

一、配置數據源

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="${db.bbs.url}"/>
        <property name="username" value
="${db.bbs.username}"/> <property name="password" value="${db.bbs.password}"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${db.bbs.initialSize}"/> <property name="minIdle" value="${db.bbs.minIdle}"/> <property name="maxActive"
value="${db.bbs.maxPoolSize}"/> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000"/> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="3000"/> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="SELECT ‘x‘"/> <property name="testWhileIdle" value="true"/> <!-- 這裏建議配置為true,防止取到的連接不可用 --> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="false"/> <!-- 打開PSCache,並且指定每個連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- 獲取數據庫連接的時候執行sql:set names utf8mb4; --> <property name="connectionInitSqls" value="set names utf8mb4 ;"/> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat"/> <!-- 配置關閉長時間不使用的連接 --> <!-- 是否清理removeAbandonedTimeout秒沒有使用的活動連接,清理後並沒有放回連接池(針對未被close的活動連接) --> <property name="removeAbandoned" value="true"/> <!-- 活動連接的最大空閑時間,1800秒,也就是30分鐘 --> <property name="removeAbandonedTimeout" value="1800"/> <!-- 連接池收回空閑的活動連接時是否打印消息 --> <property name="logAbandoned" value="true"/> </bean>

二、配置SqlSessionFactory

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--Mybatis全局配置-->
        <property name="configLocation" value="classpath:spring/mybatis-config.xml"/>
        <!--model路徑-->
        <property name="typeAliasesPackage" value="com.wslook.model"/>
        <property name="mapperLocations">
            <list>
                <value>classpath:mapper/*.xml</value>
            </list>
        </property>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

三、Mybatis全局配置文件(非必須)

<configuration>

    <settings>
        <!-- Globally enables or disables any caches configured in any mapper under this configuration -->
        <setting name="cacheEnabled" value="true"/>
        <!-- Sets the number of seconds the driver will wait for a response from the database -->
        <setting name="defaultStatementTimeout" value="3000"/>
        <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- Allows JDBC support for generated keys. A compatible driver is required.
        This setting forces generated keys to be used if set to true,
         as some drivers deny compatibility but still work -->
        <setting name="useGeneratedKeys" value="true"/>
    </settings>

    <!-- Continue going here -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="dialect" value="mysql"/>
            <property name="pageSizeZero" value="true"/>
            <!-- 如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最後一頁(false返回空);-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>

</configuration>

四、配置mapper路徑

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.wslook.dao"/>
</bean>

五、配置sqlSessionTemplate(非必須)

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

Spring集成Mybatis