官方文檔:https://docs.spring.io/spring-data/jdbc/docs/1.0.6.RELEASE/reference/html/
spring data jdbc是spring data產(chǎn)品中的一員, 它提供查詢數(shù)據(jù)庫并映射成實體的功能,類似于jpa,但沒有實體生命周期管理這些復(fù)雜功能, 并且它對領(lǐng)域驅(qū)動設(shè)計提供了一些支持。
配置
添加依賴:
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
// ... ...
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'mysql:mysql-connector-java:8.0.17'
// ... ...
}
配置類:
@Configuration
@EnableJdbcRepositories("com.example.springdatajdbcdemo") //這里的掃描目錄要寫好了
public class JdbcConfig {
}
數(shù)據(jù)庫參數(shù)配置:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
使用
用一個登錄賬號的小例子來演示用法
創(chuàng)建表
create database test;
drop table if exists test.account;
create table test.account(
id bigint primary key auto_increment,
login_name varchar(16) not null ,
password varchar(16) not null
);
創(chuàng)建實體類
spring data jdbc讀取記錄后,會先創(chuàng)建對象,然后給對象賦值
@Data //用lombok來消除模板代碼
public class Account {
@Id //標記屬性為主鍵
private Long id;
private String loginName;
private String password;
//1.如果有無參數(shù)的構(gòu)造函數(shù), spring data jdbc會使用無參數(shù)的構(gòu)造函數(shù)來創(chuàng)建對象
//2.如果只有一個構(gòu)造函數(shù), spring data jdbc會使用它
//3.如果有多個構(gòu)造函數(shù), spring data jdbc會使用有@PersistenceConstructor標記的那個
@PersistenceConstructor
public Account(Long id, String loginName, String password) {
this.id = id;
this.loginName = loginName;
this.password = password;
}
}
創(chuàng)建操作接口Repository
//繼承CrudRepository, 第一個模板參數(shù)是實體類, 第二個參數(shù)是主鍵對應(yīng)的數(shù)據(jù)類型
public interface AccountRepository extends CrudRepository<Account, Long> {
}
讀寫數(shù)據(jù)
可以直接使用CrudRepository中的方法
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataJdbcDemoApplicationTests {
@Autowired
private AccountRepository accountRepository;
@Test
public void testSave() {
Account account = new Account(null, "test", "123456");
account = accountRepository.save(account);
log.info("save ok , id is {}", account.getId());
Optional<Account> dbAccountOptional = accountRepository.findById(account.getId());
Assert.assertTrue(dbAccountOptional.isPresent());
Account dbAccount = dbAccountOptional.get();
log.info("get login account: {}", dbAccount);
Assert.assertEquals("test", dbAccount.getLoginName());
Assert.assertEquals("123456", dbAccount.getPassword());
}
}
也可以自己寫查詢語句:
public interface AccountRepository extends CrudRepository<Account, Long> {
@Query("select * from account where login_name=:loginName")
Optional<Account> getByLoginName(@Param("loginName") String loginName);
}
然后測試一下
@Test
public void testQuery() {
Optional<Account> optionalAccount = accountRepository.getByLoginName("test");
Assert.assertTrue(optionalAccount.isPresent());
Account dbAccount = optionalAccount.get();
log.info("get login account: {}", dbAccount);
Assert.assertEquals("test", dbAccount.getLoginName());
Assert.assertEquals("123456", dbAccount.getPassword());
}
總結(jié)
以上就是spring data jdbc的基本操作了, 總的來說還是比較方便,定義一個實體類,寫個接口,就能實現(xiàn)CRUD了, 沒有spring data jpa復(fù)雜的生命周期, 沒有mybatis那么復(fù)雜的代碼(個人感覺使用mybatis generator也不是很方便)。
但還有許多不足之處, 比如不能使用spring data jpa那樣使用方法名稱查詢, 而@Param標記也不能省略, 還有很多可以優(yōu)化的地方。