m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Spring Bootでデータベースに接続

そろそろDB

Javaって他の言語より導入が面倒な部分が感じられるのでなかなか進まない

環境

Spring Boot 1.4
MySQL 5.x
MySQLvagrantに配置して接続してみる

DB設定

DDL作成
create table if not exists memo (
  id int not null auto_increment,
  name varchar(30) not null,
  memo varchar(128),
  create_at datetime not null default current_timestamp,
  update_at datetime not null default current_timestamp on update current_timestamp,
  primary key (id)
) engine = INNODB;
テストデータを作成
insert into `memo`(name, memo) values('test01', 'hogehoge01');
insert into `memo`(name, memo) values('test02', 'hogehoge02');
insert into `memo`(name, memo) values('test03', 'hogehoge03');
insert into `memo`(name, memo) values('test04', 'hogehoge04');
insert into `memo`(name, memo) values('test05', 'hogehoge05');
insert into `memo`(name, memo) values('test06', 'hogehoge06');
insert into `memo`(name, memo) values('test07', 'hogehoge07');
insert into `memo`(name, memo) values('test08', 'hogehoge08');
insert into `memo`(name, memo) values('test09', 'hogehoge09');
insert into `memo`(name, memo) values('test10', 'hogehoge10');

maven設定

pom.xml
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		
	</dependencies>

jpaMySQLの情報を追加

application.yml
server:
  port: 7777
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.33.10/myapp?useSSL=false
    username: app
    password: Password123@
  jpa:
    hibernate:
      show-sql: true
      ddl-auto: update
    database-platform: org.hibernate.dialect.MySQLDialect

MySQLの接続設定を追加

画面用(ホントはいらないかも…)

MemoForm.java
package com.example.beans;

import java.io.Serializable;
import java.sql.Timestamp;

public class MemoForm implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3570891756024535003L;

	private Integer id;
	private String name;
	private String memo;
	private Timestamp createAt;
	private Timestamp updateAt;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public Timestamp getCreateAt() {
		return createAt;
	}

	public void setCreateAt(Timestamp createAt) {
		this.createAt = createAt;
	}

	public Timestamp getUpdateAt() {
		return updateAt;
	}

	public void setUpdateAt(Timestamp updateAt) {
		this.updateAt = updateAt;
	}
	
}

エンティティ準備

Memo.java
package com.example.entities;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "memo")
public class Memo implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3570891756024535003L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	
	@Column(name="name")
	private String name;
	
	@Column(name="memo")
	private String memo;
	
	@Column(name="create_at")
	private Timestamp createAt;

	@Column(name="update_at")
	private Timestamp updateAt;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public Timestamp getCreateAt() {
		return createAt;
	}

	public void setCreateAt(Timestamp createAt) {
		this.createAt = createAt;
	}

	public Timestamp getUpdateAt() {
		return updateAt;
	}

	public void setUpdateAt(Timestamp updateAt) {
		this.updateAt = updateAt;
	}
	
}

リポジトリ

MemoRepository.java
package com.example.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.entities.Memo;

public interface MemoRepository extends JpaRepository<Memo, Long>{
	public Memo findById(Integer id);
}

サービス

MemoService.java
package com.example.services;

import java.sql.Timestamp;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.beans.MemoForm;
import com.example.entities.Memo;
import com.example.repositories.MemoRepository;

@Service
@Transactional
public class MemoService {
	@Autowired
	MemoRepository repository;
	
	/**
	 * 全件取得
	 * @return
	 */
	public List<Memo> getAllList() {
		return repository.findAll();
	}
	
	/**
	 * 1件取得
	 * @return
	 */
	public MemoForm getMemo(Integer id) {
		Memo newMemo = repository.findById(id);
		
		MemoForm form = new MemoForm();
		form.setId(newMemo.getId());
		form.setName(newMemo.getName());
		form.setMemo(newMemo.getMemo());
		form.setCreateAt(newMemo.getCreateAt());
		form.setUpdateAt(newMemo.getUpdateAt());
		
		return form;
	}
	
	/**
	 * 追加
	 * @param memo
	 * @return
	 */
	public Memo regist(MemoForm form) {
		
		// 新しく作成して値を設定
		Memo memo = new Memo();
		memo.setName(form.getName());
		memo.setMemo(form.getMemo());
		memo.setCreateAt(new Timestamp(System.currentTimeMillis()));
		memo.setUpdateAt(new Timestamp(System.currentTimeMillis()));
		
		// 登録
		return repository.save(memo);
	}
	
	/**
	 * 追加
	 * @param memo
	 * @return
	 */
	public Memo update(MemoForm form) {
		
		// 新しく作成して値を設定
		Memo memo = new Memo();
		memo.setId(form.getId());
		memo.setName(form.getName());
		memo.setMemo(form.getMemo());
		memo.setCreateAt(form.getCreateAt());
		memo.setUpdateAt(new Timestamp(System.currentTimeMillis()));
		
		// 更新
		return repository.save(memo);
	}
	
	/**
	 * 削除
	 * @param id
	 */
	public void remove(Integer id) {
		Memo memo = new Memo();
		memo.setId(id);
		repository.delete(memo);
	}
}

