1.使用JDBC訪問數據庫:JDBC是用於在Java語言編程中與數據庫連接的API
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
2.數據源配置
dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
3.配置數據庫
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
4.新建接口
/** * user的service */ public interface IUserService { /** * * @param name * @param age */ int create(String name,Integer age); /** * 根據用戶名刪除用戶 * @param name */ void deleteByName(String name); /** * 獲取用戶總數 * @return */ Integer getUsersCount(); /** * 刪除所有用戶 */ void deleteAllUsers(); }
5.實現接口
@Service public class UserServiceImpl implements IUserService { @Autowired private JdbcTemplate jdbcTemplate;//Spring的JdbcTemplate是自動配置的,可直接使用 @Override public int create(String name, Integer age) { int flag = jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age); return flag; } @Override public void deleteByName(String name) { jdbcTemplate.update("delete from USER where NAME = ?", name); } @Override public Integer getUsersCount() { return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class); } @Override public void deleteAllUsers() { jdbcTemplate.update("delete from USER"); } }
6.測試代碼
/** * 測試數據庫連接 */ @Autowired private IUserService userSerivce; @Test public void testJdbc() throws Exception { // 插入5個用戶 int flag = userSerivce.create("a", 1); System.out.println(flag); userSerivce.create("b", 2); userSerivce.create("c", 3); userSerivce.create("d", 4); userSerivce.create("e", 5); // 查數據庫,應該有5個用戶 Assert.assertEquals(5, userSerivce.getUsersCount().intValue()); // 刪除兩個用戶 userSerivce.deleteByName("a"); userSerivce.deleteByName("e"); // 查數據庫,應該有5個用戶 Assert.assertEquals(3, userSerivce.getUsersCount().intValue()); }
7.多數據源配置
(1) 創建一個Spring配置類,定義兩個DataSource用來讀取application.properties
中的不同配置
/**
* 多數據源配置
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource1")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
//將多數據源注入JdbcTemplate
@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
@Qualifier("primaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
(2)配置文件
#單數據源時spring.datasource.url是可以的,多數據源時要寫成spring.datasource.jdbc-url
#網上這樣說,在2.0升級之后需要變更成:spring.datasource.jdbc-url和spring.datasource.driver-class-name即可解決!
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource1.jdbc-url=jdbc:mysql://localhost:3306/test1
spring.datasource1.username=root
spring.datasource1.password=123456
spring.datasource1.driver-class-name=com.mysql.jdbc.Driver
(3)測試
/***
* 多數據源
*/
@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate1;
@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate2;
@Test
public void test3() throws Exception {
// 往第一個數據源中插入兩條數據
jdbcTemplate1.update("insert into user(name,age) values( ?, ?)", "aaa", 20);
jdbcTemplate1.update("insert into user(name,age) values( ?, ?)", "bbb", 30);
// 往第二個數據源中插入一條數據,若插入的是第一個數據源,則會主鍵沖突報錯
jdbcTemplate2.update("insert into user(id,name,age) values(?,?, ?)", 5,"aaa", 20);
}v