欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

Spring&Mybaits数据库配置解惑,

来源: javaer 分享于  点击 25824 次 点评:14

Spring&Mybaits数据库配置解惑,


一、前言

一般我们会在datasource.xml中进行如下配置,但是其中每个配置项原理和用途是什么,并不是那么清楚,如果不清楚的话,在使用时候就很有可能会遇到坑,所以下面对这些配置项进行一一解说

1)配置数据源
?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd   
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd   
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- (1) 数据源 -->
    <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="jdbc:mysql://127.0.0.1:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
        <property name="maxWait" value="3000" />
        <property name="maxActive" value="28" />
        <property name="initialSize" value="2" />
        <property name="minIdle" value="0" />
        <property name="timeBetweenEvictionRunsMillis" value="300000" />
        <property name="testOnBorrow" value="false" />
        <property name="testWhileIdle" value="true" />
        <property name="validationQuery" value="select 1 from dual" />
        <property name="filters" value="stat" />
    </bean>

    <!-- (2) session工厂 -->
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations"
            value="classpath*:mapper/*Mapper*.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- (3) 配置扫描器,扫描指定路径的mapper生成数据库操作代理类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="annotationClass"
            value="javax.annotation.Resource"></property>
        <property name="basePackage" value="com.zlx.user.dal.sqlmap" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>


</beans>
  • 其中(1)是配置数据源,这里使用了druid连接池,用户可以根据自己的需要配置不同的数据源,也可以选择不适用数据库连接池,而直接使用具体的物理连接。
  • 其中(2)创建sqlSessionFactory,用来在(3)时候使用。
  • 其中(3)配置扫描器,扫描指定路径的mapper生成数据库操作代理类

二、SqlSessionFactory内幕

第二节配置中配置SqlSessionFactory的方式如下:

<!-- (2) session工厂 -->
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations"
            value="classpath*:mapper/*Mapper*.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

其中mapperLocations配置mapper.xml文件所在的路径,dataSource配置数据源,下面我们具体来看SqlSessionFactoryBean的代码,SqlSessionFactoryBean实现了FactoryBean和InitializingBean扩展接口,所以具有getObject和afterPropertiesSet方法(具体可以参考:

想了解更多关于粘包半包问题

用户点评