0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Spring Boot]Spring Data JDBCでDBにアクセスする

Last updated at Posted at 2021-06-20

SpringDataJDBCでDBにアクセスする

Spring BootでMySQLの操作を行います。データベースアクセスには Spring Data JPA と Spring Data JDBC の2つあるようですが、SQLを書く練習をしたいのでJDBCを使っていきます。
今回はXAMPPのMySQLのデータベース(test)のテーブル(item)にしてみます。


1. Springで Spring Data JDBCを使うポイント

大事なポイントは以下の3点のみです。

  • Spring Starterプロジェクトで 「Spring Data JDBC」 と 「MySQL Driver」 を追加する
  • DB接続情報は src/main/resource/application.propeties に追加する
  • JdbcTempalteのメソッドを実行する

2. DB接続情報を設定する

DB接続情報を src/main/resource/application.propeties に追加します。他にも方法があるようですが最も簡単な方法で実現します。

[src/main/resource/application.propeties]

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=(ユーザー名)
spring.datasource.password=(パスワード)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

3. JdbcTemplateのメソッド

色々なメソッドがありますが、このメソッドを抑えておけば大丈夫そうです。

やりたいこと メソッド
SELECT query、queryForList、queryForObject
INSERT update
UPDATE update
DELETE update

3.1. Queryの書き方(SELECT文)

【JdbcTemplate】Queryの書き方(SELECT文) を参考にします。

SELECTの方法は 3通り あります。

  • BeanPropertyRowMapper
  • RowMapper
  • Map

Mapを使えば複雑なSQLを実行して結果を取得できそうです。
@lombok.Data アノテーションを付けたBeanなら BeanPropertyRowMapper を使う方法が簡単です。今回は、 BeanPropertyRowMapper + query メソッド を使ってSELECT文を作成します。

Item.java

@lombok.Data
public class Item {
	private Integer id;
	private String name;
}

ItemDao.java

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

//DBアクセスのコンポーネントには @Respository を付ける
@Repository
public class ItemDao {

  //@Autowiredを使ってインスタンスを注入すうr
	@Autowired
	private JdbcTemplate jdbcTemplate;

  //全件取得する場合は、queryか queryForList メソッドを利用します。
  public List<Item> selectAll() {
    //全件取得のSQLを作成
    String sql = "select * from item;";
    //SQLを実行
    return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Item>(Item.class));
  }

  //1件だけ取得したい場合は queryForObject メソッドを利用します。
  public Item selectOne(Integer id) {
    //PKを使って1件のみ取得
    String sql = "select * from item where id=?;";
    //SQLを実行
    return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Item>(Item.class), id);
  }

	//DBを更新したい場合は update メソッドを利用します。
  public int insertOne(Item item) {

		//1件を新規追加するSQLを作成
    String sql = "insert into item (id, name) values (?, ?);";
		//SQLを実行
		return jdbcTemplate.update(sql, item.getId(), item.getName());
	}

	//DBを更新したい場合は update メソッドを利用します。
	public int updateOne(Item item) {
		//PKを使って1件のみ更新
		String sql = "update item set name=? where id=?;";
		//SQLを実行する
		return jdbcTemplate.update(sql, item.getName(), item.getId());
	}

	//DBを削除したい場合は update メソッドを利用します。
	public int deleteOne(Integer id) {
		//PKを使って1件のみ削除
		String sql = "delete from item where id=?;";
		//SQLを実行する
		return jdbcTemplate.update(sql, id);
	}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?