之前介紹了很多Web層的例子届腐,包括構(gòu)建RESTful API、使用Thymeleaf模板引擎渲染W(wǎng)eb視圖碳抄,但是這些內(nèi)容還不足以構(gòu)建一個動態(tài)的應(yīng)用。通常我們做App也好场绿,做Web應(yīng)用也好剖效,都需要內(nèi)容,而內(nèi)容通常存儲于各種類型的數(shù)據(jù)庫焰盗,服務(wù)端在接收到訪問請求之后需要訪問數(shù)據(jù)庫獲取并處理成展現(xiàn)給用戶使用的數(shù)據(jù)形式璧尸。
本文介紹在Spring Boot基礎(chǔ)下配置數(shù)據(jù)源和通過JdbcTemplate編寫數(shù)據(jù)訪問的示例。
數(shù)據(jù)源配置
在我們訪問數(shù)據(jù)庫的時候熬拒,需要先配置一個數(shù)據(jù)源爷光,下面分別介紹一下幾種不同的數(shù)據(jù)庫配置方式。
首先澎粟,為了連接數(shù)據(jù)庫需要引入jdbc支持蛀序,在pom.xml
中引入如下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
嵌入式數(shù)據(jù)庫支持
嵌入式數(shù)據(jù)庫通常用于開發(fā)和測試環(huán)境,不推薦用于生產(chǎn)環(huán)境活烙。Spring Boot提供自動配置的嵌入式數(shù)據(jù)庫有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ù)庫為例回懦,先引入MySQL連接的依賴包气笙,在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ù)源
當你將應(yīng)用部署于應(yīng)用服務(wù)器上的時候想讓數(shù)據(jù)源由應(yīng)用服務(wù)器管理,那么可以使用如下配置方式引入JNDI數(shù)據(jù)源粉怕。
spring.datasource.jndi-name=java:jboss/datasources/customers
使用JdbcTemplate操作數(shù)據(jù)庫
Spring的JdbcTemplate是自動配置的健民,你可以直接使用@Autowired
來注入到你自己的bean中來使用。
舉例:我們在創(chuàng)建User
表贫贝,包含屬性name
秉犹、age
蛉谜,下面來編寫數(shù)據(jù)訪問對象和單元測試用例。
- 定義包含有插入崇堵、刪除型诚、查詢的抽象接口UserService
public interface UserService {
/**
* 新增一個用戶
* @param name
* @param age
*/
void create(String name, Integer age);
/**
* 根據(jù)name刪除一個用戶高
* @param name
*/
void deleteByName(String name);
/**
* 獲取用戶總量
*/
Integer getAllUsers();
/**
* 刪除所有用戶
*/
void deleteAllUsers();
}
- 通過JdbcTemplate實現(xiàn)UserService中定義的數(shù)據(jù)訪問操作
@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)建對UserService的單元測試用例,通過創(chuàng)建鸳劳、刪除和查詢來驗證數(shù)據(jù)庫操作的正確性狰贯。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {
@Autowired
private UserService userSerivce;
@Before
public void setUp() {
// 準備,清空user表
userSerivce.deleteAllUsers();
}
@Test
public void test() throws Exception {
// 插入5個用戶
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查數(shù)據(jù)庫赏廓,應(yīng)該有5個用戶
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 刪除兩個用戶
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查數(shù)據(jù)庫涵紊,應(yīng)該有5個用戶
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}
上面介紹的JdbcTemplate
只是最基本的幾個操作,更多其他數(shù)據(jù)訪問操作的使用請參考:JdbcTemplate API
通過上面這個簡單的例子幔摸,我們可以看到在Spring Boot下訪問數(shù)據(jù)庫的配置依然秉承了框架的初衷:簡單摸柄。我們只需要在pom.xml中加入數(shù)據(jù)庫依賴,再到application.properties中配置連接信息既忆,不需要像Spring應(yīng)用中創(chuàng)建JdbcTemplate的Bean驱负,就可以直接在自己的對象中注入使用。