sts中Mysql的連接和一些簡單的操作


                                      1:Mysql數據庫的連接

                                      1.1 在sts中連接Mysql數據庫是在application中寫jdbc的連接

           

spring:
  profiles:
    active:
      - dev
  datasource :
   driver-class-name: com.mysql.jdbc.Driver
   url: jdbc:mysql://localhost:3306/student
   username: root
   password: 123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

                                                                  1.2對數據庫進行增刪查改

 

1.2.1:現在定義用戶

package com.cy.coo.li;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Man {
  
    @Id
    @GeneratedValue
    private Integer id;
    private Integer age;
    private String cupSize;
    
    public Man(){
        
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }
    
}

1.2.2:運用了一個JpaRepository<>的接口來實現對命名的規范

package com.cy.coo.li;



import java.util.List;

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

public interface manInterface extends JpaRepository<Man, Integer> {
   //通過年齡查詢,因為查出來很可能有很多個,所以使用集合
     public List<Man> findByAge(Integer age);
}

1.2.3:數據庫的增刪查改

package com.cy.coo.li;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class ManContent {

    @Autowired
    private manInterface manl;
    
    @Autowired
    private manService mans;

    /*
     * 查詢所有人的數據
     */
    @GetMapping(value = "/man")
    /*
     * List指的是集合.<>是泛型,里面指定了這個集合中存放的是什么數據. List<Man>代表把Man類中的信息的對象都在里面了
     */
    public List<Man> manlist() {
        return manl.findAll();
    }

    /*
     * 添加
     */
    @PostMapping(value = "/man")
    // @RequestParam獲取參數
    /*
     * 因為save返回的時添加進去的對象,所以返回類型就是這個對象
     */
    public Man manAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) {
        Man man = new Man();

        man.setCupSize(cupSize);
        man.setAge(age);
        return manl.save(man);
    }

    // 查詢
    @GetMapping(value = "/man/{id}")
    //因為
    public Man manFindOne(@PathVariable("id") Integer id) {
        System.out.println(id);
        return manl.findOne(id);

    }
       //更新
    @PutMapping(value="/man{id}")
    public Man manUpdata(@RequestParam(value = "id") Integer id,
    @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){
        Man manll=new Man();
        manll.setId(id);
        manll.setAge(age);
        manll.setCupSize(cupSize);
        return manl.save(manll);
    }
    //刪除
    @DeleteMapping(value="/man{id}")
    public void manDelete(@RequestParam("id") Integer id){
        //因為delete的返回值為null所以就是沒得1返回值
        manl.delete(id);
    }
    //通過年齡查詢
     @GetMapping(value="/man/age/{age}")
     public List<Man> manListAge(@PathVariable("age") Integer age){
         return manl.findByAge(age);
     }
     
     @PostMapping(value="/man/two")
     public void manTwo(){
         mans.InsertTwo();
     }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM