https://github.com/yugabyte/spring-data-yugabytedb
Spring Data Module for YugabyteDB.
https://github.com/yugabyte/spring-data-yugabytedb
Last synced: 12 months ago
JSON representation
Spring Data Module for YugabyteDB.
- Host: GitHub
- URL: https://github.com/yugabyte/spring-data-yugabytedb
- Owner: yugabyte
- Created: 2019-05-31T16:41:50.000Z (about 7 years ago)
- Default Branch: sdyb-main
- Last Pushed: 2021-08-30T11:10:46.000Z (almost 5 years ago)
- Last Synced: 2025-04-23T23:38:55.446Z (about 1 year ago)
- Language: Java
- Homepage:
- Size: 623 KB
- Stars: 18
- Watchers: 81
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.adoc
Awesome Lists containing this project
- awesome - yugabyte/spring-data-yugabytedb - Spring Data Module for YugabyteDB. (<a name="Java"></a>Java)
README
= Spring Data YugabyteDB
Spring Data YugabyteDB brings the power of Distributed SQL to Spring developers by using the familar Spring Data paradigms, Spring Data YugabyteDB supports Spring template and Spring repositories for accessing the data from YugabyteDB. Spring Data YuabyteDB is based on the [Spring Data JDBC](https://github.com/spring-projects/spring-data-jdbc) project which is extended to support YugabyteDB distributed SQL features.
YugabyteDB is a high-performance, cloud-native distributed SQL database that aims to support all PostgreSQL features. It is best to fit for cloud-native OLTP (i.e. real-time, business-critical) applications that need absolute data correctness and require at least one of the following: scalability, high tolerance to failures, or globally-distributed deployments.
== Features
In addition to providing most PostgreSQL features supported by Spring Data JDBC, this project aims to enable the following:
* CRUD operations for YugabyteDB
* `@EnableYsqlRepositories` annotation to enable `YsqlRepository`
* Yugabyte Distributed SQL transaction manager
* Support for Follower Reads
* Eliminate Load Balancer from SQL (cluster-awareness)
* Develop Geo-Distributed Apps (topology-awareness)
* Row Level Geo-partitioning support (partition-awareness)
=== Getting Started
A quick start example of getting started with Spring Data YugabyteDB in JAVA:
[source, java]
----
public interface ShoppingCartRepository extends YsqlRepository {
ShoppingCart findById(String id);
List findByUserId(String userId);
}
@Service
public class CartService {
private final ShoppingCartRepository repository;
public CartService(CartService repository) {
this.repository = repository;
}
public void doWork() {
repository.deleteAll();
ShoppingCart myShoppingCart = new ShoppingCart();
myShoppingCart.set("cart1")
myShoppingCart.setUserId("u1001");
myShoppingCart.setProductId("asin1001");
myShoppingCart.setQuantity(1);
repository.save(myShoppingCart);
ShoppingCart savedCart = repository.findById("cart1");
List productsInCart = repository.findByUserId("u1001");
}
}
@Configuration
@EnableYsqlRepositories
public class YsqlConfig extends AbstractYugabyteJdbcConfiguration {
@Bean
DataSource dataSource() {
String hostName = "127.0.0.1";
String port = "5433";
Properties poolProperties = new Properties();
poolProperties.setProperty("dataSourceClassName", "com.yugabyte.ysql.YBClusterAwareDataSource");
poolProperties.setProperty("dataSource.serverName", hostName);
poolProperties.setProperty("dataSource.portNumber", port);
poolProperties.setProperty("dataSource.user", "yugabyte");
poolProperties.setProperty("dataSource.password", "");
poolProperties.setProperty("dataSource.loadBalance", "true");
poolProperties.setProperty("dataSource.additionalEndpoints",
"127.0.0.2:5433,127.0.0.3:5433");
HikariConfig hikariConfig = new HikariConfig(poolProperties);
DataSource ybClusterAwareDataSource = new HikariDataSource(hikariConfig);
return ybClusterAwareDataSource;
}
@Bean
JdbcTemplate jdbcTemplate(@Autowired DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
return jdbcTemplate;
}
@Bean
NamedParameterJdbcOperations namedParameterJdbcOperations(DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
@Bean
TransactionManager transactionManager(DataSource dataSource) {
return new YugabyteTransactionManager(dataSource);
}
}
----
=== Maven configuration
Add the Maven dependency:
[source,xml]
----
com.yugabyte
spring-data-yugabytedb-ysql
2.3.0
com.yugabyte
jdbc-yugabytedb
42.2.7-yb-5.beta.5
----
See also the https://github.com/yugabyte/spring-data-yugabytedb-example[spring-data-yugabytedb-example] app.