全部产品
Search
文档中心

云数据库 OceanBase:MyBatis 连接 OceanBase 数据库

更新时间:Feb 04, 2024

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

配置依赖

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.25</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.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx:3306/test?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>