---
# System prepended metadata

title: Java Spring boot& DB new

---

# Java Spring boot& DB new  

###### Day1: 
    
* learn Sprnig boot for beginners
<!-- [1. Video coures from YT](https://www.youtube.com/watch?v=HTuE0GZtnM4) -->
* Setup SpringBoot ( JAVA ) ---IntelliJ IDEA
    1. New Project select Spring Assistant.
    ![](https://i.imgur.com/ZuXgmNT.png)
    2. Select Web click Spring Web.
    3. Select Template Engines click Thymeleaf
    4. Select Developer Tools click Spring BootDevTools

## Strat bulid Spring boot using Java code --mavnen
#### 1. Create Application
    
    @SpringBootApplication
    public class DemoApplication {
	   public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	   }
    }
___
#### 2. Create index.html to templates file
    skip...
    
    <body>
        <h3>Hello Spring boot</h3>
        <a href="/hello">Click here</a>
    </body>
    </html>
#### 3. Create hello.html to templates file
    skip....
    
    <body>
        <h3>Hello Spring boot</h3>
    </body>
#### 4. Create(a Spring MVC controller with Thymeleaf template) WebController ( because didn't crate this .java the index page can't find other .html)
    skip....
    
    @Controller
    public class WebController {
        @RequestMapping("/hello")
        public String sayHello(){
            System.out.println("Say hello world spring boot");
            return "hello";
        }
    }
___
    change to
    
    WebController.java
       
       @Controller
    public class WebController {
        @RequestMapping("/hello")
        public String sayHello(Model model){
            System.out.println("Say hello world spring boot");
            model.addAttribute("message","Greetings how");
            return "hello";
        }
    }  
---
    hello.html
    
    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Hello Page</title>
    </head>
    <body>
        <h3>Hello Spring boot from Intellij IDEA</h3>
        <h2 th:text="${message}"></h2>
    </body>
    </html>
#### 5. look test flie try JUnit test 
    @SpringBootTest
    class DemoApplicationTests {

	@Autowired
	private WebController controller;


	@Test
	void contextLoads() {
		Assertions.assertThat(controller).isNotNull();
	}

}

#### 6. Create(REST controller) WebServicesController using @RestController

    @RestController
        public class WebServicesController {

        @GetMapping("/rest")
        public String sayREST(){
            return "Great, Let's take a REST with Spring boot";
        }
    }
    
    and add to hello.html
        <a href="/rest">Take a REST</a>


#### 7. Package & Run application 
1.Open Maven on right side
2.Tap mvn package 
#### 8. Create 






   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
## More information
* MVC
    1. Model
    2. View
    3. Controller


# Issues
##### After start Appaction will started and stop.
    
    Can open pom.xml file
    Add command line:
    <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
	</dependency>


## Databases 

* Data type:
    1. CHAR / VARCHAR / TEXT
    2. INTEGER / DECIMAL(10,2)
    3. DATE / DATETIME
    4. BLOB =  Binary Large Object
    
* DML (Data Manipulation Language)資料操作語言
    1. insert
    2. delete
    3. update

* DQL (Data Query Language) 資料查詢語言
    1. select

* Simple to use
    1. Create database 'NAME';
    2. Show databases; 
    3. Create table;
    4. Show tables;
    5. Drop database 'NAME';



* Create table
    
###### tags: need to use ` 在 1 旁邊
    
    create table `awesomdb`.`heroes` (
     `id` int not null auto_increment,
     `name` varchar(100) not null,
     `gener` char(1) null,
     `age` int null,
     `hero_level` char(1) not null,
     `hero_rank` int null,
     `description` text null,
    PRIMARY KEY(`id`));
    
* 新增欄位
    (在 descriotion 後面新增)
    
        alter table `awesomdb`.`heroes`
        -> add column `abc` varchar(45) null after `description`;
        
* 移除欄位
        
        alter table `awesomdb`.`heroes`
        drop column `abc`;
        
* 寫入資料
        
        
        insert into awesomedb.heroes
        (name,age,hero_level,hero_rank, description)
        values("KK", 18,"S",1,"hi");
        
* 查詢資料
        
        select * from heroes; (會把全部資料撈出來)
        
        select * (把指定的 資料撈出來)
        from heroes
        where hero_level = 's' and gender = 'F';
        
        select name, gender(部分欄位)
        from heroes
        where hero_level = 's' and gender = 'F';
        
        (查詢沒有填寫的)
        select *(部分欄位)
        from heroes
        where age is null;
        
        (查詢有指定文字的關鍵字)
        select *(部分欄位)
        from heroes
        where name like '%查詢的字%';
        (or)
        where name like '查詢的字%';在最前面
        where name like '%查詢的字';在最後面
        
        (查詢年紀 10~25)
        where age >= 10 and age <= 25
        (or)
        where age between 10 and 25
        
        (查詢 hero level 's' 跟 'a' 級的)
        select name,age,hero_level from heroes
        where hero_level = 'S' or hero_level = 'A';
        (or)
        where hero_level in ('S' ,'A');
        
        
        (查詢 不是 's' 的人)  <> 是不等於
        select name,age,hero_level from heroes
        where hero_level <> 'S' and hero_level <> 'A';
        or
        where hero_level not in ('S','A')
        

* 更新資料

         UPDATE `awesome_db`.`heroes` SET `age` = '11' WHERE (`id` = '5');
