MyBatis 连接 OceanBase 数据库

本文介绍 MyBatis 连接示例的配置依赖和配置文件。

配置依赖

<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>

配置文件

mybatis-config.xml 文件

内容如下:

<?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>

    <!--注册 mapper(mapper.xml 所在地址)-->
    <mappers>
        <mapper resource="com/test/UserMapper.xml"></mapper>
    </mappers>
</configuration>

mapper.xml 文件

内容如下:

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 是 mapper 接口,不能填错-->
<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>