SpringBoot 数据库操作 增删改查
2018-06-18 00:34:36来源:未知 阅读 ()
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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:常用类(上)
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 学习Java 8 Stream Api (4) - Stream 终端操作之 collect 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
- nacos~配置中心功能~springboot的支持 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash