# SpringBoot 3/25 上課內容
###### tags: `SpringBoot`
[老師雲端](https://onedrive.live.com/?authkey=%21AOSC5VqK5wFJeuk&id=AF483E7E8FEC387B%218460&cid=AF483E7E8FEC387B)
# 31 Spring Boot 動態網站開發實務

* 要注意:正式上線不會有這段
## Spring Boot 動態網站開發實務

* static
* templates存放:樣板程式 網頁程式

# 32 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 33 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 34 Spring Boot 動態網站開發實務


## Spring Boot 動態網站開發實務


# 35 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 36 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 37 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 38 Spring Boot 動態網站開發實務

* produces = 回傳格式
## Spring Boot 動態網站開發實務

# 39 Spring Boot 動態網站開發實務

# 40 Spring Boot 動態網站開發實務

[starters](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter)

## Spring Boot 動態網站開發實務

# 41 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 42 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 43 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 44 Spring Boot 動態網站開發實務


## Spring Boot 動態網站開發實務

# 45 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 46 Spring Boot 動態網站開發實務

# 47 Spring Boot 動態網站開發實務

[ORM介紹及ORM優點、缺點](http://blog.twbryce.com/what-is-orm/)
## Spring Boot 動態網站開發實務

# 48 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 49 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 50 Spring Boot 動態網站開發實務

[Repository](https://docs.spring.io/spring-data/commons/docs/2.4.6/api/org/springframework/data/repository/Repository.html)
## Spring Boot 動態網站開發實務

# 51 Spring Boot 動態網站開發實務

* save:會自動更新或新增
* existsById:自動判斷存在再更新
## Spring Boot 動態網站開發實務

# 52 Spring Boot 動態網站開發實務

* deletelnBatch:可分批一次刪除
## Spring Boot 動態網站開發實務

# 53 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 54 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 55 Spring Boot 動態網站開發實務

## Spring Boot 動態網站開發實務

# 56 Spring Boot 動態網站開發實務

# <font color="green">------------實作部份------------</font>

* 在css建會跑到webapp在把他拉上去



#### color.css
```clike=
@charset "UTF-8";
#red{color:red;}
#green{color:green;}
#blue{color:blue;}
```
#### welcome.html
```clike=
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
<link href="css/color.css" rel="stylesheet"></link>
</head>
<body>
<div id="red">Welcome</div>
<div id="green">Today would be a great day.</div>
<img alt="bigcat" src="images/bigcat.jpg" width="320" height="280" />
</body>
</html>
```
#### index.jsp
```clike=
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
hello<br/>
</body>
</html>
```
#### application.properties
```clike=
#Server port
server.port=8081
# 80 可以不用通訊埠 http://localhost/databaseproperties.Controller
#server.port=80
#data setup資料設定
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
#Jpa SQLServer Database Configuration
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=LeonPower
spring.datasource.username=watcher
spring.datasource.password=P@ssw0rd
#Jpa MySQL Database Configuration
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/LeonPower
#spring.datasource.username=watcher
#spring.datasource.password=P@ssw0rd
#oracle localhost:1521
#Spring JPA Configuration
spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
#spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
name=mary
age=18
#profiles information
profiles.username=alice
profiles.address=USA
profiles.phone=N/A
#http://localhost:8081/
#http://localhost:8081/images/bigcat.jpg
#自訂路徑 http://localhost:8081/static/images/bigcat.jpg
##spring.web.resources.static-locations=classpath:/
##spring.mvc.static-path-pattern=/**
#自訂路徑變 http://localhost:8081/bigcat.jpg
#spring.web.resources.static-locations=classpath:/static/images/
# 多路徑寫法都可用 設定不能重複
#spring.web.resources.static-locations=classpath:/static/images,classpath:/static/,classpath:/
#spring.mvc.static-path-pattern=/**
# 自訂想要的名稱or註解掉
#server.servlet.context-path=/OrderSystem
```
#### jdbc.properties


```clike=
database.drivername=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc:sqlserver://localhost:1433;databaseName=LeonPower
database.username=watcher
database.password=P@ssw0rd
```
#### DataBaseProperties.java
* 要搭配 pom.xml

```clike=
package tw.leonchen.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "database")
public class DataBaseProperties {
private String drivername;
private String url;
private String username;
private String password;
public String getDrivername() {
return drivername;
}
public void setDrivername(String drivername) {
this.drivername = drivername;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
#### DemoSpringBootWebProjectApplication.java
```clike=
package tw.leonchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@ServletComponentScan //ignoreResourceNotFound 忽略找不到資源
@PropertySource(value = {"classpath:jdbc.properties"},ignoreResourceNotFound = true)
public class DemoSpringBootWebProjectApplication {
public static void main(String[] args) {
SpringApplication.run(DemoSpringBootWebProjectApplication.class, args);
}
}
```
#### DataBasePropertiesController.java

```clike=
package tw.leonchen.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tw.leonchen.model.DataBaseProperties;
@RestController
public class DataBasePropertiesController {
@Autowired
private DataBaseProperties dbProps;
@GetMapping("/databaseproperties.Controller")
public DataBaseProperties processPropertiesAction() {
return dbProps;
}
}
```
#### pom.xml
[Apache Commons IO » 2.8.0](https://mvnrepository.com/artifact/commons-io/commons-io/2.8.0)
```clike=
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
```
#### ResourcesController.java
```clike=
package tw.leonchen.controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
//第一種 import org.springframework.core.io.ClassPathResource;
//第二種 import org.springframework.beans.factory.annotation.Autowired;
//第二種 import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.Resource; //第三種
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ResourcesController {
//第二種 @Autowired
//第二種 private ResourceLoader resourceLoader;
@Value("classpath:static/images/bigcat.jpg")//第三種
private Resource resource;//第三種
@GetMapping(path = "/resources.controller" ,produces = {MediaType.IMAGE_JPEG_VALUE})
public byte[] processResourcesAction()throws IOException {
//第一種 Resource resource = new ClassPathResource("/static/images/bigcat.jpg");
//第二種 Resource resource = resourceLoader.getResource("classpath:static/images/bigcat.jpg");
File file = resource.getFile();
System.out.println(file.getName() + ":" + file.getPath() + ":" + file.length());
InputStream is1 = resource.getInputStream();
return IOUtils.toByteArray(is1);
}
}
```
#### pom.xml tomcat->jetty



```clike=
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>DemoSpringBootWebProject</groupId>
<artifactId>DemoSpringBootWebProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>DemoSpringBootWebProject</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--xxxxxxxxxxxxxxxxxxxxxxxx 把tomcat改為->jetty xxxxxxxxxxxxxxxxxxxxxxxx-->
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-web</artifactId> -->
<!-- <exclusions> -->
<!-- <exclusion> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-tomcat</artifactId> -->
<!-- </exclusion> -->
<!-- </exclusions> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--xxxxxxxxxxxxxxxxxxxxxxxx 把tomcat改為->jetty xxxxxxxxxxxxxxxxxxxxxxxx-->
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-jetty</artifactId> -->
<!-- <scope>provided</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
```

#### JspController.java

```clike=
package tw.leonchen.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class JspController {
@GetMapping("/jsppagetest.controller")
public String processJspAction() {
return "index";
}
}
```
#### sql LeonPower
```clike=
create table Bird(
bid int not null primary key identity(100,1),
bname nvarchar(50)not null,
size nvarchar(1)not null,
color nvarchar(20)not null,
age int not null
);
```
#### Bird.java
```clike=
package tw.leonchen.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
@Entity
@Table(name = "bird") //不要大寫 會出錯
@Component
public class Bird {
@Id @Column(name="BID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int bid;
@Column(name="BNAME")
private String bname;
@Column(name="SIZE")
private String size;
@Column(name="COLOR")
private String color;
@Column(name="AGE")
private int age;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
#### BirdRepository.java
```clike=
package tw.leonchen.model;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BirdRepository extends JpaRepository<Bird, Integer> {
}
```
#### BirdService.java
```clike=
package tw.leonchen.model;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BirdService {
@Autowired
private BirdRepository birdRepository;
public Bird insert(Bird bird){
return birdRepository.save(bird);
}
public Bird update(Bird bird) {
return birdRepository.save(bird);
}
public void deleteById(Integer id) {
birdRepository.deleteById(id);
}
public Bird finById(Integer id) {
Optional<Bird> birdOptional = birdRepository.findById(id);
return birdOptional.get();
}
}
```
#### BirdController.java
```clike=
package tw.leonchen.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tw.leonchen.model.Bird;
import tw.leonchen.model.BirdService;
@RestController
public class BirdController {
@Autowired
private BirdService birdService;
@GetMapping("/birdinsert.controller")
public Bird processBirdInsertAction() {
Bird bird1 = new Bird();
bird1.setBname("eagle");
bird1.setSize("L");
bird1.setColor("brown");
bird1.setAge(7);
return birdService.insert(bird1);
}
@GetMapping("/birdinsertgen2.controller")
public Bird processBirdInsertAction2(@RequestBody Bird bird) {
return birdService.insert(bird);
}
@GetMapping("/birddelete.controller")
public String processBirdDelete(@RequestParam(name = "id")Integer id) {
birdService.deleteById(id);
return "ok";
}
@GetMapping("/birdquerybyid.controller")
public Bird processBirdQueryById(@RequestParam(name = "id")Integer id) {
return birdService.finById(id);
}
@GetMapping("/birdupdate.controller")
public Bird processBirdUpdate(Bird bird) {
Bird bird2 = new Bird();
bird2.setBid(100);
bird2.setBname("bluebird");
bird2.setSize("s");
bird2.setColor("blue");
bird2.setAge(2);
return birdService.update(bird);
}
}
```
# MySQL
[MySQL下載](https://dev.mysql.com/downloads/windows/installer/8.0.html)

* java jre





* 3306不用動

* 用預設

P@ssw0rd




P@ssw0rd


#### 命令提示自元

如果沒選到可以用以下的方法


P@ssw0rd

### Mysql資料庫


#### 建立table
```clike=
use leonpower;
create table Bird(
bid int not null primary key auto_increment,
bname varchar(50) not null,
size varchar(1) not null,
color nvarchar(20) not null,
age int not null
) auto_increment = 100 ;
Insert into Bird(bname, size, color, age) Values('eagle','L','brown',6);
select * from bird;
drop table bird;
```

### 使用者創建
* 2選一



### 自己取連線


