Java -repository
Javaあんまさわらず忘れるからめも あとで追記する
Repository DB接続を行うクラス (DAO
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.stereotype.Repository; import com.xxx.domain.Xxxx;
@Autowired :インジェクション 自動でインスタンスセット
Named~
id = :id";
プレースホルダ
@Repository public class XxxxRepository { @Autowired private NamedParameterJdbcTemplate temp;
//検索処理をする private static final RowMapper<Xxxx> xxxRowMapper = (rs,i) -> { return new Xxxx (rs.getInt("id"),rs.getString("~~name"), rs.getString("~~~"),rs.getString("~~~")); };
//全件検索 public List<Xxxx> findAll() { String sql = "SELECT id, ~~~, ~~~~~ FROM table ORDER BY id"; List<Xxxx> list = temp.query(sql,xxxxRowMapper); return list; };
//1件取得 public Xxxx load (int id) { String sql = "SELECT * FROM xxx WHERE id = :id"; SqlParameterSource param = new MapSqlParameterSource().addValue("id", id); Xxxx xxxx = temp.queryForObject(sql, param, xxxxRowMapper); return xxxx; } }