Todoのまとめ

・Todoサービスのまとめ

Dependency Injection: 依存性の注入
・thymeleaf(たいむりーふ)の依存
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Hibernateを使えるようになるための依存
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

・データベースの連結の依存
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

YAMLでデータベースの設定
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/todo?characterEncoding=UTF-8&serverTimezone=JST
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    hibernate:
     naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

・画面遷移の設定
ボタンを押すと、指定画面に遷移しもす。
@Controller
public class PageController {

    @RequestMapping("/other")
    public String other() {
        return "redirect:/";
    }

    @RequestMapping("/jump_to_add")
    public String add() {
        return "redirect:/add";
    }

    @RequestMapping("/edit/other")
    public String delete() {
        return "redirect:/";
    }
}

・時間の取得
Controllerの最初定義すると、時間の更新はサーバの起動時間になってしまいました。
ですから、新しいクラスを作成します。
public class Get {
    public static String time() {
     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     String time = sdf.format(timestamp);
     return time;
    }
}

・Todo画面
repository.findAllは全てのデータを取得します。
そして、mav.addObjectで値を保存します。

@RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index(@ModelAttribute TodoData tododata, ModelAndView mav) {
        mav.setViewName("index");
        Iterable<TodoData> list = repository.findAll();
        mav.addObject("datalist", list);
        return mav;
    }

追加画面へ遷移します。
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add(ModelAndView mav) {
        mav.setViewName("add");
        return mav;
    }

Getクラスで時間をもらって、時間を設定して、データを保存します。
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    public ModelAndView upload(@ModelAttribute TodoData tododata, ModelAndView mav) {
        tododata.setCreatedDate(Get.time());
        tododata.setUploadDate(Get.time());
        repository.saveAndFlush(tododata);
        return new ModelAndView("redirect:/");
    }

編集画面に遷移して、テキストのIDを検索して、検索結果を保存します。
    @RequestMapping(value = "/edit/{todoid}", method = RequestMethod.GET)
    public ModelAndView edit(@ModelAttribute TodoData tododata, @PathVariable int todoid, ModelAndView mav,
            String created_date, String user_id) {
        mav.setViewName("edit");
        Optional<TodoData> data = repository.findById*1;
        return mav;
    }

Getクラスで時間をもらって、時間を設定して、データを保存します。
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    public ModelAndView update(@ModelAttribute TodoData tododata, ModelAndView mav) {
        tododata.setUploadDate(Get.time());
        repository.saveAndFlush(tododata);
        return new ModelAndView("redirect:/");
    }

*1:long) todoid);
        mav.addObject("formModel", data.get(