SpringBoot中Controller以及Jpa操作数据库的使用

常用注解

  1. @PathVariable:获取Url中的数据
  2. @RequestParam: 获取请求参数的值
  3. @GetMapping : 组合注解,相当于@RequestMapping(method = RequestMethod.GET)
  4. @PostMapping: 组合注解,相当于@RequestMapping(method = RequestMethod.POST)
  5. @RestController:组合注解,相当于@Controller 加 @ResponseBody

    SpringBoot中Controller以及Jpa操作数据库的使用_第1张图片


## Jpa ##

  • Spring-Data-Jpa定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate,TopLink.

    pom依赖:

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.2.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>

    build>

数据库配置:

SpringBoot中Controller以及Jpa操作数据库的使用_第2张图片

增删改查代码:
实体类 :

package com.cym;

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

/**
 * Created by hasee
 * on 2017/4/15.
 */
@Entity    //表示当前类代表数据库中的表
public class Girl {
    @Id
    @GeneratedValue   //自增
    private Integer id;

    private Integer age;

    public Girl() {

    }
    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;
    }
}

接口:

package com.cym;

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

import java.util.List;

/**
 * Created by hasee
 * on 2017/4/15.
 */
public interface GirlPreposity extends JpaRepository<Girl,Integer>{

    //通过年龄age查询
   public List findByAge(Integer age);
}

Controller层(逻辑实际是写在servicec层,这里为了简化):

/**
 * Created by hasee
 * on 2017/4/15.
 */

@RestController
public class GirlController {
    @Autowired
    private GirlPreposity girlPreposity;
    /**
     *查询所有列表
     */
    @GetMapping(value = "/girls")
    public List findlist(){
        return girlPreposity.findAll();
    }
    /**
     *id查询一条数据
     */
    @GetMapping(value = "/select")
    public Girl findid(@RequestParam(value = "id",defaultValue = "1") Integer id){

        return girlPreposity.findOne(id);
    }
    /**
     *age 查询
     */
    @GetMapping(value = "/girls/age/{age}")
    public List findlist1(@PathVariable("age") Integer age){
        return girlPreposity.findByAge(age);
    }
    /**
     *添加一条数据
     */
    @PostMapping (value= "/girls")
    public Girl add(@RequestParam(value = "age") Integer age){
        Girl girl = new Girl();
        girl.setAge(age);
        return girlPreposity.save(girl);
    }
    /**
     *更新
     */
    @PutMapping(value= "/girls/{id}")
    public Girl update(@PathVariable("id") Integer id,
                       @RequestParam(value = "age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setAge(age);
        System.out.println(id+age);
        return girlPreposity.save(girl);
    }
    /**
     *更新
     */
    @DeleteMapping(value= "/girls/{id}")
    public void delet(@PathVariable("id") Integer id){

        girlPreposity.delete(id);
    }
}

你可能感兴趣的:(SpringBoot)