1. 程式人生 > 其它 >mybatis配置檔案中dataSource的配置

mybatis配置檔案中dataSource的配置

技術標籤:mybatisjava

一、dataSource中三種新增屬性的方式
方式一:配置資料庫連線資訊

<dataSource type="POOLED">
 <property name="driver" value="com.mysql.jdbc.Driver"/>
 <property name="url"  value="jdbc:mysql://localhost:3306/amy_db"/>
<property name="username"
value="root"/>
<property name="password" value="root"/> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <
property
name="password" value="${password}"/>
</dataSource>

方式二:配置資料庫連線資訊

<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/amy_db"/>
<property
name="username" value="root"/>
<property name="password" value="root"/> </properties> <environments default="development"> <environment id="development"> <!--採用JDBC事務管理--> <transactionManager type="JDBC"> <property name="autoCommit" value="false"/> </transactionManager> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments>

方式三:配置資料庫連線資訊
jdbc.properties

#資料庫配置檔案
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/amy_db
username=root
password=root
<properties resource="jdbc.properties"/>
 <environments default="development">
        <environment id="development">
            <!--採用JDBC事務管理-->
            <transactionManager type="JDBC">
                <property name="autoCommit" value="false"/>
            </transactionManager>
 			<dataSource type="POOLED">
			<property name="driver" value="${driver}"/>
     		<property name="url" value="${url}"/>
			<property name="username" value="${username}"/>
			<property name="password" value="${password}"/>
		</dataSource>         
        </environment>
    </environments>

二、使用自定義DataSource

package util;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.ibatis.datasource.DataSourceFactory;

import javax.sql.DataSource;
import java.util.Properties;

public class DbcpDataSourceFactory extends BasicDataSource implements DataSourceFactory {
    private Properties properties = null;

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public DataSource getDataSource() {
        DataSource dataSource = null;
        try{
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
           e.printStackTrace();
        }
        return dataSource;
    }
}

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD  Configuration 3.1//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--配置資料庫連線資訊-->
    <properties resource="jdbc.properties"/>
    <!--定義別名 -->
    <typeAliases>
        <!-- 方式一:使用包名+類名全路徑限定 -->
        <typeAlias alias="role" type="pojo.Role"/>
        <!--掃描包路徑 + @Alias註解的方式-->
        <package name="pojo.bean"/>
    </typeAliases>

    <!--註冊自定義的typeHandler-->
    <typeHandlers>
        <typeHandler jdbcType="VARCHAR" javaType="string" handler="typeHandler.MyStringTypeHandler"/>
        <!-- EnumOrdinalTypeHandler -->
        <typeHandler handler="org.apache.ibatis.type.EnumTypeHandler" javaType="util.Sex" />
    </typeHandlers>

    <!--定義資料庫資訊。預設使用 development 資料庫構建環境-->
    <environments default="development">
        <environment id="development">
            <!--採用JDBC事務管理-->
            <transactionManager type="JDBC">
                <property name="autoCommit" value="false"/>
            </transactionManager>
            <!--使用自定義的dataSource-->
            <dataSource type="util.DbcpDataSourceFactory">
                 <property name="driver" value="${driver}"/>
                 <property name="url" value="${url}"/>
                 <property name="username" value="${username}"/>
                 <property name="password" value="${password}"/>
            </dataSource>

        </environment>
    </environments>
    <!--定義對映器-->
    <mappers>
        <mapper resource="mapper/role.xml"/>
        <mapper resource="mapper/userMapper.xml"/>
    </mappers>
</configuration>

jdbc.properties

#資料庫配置檔案
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/amy_db
username=root
password=root