Language: English | 简体中文
Official website: https://rabbit-sql.com
Get Best practice to quick start.
Spring-boot autoconfigure starter based on rabbit-sql, use spring managed transaction as default, use @Transactional annotation or inject com.github.chengyuxing.sql.spring.autoconfigure.Tx (simple wrapper for spring transaction) to use transaction.
- compatible with spring jdbc transaction;
- compatible with mybatis, spring-data-jpa and so on to use transaction together;
Tx, use spring transaction instead.
com.github.chengyuxing.sql.transaction.Tx❌- com.github.chengyuxing.sql.spring.autoconfigure.Tx ✅
get more usage about rabbit-sql from document。
java 8
<dependency>
<groupId>com.github.chengyuxing</groupId>
<artifactId>rabbit-sql-spring-boot-starter</artifactId>
<version>5.3.20</version>
</dependency>Dynamic sql test:
Xql interface mapper generate:
Plugin marketplace: Rabbit sql and documentation.
application.yml (spring.datasource is required):
spring:
datasource:
url: jdbc:postgresql://127.0.0.1:5432/postgres
username: chengyuxingInject Baki ready to use:
@Autowired
Baki baki;begin with input baki to edit application.yml, a simple example look like:
application.yml
baki:
xql-file-manager:
files:
a: mydir/one.sql
b: mydir/two.sqlBy implements the property interface of BakiDao which annotation @Component or @Bean defined in @Configuration to autowire.
@Component
public class RedisCacheManager implements QueryCacheManager {
final RedisTemplate<Object, Object> redisTemplate;
public RedisCache(RedisTemplate<Object, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public @NotNull Stream<DataRow> get(@NotNull String sql, Map<String, ?> args, @NotNull RawQueryProvider provider) {
...
}Working with Rabbit sql plugin
- Remove the
xql-file-managerproperty fromapplication.yml; - Sql file extension must rename to
xql; - Create
xql-file-manager.ymlin resource root.../src/main/resources; - Configure properties.
Here is two way to use:
- Inject core interface
Baki; - Springboot application startup class annotated with
@XQLMapperScan, create interface mappring to xql file, inject interface e.gExampleMapper.java
@SpringBootApplication
@XQLMapperScan
public class Startup implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Startup.class, args);
}
@Autowired
Baki baki;
@Autowired
ExampleMapper exampleMapper;
@Override
public void run(String... args) throws Exception {
try (Stream<DataRow> s = baki.query("&a.region").arg("id", 5).stream()) {
s.forEach(System.out::println);
}
}
}work with spring transaction:
@Service
public class MyService {
@Autowired
Baki baki;
// com.github.chengyuxing.sql.spring.autoconfigure.Tx
@Autowired
Tx tx;
@Transactional
public void some() {
...
}
public void b(){
tx.using(()->{
...
});
}
}Dynamic configure the xql config by command line arguments:
-
xql.config.location : dynamic specify the
XQLFileManagerconfig location -
xql.config.constants. :add or replace the
constants's property value which inXQLFileManagerxql-file-manager.ymlconstants: db: dev
command line argument:
--xql.config.constants.db=prod


