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(); //删除所有数据