數(shù)據(jù)源配置
在我們?cè)L問(wèn)數(shù)據(jù)庫(kù)的時(shí)候,需要先配置一個(gè)數(shù)據(jù)源,下面分別介紹一下幾種不同的數(shù)據(jù)庫(kù)配置方式。
首先,為了連接數(shù)據(jù)庫(kù)需要引入jdbc支持,在pom.xml
中引入如下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
#嵌入式數(shù)據(jù)庫(kù)支持
嵌入式數(shù)據(jù)庫(kù)通常用于開(kāi)發(fā)和測(cè)試環(huán)境,不推薦用于生產(chǎn)環(huán)境。Spring Boot提供自動(dòng)配置的嵌入式數(shù)據(jù)庫(kù)有H2、HSQL、Derby,你不需要提供任何連接配置就能使用。
比如,我們可以在pom.xml
中引入如下配置使用HSQL
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
#連接生產(chǎn)數(shù)據(jù)源
以MySQL數(shù)據(jù)庫(kù)為例,先引入MySQL連接的依賴(lài)包,在pom.xml
中加入:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
在src/main/resources/application.properties
中配置數(shù)據(jù)源信息
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#連接JNDI數(shù)據(jù)源
當(dāng)你將應(yīng)用部署于應(yīng)用服務(wù)器上的時(shí)候想讓數(shù)據(jù)源由應(yīng)用服務(wù)器管理,那么可以使用如下配置方式引入JNDI數(shù)據(jù)源。
spring.datasource.jndi-name=java:jboss/datasources/customers
#使用JdbcTemplate操作數(shù)據(jù)庫(kù)
Spring的JdbcTemplate是自動(dòng)配置的,你可以直接使用@Autowired
來(lái)注入到你自己的bean中來(lái)使用。
舉例:我們?cè)趧?chuàng)建User
表,包含屬性name
、age
,下面來(lái)編寫(xiě)數(shù)據(jù)訪問(wèn)對(duì)象和單元測(cè)試用例。
- 定義包含有插入、刪除、查詢(xún)的抽象接口UserService
public interface UserService {
/**
* 新增一個(gè)用戶(hù)
* @param name
* @param age
*/
void create(String name, Integer age);
/**
* 根據(jù)name刪除一個(gè)用戶(hù)高
* @param name
*/
void deleteByName(String name);
/**
* 獲取用戶(hù)總量
*/
Integer getAllUsers();
/**
* 刪除所有用戶(hù)
*/
void deleteAllUsers();
}
- 通過(guò)JdbcTemplate實(shí)現(xiàn)UserService中定義的數(shù)據(jù)訪問(wèn)操作
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void create(String name, Integer age) {
jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
}
@Override
public void deleteByName(String name) {
jdbcTemplate.update("delete from USER where NAME = ?", name);
}
@Override
public Integer getAllUsers() {
return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
}
@Override
public void deleteAllUsers() {
jdbcTemplate.update("delete from USER");
}
}
- 創(chuàng)建對(duì)UserService的單元測(cè)試用例,通過(guò)創(chuàng)建、刪除和查詢(xún)來(lái)驗(yàn)證數(shù)據(jù)庫(kù)操作的正確性。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {
@Autowired
private UserService userSerivce;
@Before
public void setUp() {
// 準(zhǔn)備,清空user表
userSerivce.deleteAllUsers();
}
@Test
public void test() throws Exception {
// 插入5個(gè)用戶(hù)
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查數(shù)據(jù)庫(kù),應(yīng)該有5個(gè)用戶(hù)
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 刪除兩個(gè)用戶(hù)
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查數(shù)據(jù)庫(kù),應(yīng)該有5個(gè)用戶(hù)
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}
上面介紹的JdbcTemplate只是最基本的幾個(gè)操作,更多其他數(shù)據(jù)訪問(wèn)操作的使用請(qǐng)參考:JdbcTemplate API