1
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 1 year has passed since last update.

【メモ】DAOパターンのファイル一例【Java】

Posted at

TaskDaoImpl
@Repository
public class TaskDaoImpl implements TaskDao {

	private final JdbcTemplate jdbcTemplate;

	public TaskDaoImpl(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public List<Task> findAll() {

	  String sql = "SELECT task.id, user_id, type_id, title, detail, deadline, "
			+ "type, comment FROM task "
			+ "INNER JOIN task_type ON task.type_id = task_type.id";

	  //削除してください

	  //タスク一覧をMapのListで取得
	  List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);

	  //return用の空のListを用意
	  List<Task> list = new ArrayList<>();

	  //二つのテーブルのデータをTaskにまとめる
	  for(Map<String, Object> result : resultList) {

		Task task = new Task();
		task.setId((int)result.get("id"));
		task.setUserId((int)result.get("user_id"));
		task.setTypeId((int)result.get("type_id"));
		task.setTitle((String)result.get("title"));
		task.setDetail((String)result.get("detail"));
		task.setDeadline(((Timestamp) result.get("deadline")).toLocalDateTime());

		TaskType type = new TaskType();
		type.setId((int)result.get("type_id"));
		type.setType((String)result.get("type"));
		type.setComment((String)result.get("comment"));

		//TaskにTaskTypeをセット

		list.add(task);
		}
	  return list;
	}

}
1
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
1
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?