# tablestore-spring-boot-starter
**Repository Path**: larrykoo/tablestore-spring-boot-starter
## Basic Information
- **Project Name**: tablestore-spring-boot-starter
- **Description**: Aliyun TableStore Spring Boot Starter
基于阿里云TableStore(OTS) Spring Boot自动集成
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2021-03-08
- **Last Updated**: 2022-09-06
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# tablestore-spring-boot-starter
Aliyun TableStore Spring Boot Starter
基于阿里云TableStore(OTS) Spring Boot自动集成
----------
使用方式
----------
1. pom.xml中引入
```xml
io.github.kennfalcon
tablestore-spring-boot-starter
0.0.1
```
2. 在application.yml中增加配置
```yaml
tablestore:
endpoint: https://xxx.xxx.ots.aliyuncs.com # 表格存储访问地址
instance: xxx # 表格存储实例名
ak: xxxxx # 阿里云访问AccessKeyId
sk: xxxxx # 阿里云访问AccessKeySecret
```
3. 使用时,会自动注入容器两个Bean
```java
import com.alicloud.openservices.tablestore.SyncClient;
import org.springframework.boot.autoconfigure.tablestore.service.TableStoreService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class A {
@Resource
private SyncClient syncClient;
@Resource
private TableStoreService tableStoreService;
// ...
}
```
其中
- SyncClient为表格存储提供的操作句柄
- TableStoreService是基于表格存储的Java SDK衍生的一组API,可以方便进行CRUD操作,具体TableService使用方式请查看
TableStoreService API
----------
### 1. 注解说明
@Table
```java
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Table {
/**
* 标注该对象映射的表名
*
* @return 该对象对应映射的表名
*/
String name();
/**
* 标准该对象映射的索引名
*
* @return 该对象映射的索引名
*/
String index() default "";
}
```
@OtsColumn
```java
import org.springframework.boot.autoconfigure.tablestore.utils.compress.NoCompress;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface OtsColumn {
/**
* 是否是主键
*
* @return
*/
boolean primaryKey() default false;
/**
* 是否是自增(只有主键可自增)
*
* @return
*/
boolean autoIncrease() default false;
/**
* 表格存储存储的字段名称
*
* @return field name
*/
String name() default "";
/**
* 是否可写
*
* @return
*/
boolean writable() default true;
/**
* 是否可读
*
* @return
*/
boolean readable() default true;
/**
* 存储到OTS中的类型
*
* @return
*/
OtsColumnType type() default OtsColumnType.NONE;
/**
* 压缩(默认不压缩),一般存放byte[]时设置
*
* @return
*/
Class> compress() default NoCompress.class;
}
```
OtsColumnType与OTS类型对应表
| OtsColumnType | PrimaryKeyType | ColumnType |
| :------------- | :---------------- | :---------- |
| STRING | STRING | STRING |
| INTEGER | INTEGER | INTEGER |
| BOOLEAN | _**Not support**_ | BOOLEAN |
| DOUBLE | _**Not support**_ | DOUBLE |
| BINARY | BINARY | BINARY |
若OtsColumnType为空时,字段类型对应表
| Java Data Type | PrimaryKeyType | ColumnType |
| :------------------ | :---------------- | :---------- |
| Short(short) | INTEGER | INTEGER |
| Integer(int) | INTEGER | INTEGER |
| Long(long) | INTEGER | INTEGER |
| Float(float) | _**Not support**_ | DOUBLE |
| Double(double) | _**Not support**_ | DOUBLE |
| Boolean(boolean) | _**Not support**_ | BOOLEAN |
| String | STRING | STRING |
| Byte\[\](byte\[\]) | BINARY | BINARY |
其他类型写入获取读取时,会先转为JSON,以STRING类型写入或读取OTS
### 2. 接口说明
```java
PutRowResponse put(T data, Condition condition)
```
data: 数据
condition: 插入条件
```java
UpdateRowResponse update(T data, Condition condition, boolean deleteNull)
```
data: 数据
condition: 更新条件
deleteNull: 是否删除空值字段
```java
DeleteRowResponse delete(String table, T key, Condition condition)
```
table: 表名
key: 主键,支持(PrimaryKey类型,和自定义类型Bean)
condition: 删除条件