All Products
Search
Document Center

ApsaraDB for OceanBase (Deprecated):Connect to an OceanBase database by using MyBatis

Last Updated:Aug 01, 2023

This topic describes how to configure dependencies and configuration files for a MyBatis connection.

Configure dependencies

<dependency>
    <groupId>com.oceanbase</groupId>
    <artifactId>oceanbase-client</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.4</version>
</dependency>

Configuration file

mybatis-config.xml

File content:

<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.oceanbase.jdbc.Driver"/>
                <property name="url" value="jdbc:oceanbase://xxx.xxx.xxx.xxx:1521/?useUnicode=true&amp;characterEncoding=utf-8&amp;useServerPrepStmts=false&amp;useCursorFetch=true"/>
                <property name="username" value="a****"/>
                <property name="password" value="******"/>
            </dataSource>
        </environment>
    </environments>

    <!--Register the mapper (the address of mapper.xml)-->
    <mappers>
        <mapper resource="com/test/UserMapper.xml"></mapper>
    </mappers>
</configuration>

mapper.xml

File content:

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--Make sure that the namespace field is set to the mapper API.-->
<mapper namespace="com.test.UserMapper">
    <select id="selectUser" resultType="com.test.User" fetchSize="40000">
        select * from user;
    </select>

    <delete id="delete" >
        delete from user;
    </delete>
</mapper>