MyBatis 是一個 Java 資料持久層架構。通過 MyBatis 整合Table Store JDBC 驅動,可以在 Java 專案中以 SQL 方式訪問Table Store資料。更多資訊,參見MyBatis 官網。
前提條件
已擷取 AccessKey,RAM 使用者需具備
"Action": "ots:SQL*"許可權。已建立資料表和映射表,詳見DDL 操作。
使用流程
完成以下步驟,將 MyBatis 與Table Store JDBC 驅動整合。
Table Store的 JDBC 驅動內建串連池功能,不建議使用外部資料庫串連池工具,以避免相容性問題。
步驟一:安裝 JDBC 驅動
可以通過以下兩種方式安裝 JDBC 驅動。
添加Maven依賴
在 Maven 工程中使用Table Store JDBC 驅動,在 pom.xml 中加入以下依賴。以 5.17.0 版本為例,在 <dependencies> 內加入如下內容:
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>tablestore-jdbc</artifactId>
<version>5.17.0</version>
</dependency>手動安裝
下載Table Store JDBC 驅動並匯入到專案中。具體下載路徑參見Table Store JDBC 驅動。
步驟二:安裝 MyBatis
可以通過以下兩種方式安裝 MyBatis。
添加Maven依賴
在 Maven 工程中使用 MyBatis,在 pom.xml 中加入以下依賴。以 3.5.9 版本為例,在 <dependencies> 內加入如下內容:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>手動安裝
下載 MyBatis 安裝包並匯入到專案中。具體下載路徑參見MyBatis 安裝包。
步驟三:映射 SQL 欄位
建立資料表欄位對應的 Java Bean。在本樣本中 trip_id 為資料表中的唯一主鍵。
重要Java Bean 的成員變數名稱必須和資料表的欄位名相同。
package mybatis; public class Trip { private long trip_id; private long duration; private String start_date; private String end_date; private long start_station_number; private long end_station_number; private String start_station; private String end_station; private String bike_number; private String member_type; // 以 trip_id 為例,其他欄位類似。 public void setTrip_Id(Long trip_id){ this.trip_id = trip_id; } public Long getTrip_Id() { return trip_id; } }建立映射設定檔,並在映射設定檔中定義查詢條件。此處以在 mybatis 目錄下建立 TripMapper.xml 為例。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mybatis.TripMapper"> <select id="selectTrip" resultType="mybatis.Trip"> select * from trips where trip_id = #{id} </select> </mapper>
步驟四:構建 SqlSessionFactory
SqlSessionFactory 用於建立 MyBatis 會話,通過會話串連Table Store。
建立 MyBatis 設定檔 mybatis-config.xml,添加如下內容,將配置項替換為實際值。
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <!-- Table Store JDBC 驅動需主動關閉才能退出進程。常駐程式用 POOLED,查完即退用 UNPOOLED。 --> <dataSource type="UNPOOLED"> <property name="driver" value="com.alicloud.openservices.tablestore.jdbc.OTSDriver"/> <property name="url" value="jdbc:ots:https://myinstance.cn-hangzhou.ots.aliyuncs.com/myinstance"/> <property name="username" value="***********************"/> <property name="password" value="********************************"/> </dataSource> </environment> </environments> <mappers> <!-- 設定為映射設定檔的路徑。--> <mapper resource="mybatis/TripMapper.xml"/> </mappers> </configuration>配置項說明如下。
配置項
類型
是否必選
描述
driver
class
是
Table Store JDBC 驅動的類名,固定為
com.alicloud.openservices.tablestore.jdbc.OTSDriver。url
string
是
執行個體訪問地址。格式為
jdbc:ots:endpoint/instanceName,其中 endpoint 為執行個體的服務地址,instanceName 為執行個體名稱,根據實際情況修改。username
string
是
阿里雲帳號或者 RAM 使用者的 AccessKey ID。
password
string
是
阿里雲帳號或者 RAM 使用者的 AccessKey Secret。
通過載入 MyBatis 設定檔構建 SqlSessionFactory。
String resource = "mybatis/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
步驟五:建立 SqlSession 查詢資料
Table Store不支援事務,openSession 的 autoCommit 參數必須設定為 true。
SqlSession session = sqlSessionFactory.openSession(true);
Trip trip = (Trip) session.selectOne("mybatis.TripMapper.selectTrip", 99L);
System.out.println("trip id: " + trip.getTrip_id());
System.out.println("start date: " + trip.getStart_date());
System.out.println("end date: " + trip.getEnd_date());
System.out.println("duration: " + trip.getDuration());
session.close();完整樣本
以下樣本用於查詢表中主鍵列值為 99 的行資料,並擷取指定列的值。
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import mybatis.Trip;
import java.io.IOException;
import java.io.InputStream;
public class MyBatisDemo {
public static void main(String[] args) throws IOException {
String resource = "mybatis/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// Table Store不支援事務,autoCommit 必須設定為 true。
SqlSession session = sqlSessionFactory.openSession(true);
Trip trip = (Trip) session.selectOne("mybatis.TripMapper.selectTrip", 99L);
System.out.println("trip id: " + trip.getTrip_id());
System.out.println("start date: " + trip.getStart_date());
System.out.println("end date: " + trip.getEnd_date());
System.out.println("duration: " + trip.getDuration());
session.close();
}
}