Spring Boot中使用JdbcTemplate訪問資料庫
阿新 • • 發佈:2018-12-18
一 資料來源的配置
1 pom中引入JDBC的支援
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2 關於嵌入式資料庫
嵌入式資料庫通常用於開發和測試環境,不推薦用於生產環境。Spring Boot提供自動配置的嵌入式資料庫有H2、HSQL、Derby,不需要提供任何連線配置就能使用。
如果要引入HSQL,pom配置如下:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
3 關於MySQL資料來源
如果引入MySQL,在pom.xml中加入下面依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
二 實戰
1 新增依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies>
2 服務層
1 介面定義
package com.didispace.service;
public interface UserService {
/**
* 新增一個使用者
* @param name
* @param age
*/
void create(String name, Integer age);
/**
* 根據name刪除一個使用者高
* @param name
*/
void deleteByName(String name);
/**
* 獲取使用者總量
*/
Integer getAllUsers();
/**
* 刪除所有使用者
*/
void deleteAllUsers();
}
2 介面的實現類
package com.didispace.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@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");
}
}
3 編寫啟動類
package com.didispace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4 編寫測試類
package com.didispace;
import com.didispace.service.UserService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@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());
}
}
三 執行測試類
執行後,資料庫結果如下:
四 參考