企業分散式微服務雲SpringCloud SpringBoot mybatis (七)Spring Boot中使用JdbcTemplate訪問資料庫

allalongx發表於2018-02-23

本文介紹在Spring Boot基礎下配置資料來源和通過JdbcTemplate編寫資料訪問的示例。

資料來源配置

在我們訪問資料庫的時候,需要先配置一個資料來源,下面分別介紹一下幾種不同的資料庫配置方式。

首先,為了連線資料庫需要引入jdbc支援,在pom.xml中引入如下配置:

org.springframework.boot spring-boot-starter-jdbc 嵌入式資料庫支援

嵌入式資料庫通常用於開發和測試環境,不推薦用於生產環境。Spring Boot提供自動配置的嵌入式資料庫有H2、HSQL、Derby,你不需要提供任何連線配置就能使用。

比如,我們可以在pom.xml中引入如下配置使用HSQL

org.hsqldb hsqldb runtime 連線生產資料來源

以MySQL資料庫為例,先引入MySQL連線的依賴包,在pom.xml中加入:

mysql mysql-connector-java 5.1.21 在src/main/resources/application.properties中配置資料來源資訊

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資料來源

當你將應用部署於應用伺服器上的時候想讓資料來源由應用伺服器管理,那麼可以使用如下配置方式引入JNDI資料來源。

spring.datasource.jndi-name=java:jboss/datasources/customers 使用JdbcTemplate運算元據庫

Spring的JdbcTemplate是自動配置的,你可以直接使用@Autowired來注入到你自己的bean中來使用。

舉例:我們在建立User表,包含屬性name、age,下面來編寫資料訪問物件和單元測試用例。

定義包含有插入、刪除、查詢的抽象介面UserService public interface UserService {

/**
 * 新增一個使用者
 * @param name
 * @param age
 */
void create(String name, Integer age);

/**
 * 根據name刪除一個使用者高
 * @param name
 */
void deleteByName(String name);

/**
 * 獲取使用者總量
 */
Integer getAllUsers();

/**
 * 刪除所有使用者
 */
void deleteAllUsers();
複製程式碼

}

通過JdbcTemplate實現UserService中定義的資料訪問操作 @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");
}
複製程式碼

}

建立對UserService的單元測試用例,通過建立、刪除和查詢來驗證資料庫操作的正確性。

@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);

	// 查資料庫,應該有5個使用者
	Assert.assertEquals(5, userSerivce.getAllUsers().intValue());

	// 刪除兩個使用者
	userSerivce.deleteByName("a");
	userSerivce.deleteByName("e");

	// 查資料庫,應該有5個使用者
	Assert.assertEquals(3, userSerivce.getAllUsers().intValue());

}
複製程式碼

} 上面介紹的JdbcTemplate只是最基本的幾個操作,更多其他資料訪問操作的使用請參考:JdbcTemplate API

企業分散式微服務雲SpringCloud SpringBoot mybatis (七)Spring Boot中使用JdbcTemplate訪問資料庫
原始碼來源:http://minglisoft.cn/honghu/technology.html

通過上面這個簡單的例子,我們可以看到在Spring Boot下訪問資料庫的配置依然秉承了框架的初衷:簡單。我們只需要在pom.xml中加入資料庫依賴,再到application.properties中配置連線資訊,不需要像Spring應用中建立JdbcTemplate的Bean,就可以直接在自己的物件中注入使用。

相關文章