Spring 02

JSPとSpring Boot

JSP maven
<!-- spring boot tomcat jsp-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

Servlet maven

<!--servlet-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
</dependency>

JSTL maven
<!-- jstl-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>


JSPを利用するために、JSPのフォルダーを指定します。
application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

下記のは、もし/indexへ遷移したら、springmvcがJSPのフォルダーのindex.jspを探します。

@Controller
public class IndexController {
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "index";
    }
}

@Controller
使用者からもらったURLを対応のサービスに渡します。よくRequestMappingと一緒に使われています。

@RequestMapping
ルーティング情報を提供します。


・SpringBootがSpringDataJPAでCRUDを作ります。
<!--mysql-connector-java-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- spring data jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!--tomcat-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <!--<scope>provided</scope>-->
</dependency>

内部のtomcatで実行する場合は、scopeを削除します。

@ResponseBody
戻り値をHTTP response bodyに書き込みます。

@RestController
JsonXML等を返すWebAPI用のコントローラで使用します。


・DataBaseの接続とJPA(Java Persistence API )
下記の二つは同じ機能ですが、一つのコードは長いです。もう一つは短くて、理解やすいです。しかし、yaml形式の定義ファイルはpropertiesのルールより厳しいです。一つスペースも影響されています。

(application.properties)
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=matt
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

(application.yml)
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: matt
    password: 12345
    driver-class-name: com.mysql.cj.jdbc.Driver

Hibernate
Java のためのORM(Object-Relational Mapping) ライブラリであり、オブジェクト指向ドメインモデルを関係データベースにマッピングするためのフレームワークを提供します。

@Entity
テーブルを指定します。

@Table
よく@Entityと一緒に使われています。

@id
一つテーブルは一つprimary keyしかありません。

@GeneratedValue
primary key生成方法です。AUTO、INDENTITY、SEQUENCEとTABLE 四つ方法があります。既定値はAUTOです。

@Column
特定のデータを指定します。特定の属性を指定することができます。今回は使用しません。

@Autowired
Beanから自動的に導入します。

JpaRepository
SpringDataJPAが提供した簡単なデータ操作インターフェースです

JpaSpecificationExecutor
SpringDataJPAが提供した複雑な検査インターフェースです。

Serializable
直列化インターフェースです。

・DataBaseの接続問題
原因:mysql-connector-java の5.1.33 ~ 37 のバグでした。
詳しいページ:https://www.nakamuri.info/mw/index.php/Mysql-connector-java_%E3%81%AE%E3%83%90%E3%82%B0%E3%81%A7_Java%E3%81%8B%E3%82%89MySQL%E3%81%AB%E6%8E%A5%E7%B6%9A%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%84
以前
url: jdbc:mysql://localhost:3306/test_01?characterEncoding=utf8
現在
url: jdbc:mysql://localhost:3306/test_01?characterEncoding=UTF-8&serverTimezone=JST


CRUDを作成しました。
バグ:Create機能は実行できません。
原因:sequenceでprimary keyを生成するのは、MySQLに対してサポートしません。MySQLはidentityを使っています。
解決方法:@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)