spring boot学习(四)---数据库操作

来源:互联网 发布:淘宝好评返现投诉敲诈 编辑:程序博客网 时间:2024/05/16 14:44

四 数据库操作

1.spring data-jpa

spring 对hibernate的一个封装

2.restful接口

restful接口,实现如下功能

方式 路径 功能 get /stu 获取列表 post /stu 创建一个学生 get /stu/id 通过id获取一个学生 put /stu/id 通过id 更新一个学生 delete /stu/id 通过id删除一个id

3.添加jar包

        <!--spring data-jpa-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-jpa</artifactId>        </dependency>        <!--mysql驱动-->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>

在application.yml 添加数据库和hibernate配置

spring:  profiles:    active: dev  datasource:  driver-class-name: com.mysql.jdbc.Driver  url: jdbc:mysql://127.0.0.1:3306/myssh?useUnicode=yes&amp;characterEncoding=UTF-8  username: root  password: aqiang1003  jpa:    hibernate:    ddl-auto: create    show-sql: true

ddl-auto create 表示每次自动创建表,会把之前的表删除
update 不存在会创建表 ,不会删除表
create-drop 应用停止时删除
none 默认什么都不做
valdate 验证类里的属性和表结构是否一致,不一致会报错

写一个stu类,通过注解自动生成hibernate

package com.boot.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entity  //表示这个类就是一个表public class Stu {    @Id    @GeneratedValue //自增    private Integer id;    private String name;    private String age;    //无参的必须    public Stu() {    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }}

在dao中编写StuRepository接口

package com.boot.dao;import org.springframework.data.jpa.repository.JpaRepository;import com.boot.entity.Stu;public interface StuRepository extends JpaRepository<Stu, Integer> {}在Controller中编写StuControllerpackage com.boot.web;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.boot.dao.StuRepository;import com.boot.entity.Stu;@RestController@RequestMapping("/stu")public class StuController {    @Autowired    private StuRepository stuRepository;    //由于操作比较简单直接在controller使用dao层接口    @RequestMapping(method=RequestMethod.GET)    public List<Stu> stuList(){        return stuRepository.findAll();    }}

在数据库的stu表中填入数据
这里写图片描述
可以使用火狐浏览器的httprequest插件模拟get请求

http://localhost:8080/stu

如下

http://localhost:8080/stu

在StuController 类中新增方法

/**     * 新增     *      */    @RequestMapping(method=RequestMethod.POST)    public String getAdd(@RequestParam("name") String name,@RequestParam("age") String age){        Stu stu = new Stu();        stu.setAge(age);        stu.setName(name);        stuRepository.save(stu);        return stu.getId().toString();    }

结果如下
这里写图片描述

剩余3个方法`/**
* 根据id查一个
*
*/
@RequestMapping(value=”/{id}”,method=RequestMethod.GET)
public Stu getById(@PathVariable(“id”) Integer id){
return stuRepository.findOne(id);

}/** * 根据id改一个 * */@RequestMapping(value="/{id}",method=RequestMethod.PUT)public void updateById(@PathVariable("id") Integer id,@RequestParam("name") String name,@RequestParam("age") String age){    Stu stu = stuRepository.findOne(id);    stu.setAge(age);    stu.setName(name);     stuRepository.save(stu);}/** * 根据id删一个 * */@RequestMapping(value="/{id}",method=RequestMethod.DELETE)public void delById(@PathVariable("id") Integer id){     stuRepository.delete(id);}

get

http://localhost:8080/stu/1 

这里写图片描述

put

http://localhost:8080/stu/2

这里写图片描述

delete

http://localhost:8080/stu/1

这里写图片描述

最后数据库结果
这里写图片描述

拓展查询

在StuRepository添加

public List<Stu> findByAge(String age);

在StuController中添加

@RequestMapping(value="/age/{age}",method=RequestMethod.GET)    public List<Stu> delById(@PathVariable("age") String age){         return stuRepository.findByAge(age);    }

这里写图片描述

原创粉丝点击