コントローラー

MemoController.java
package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.example.beans.MemoForm;
import com.example.entities.Memo;
import com.example.services.MemoService;

@Controller
@RequestMapping("memo")
public class MemoController {
	
	@Autowired
	private MemoService memoService;
	
	/**
	 * 一覧表示
	 * @return
	 */
	@RequestMapping(method = {RequestMethod.GET})
	public ModelAndView index() {
		
		// 一覧を取得
		List<Memo> list = memoService.getAllList();
		
		// Modelを生成
		ModelAndView mv = new ModelAndView();
		
		// 取得したデータを画面へ割当
		mv.addObject("list", list);
		
		// 使用するビューを指定
		mv.setViewName("memo/index");
		
		// view
		return mv;
	}
	
	/**
	 * 新規画面
	 * @return
	 */
	@RequestMapping(value = {"new"}, method = {RequestMethod.GET})
	public ModelAndView _new(@ModelAttribute MemoForm form) {
		
		// Modelを生成
		ModelAndView mv = new ModelAndView();
				
		// 使用するビューを指定
		mv.setViewName("memo/new");
		
		// formを設定する
		mv.addObject("form", form);
		
		// view
		return mv;
	}
	
	/**
	 * 登録処理
	 * @return
	 */
	@RequestMapping(value = {"regist"}, method = {RequestMethod.POST})
	public String regist(@ModelAttribute MemoForm form) {
		
		// 追加処理
		memoService.regist(form);
		
		// view
		return "redirect:/memo";
	}
	
	/**
	 * 編集画面
	 * @param id
	 * @return
	 */
	@RequestMapping(value = {"edit/{id}"}, method = {RequestMethod.GET})
	public ModelAndView edit(@PathVariable("id") String id) {
		
		MemoForm form = memoService.getMemo(Integer.parseInt(id));
		
		// Modelを生成
		ModelAndView mv = new ModelAndView();
						
		// 使用するビューを指定
		mv.setViewName("memo/edit");
			
		// formを設定する
		mv.addObject("form", form);
				
		// view
		return mv;
	}
	
	/**
	 * 更新処理
	 * @return
	 */
	@RequestMapping(value = {"update"}, method = {RequestMethod.POST})
	public String update(@ModelAttribute MemoForm form) {
		
		// 更新処理
		memoService.update(form);
		
		// view
		return "redirect:/memo";
	}
	
	/**
	 * 削除実施
	 * @param id
	 * @return
	 */
	@RequestMapping(value = {"remove/{id}"}, method = {RequestMethod.GET})
	public String remove(@PathVariable("id") String id) {
		
		// 削除実行
		memoService.remove(Integer.parseInt(id));
		
		// 一覧へリダイレクトする
		return "redirect:/memo";
	}
	
}

ビュー

memo/index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Sample Memo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Memo Sample</h1>
	<div>
		<a href="/memo/new">new</a>
    	<table>
    		<tbody th:if="${list} != null">
    			<tr th:each="memo: ${list}">
    				<td th:text="${memo.id}"><br /></td>
    				<td th:text="${memo.name}"><br /></td>
    				<td th:text="${memo.memo}"><br /></td>
    				<td th:text="${memo.createAt}"><br /></td>
    				<td th:text="${memo.updateAt}"><br /></td>
    				<td>
    					<a th:href="@{/memo/edit/{id}(id=${memo.id})}">edit</a>
    				</td>
    				<td>
    					<a th:href="@{/memo/remove/{id}(id=${memo.id})}">remove</a>
    				</td>
    			</tr>
    		</tbody>
    	</table>
	</div>
</body>
</html>
memo/new.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Sample Memo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Memo Sample</h1>
	<div>
    	<form th:action="@{/memo/regist}" method="post">
    		<table>
    			<tr>
    				<th>name</th>
    				<td>
    					<input type="text" name="name" />
    				</td>
    			</tr>
    			<tr>
    				<th>memo</th>
    				<td>
    					<input type="text" name="memo" />
    				</td>
    			</tr>
    		</table>
    		<input type="submit" value="登録する" />
    	</form>
	</div>
</body>
</html>
memo/edit.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Sample Memo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Memo Sample</h1>
	<div>
    	<form th:action="@{/memo/update}"  method="post">
    		<input type="hidden" name="id" th:field="${form.id}" />
    		<input type="hidden" name="createAt" th:field="${form.createAt}" />
    		<table>
    			<tr>
    				<th>name</th>
    				<td>
    					<input type="text" name="name" th:field="${form.name}" />
    				</td>
    			</tr>
    			<tr>
    				<th>memo</th>
    				<td>
    					<input type="text" name="memo" th:field="${form.memo}" />
    				</td>
    			</tr>
    		</table>
    		<input type="submit" value="更新する" />
    	</form>
	</div>
</body>
</html>

実行

所感

さわりさわりなのでまだ違和感がある。
なんとかここまでこぎつけた。まだ、認証とかバリデーションとかあるのでそのあたりも調べないといけない
結構やること多いです(´・ω・`)