The key component of MyBatis is SqlSessionFactory from which we get SqlSession and execute the mapped SQL statements. The SqlSessionFactory object can be created using XML-based configuration or Java API.

The most commonly used approach for building SqlSessionFactory is XML-based configuration. The following mybatis-config.xml file shows how a typical MyBatis configuration file looks: 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="application.properties">
        <property name="username" value="db_user"/>
        <property name="password" value="verysecurepwd"/>
    </properties>
    <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    
    <typeAliases>
        <typeAlias alias="Tutor" type="com.mybatis3.domain.Tutor"/>
        <package name="com.mybatis3.domain"/>
    </typeAliases>
    <typeHandlers>
        <typeHandler handler="com.mybatis3.typehandlers.PhoneTypeHandler"/>
        <package name="com.mybatis3.typehandlers"/>
    </typeHandlers>
    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClassName}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
        <environment id="production">
            <transactionManager type="MANAGED"/>
            <dataSource type="JNDI">
                <property name="data_source" value="java:comp/jdbc/MyBatisDemoDS"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/mybatis3/mappers/StudentMapper.xml"/>
        <mapper url="file:///D:/mybatisdemo/mappers/TutorMapper.xml"/>
        <mapper class="com.mybatis3.mappers.TutorMapper"/>
    </mappers>
    
</configuration>
View Code

相关文章:

  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案