• 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();
         }
        
    }
  • 相关阅读:
    页面转表格并且下载
    关于表格导入
    C/C++作用域运算符::
    设计模式之观察者模式_C++
    C# 传不定参数
    C语言中的位域的使用
    C++/python求哈希值(SHA256)
    C++中纯虚函数
    类模板/函数模板实现父类指针指向子类对象(虚函数实现多态)
    Windows Socket 接口简介
  • 原文地址:https://www.cnblogs.com/lihaiyang123/p/7534424.html
Copyright © 2020-2023  润新知