# SpringBoot 3/25 上課內容 ###### tags: `SpringBoot` [老師雲端](https://onedrive.live.com/?authkey=%21AOSC5VqK5wFJeuk&id=AF483E7E8FEC387B%218460&cid=AF483E7E8FEC387B) # 31 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/7alAsRZ.png) * 要注意:正式上線不會有這段 ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/Wvl9aHo.png) * static * templates存放:樣板程式 網頁程式 ![](https://i.imgur.com/hkTOt61.png) # 32 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/rov5DXw.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/ouoovnv.png) # 33 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/lXFjRUT.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/BP7LNrg.png) # 34 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/NeC0cOc.png) ![](https://i.imgur.com/WcK4Cft.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/FWI2f1L.png) ![](https://i.imgur.com/TiOuAC3.png) # 35 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/OFtYJxK.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/ZRBLkRG.png) # 36 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/rwuLtTM.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/YcFeaz1.png) # 37 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/QUbNjz3.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/X2O6YoG.png) # 38 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/c1Cn8Fz.png) * produces = 回傳格式 ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/1y6aNC7.png) # 39 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/FeQYrxb.png) # 40 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/7a9lbTZ.png) [starters](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter) ![](https://i.imgur.com/kwIj7dH.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/kJydT2M.png) # 41 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/dcErbVs.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/KRYp0zg.png) # 42 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/jN41c2J.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/9CKfljB.png) # 43 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/DB5Y0nu.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/kTRgWKn.png) # 44 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/WicoiCN.png) ![](https://i.imgur.com/DkbSuNs.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/ZyE29li.png) # 45 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/7yKUPQ1.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/KpIBCMc.png) # 46 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/vBMuKJR.png) # 47 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/afKzyuX.png) [ORM介紹及ORM優點、缺點](http://blog.twbryce.com/what-is-orm/) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/16bLbwT.png) # 48 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/qp9XktW.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/oL408FK.png) # 49 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/sHaUOlm.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/mNcozfX.png) # 50 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/XtbXDtz.png) [Repository](https://docs.spring.io/spring-data/commons/docs/2.4.6/api/org/springframework/data/repository/Repository.html) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/d71A7eQ.png) # 51 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/hXLyN0w.png) * save:會自動更新或新增 * existsById:自動判斷存在再更新 ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/oszKUhq.png) # 52 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/6MFp1fH.png) * deletelnBatch:可分批一次刪除 ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/Yow5CyZ.png) # 53 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/ZBgL7eR.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/YaWWyB3.png) # 54 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/gkR5AsO.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/rJXs6YI.png) # 55 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/YD4KoZ0.png) ## Spring Boot 動態網站開發實務 ![](https://i.imgur.com/MdrYgSv.png) # 56 Spring Boot 動態網站開發實務 ![](https://i.imgur.com/BEtL6Rl.png) # <font color="green">------------實作部份------------</font> ![](https://i.imgur.com/qplnEDJ.png) * 在css建會跑到webapp在把他拉上去 ![](https://i.imgur.com/wDaffr0.png) ![](https://i.imgur.com/5OR2Eew.png) ![](https://i.imgur.com/MEXTVrZ.png) #### 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 ![](https://i.imgur.com/BLbwpxO.png) ![](https://i.imgur.com/6oOWhxl.png) ```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 ![](https://i.imgur.com/LqcinvK.png) ```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 ![](https://i.imgur.com/T35gu2k.png) ```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 ![](https://i.imgur.com/NNfeS7F.jpg) ![](https://i.imgur.com/BGxi2LE.png) ![](https://i.imgur.com/fo0OACi.png) ```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> ``` ![](https://i.imgur.com/gGEXtGb.png) #### JspController.java ![](https://i.imgur.com/qFZNZER.png) ```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) ![](https://i.imgur.com/0Bnoqo0.png) * java jre ![](https://i.imgur.com/vKgnBW6.png) ![](https://i.imgur.com/sZzJlCP.png) ![](https://i.imgur.com/lgDsylw.png) ![](https://i.imgur.com/nS1eK8z.png) ![](https://i.imgur.com/ZEH4lRM.png) * 3306不用動 ![](https://i.imgur.com/3smfJGd.png) * 用預設 ![](https://i.imgur.com/imB9NIE.png) P@ssw0rd ![](https://i.imgur.com/E7S2fr2.png) ![](https://i.imgur.com/fHkfkhr.png) ![](https://i.imgur.com/xzspg2r.png) ![](https://i.imgur.com/1cr8RAM.png) P@ssw0rd ![](https://i.imgur.com/DNybvpD.png) ![](https://i.imgur.com/AodAy1C.png) #### 命令提示自元 ![](https://i.imgur.com/dE65nlN.png) 如果沒選到可以用以下的方法 ![](https://i.imgur.com/iznQl8Q.png) ![](https://i.imgur.com/WqnkUO2.png) P@ssw0rd ![](https://i.imgur.com/sBPPNQU.png) ### Mysql資料庫 ![](https://i.imgur.com/BxcHtTZ.png) ![](https://i.imgur.com/kpdIIed.png) #### 建立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; ``` ![](https://i.imgur.com/1ZLgxtu.png) ### 使用者創建 * 2選一 ![](https://i.imgur.com/tpsfkDU.png) ![](https://i.imgur.com/zfjKJ7v.png) ![](https://i.imgur.com/Hwo172F.png) ### 自己取連線 ![](https://i.imgur.com/f6Uf80a.png) ![](https://i.imgur.com/dV9llF6.png) ![](https://i.imgur.com/t13JlZB.png)