SpringBoot 数据库操作 增删改查

2018-06-18 00:34:36来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

1、pom添加依赖

<!--数据库相关配置-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.11</version>
        </dependency>

2、application.properties文件中添加数据库配置

spring.datasource.url = jdbc:mysql://localhost:3306/dss  
spring.datasource.username = root  
spring.datasource.password = 123  
spring.datasource.driverClassName = com.mysql.jdbc.Driver  
# Specify the DBMS
spring.jpa.database = MYSQL  
# Show or not log for each sql query
spring.jpa.show-sql = true  
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update  
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy  
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect  

3、创建Entity实体类

package com.haha.demo;

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


import javax.persistence.*;


@Entity
@Table(name = "link")
public class Link {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private String link;
    private String img;
    private Integer code;
    private String shijian;


    public Link(){
    }

    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 getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }


    public String getShijian() {
        return shijian;
    }

    public void setShijian(String shijian) {
        this.shijian = shijian;
    }
}

4、创建接口类

package com.haha.demo;

import java.util.List;


import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;


public interface LinkRepository extends CrudRepository<Link, Integer>{
     List<Link> findByName(String name);
     @Query("SELECT m FROM Link m where m.name like %?1% order by id desc ")
     List<Link> search(String name);
}

5、创建Controller

package com.haha.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;




@RequestMapping("/link")
@RestController
public class LinkController {

    @Autowired
    private LinkRepository linkRepository;


    /**
     * 查询列表
     * @return
     */
    @PostMapping(value = "/list")
    @ResponseBody
    public List<Link>  getlinklList(String name) {

        return (List<Link>) linkRepository.findAll();
 } }

 6、数据库的添加,修改操作

 Link link= new Link();
 link.setLink("aaa");
 link.setName("aaa");
 link.setImg("aaa");
 link.setShijian("1505090911");
 link.setCode(9);
 link.setId(10);    //修改操作添加此句,添加操作略去此句

linkRepository.save(link);

 

7、数据库的删除操作

linkRepository.deleteById(7); //删除id为7的选项
linkUrlRepository.deleteAll();  //删除所有数据

  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:SpringMVC 与 REST.

下一篇:常用类(上)