SpringBoot的数据库操作
来源:互联网 发布:网页编程前景 编辑:程序博客网 时间:2024/06/04 00:52
作为一个忘得总比记得多的人!应该干嘛,做个笔记啊。方便以后看。
由衷的感叹一句!真方便!
1.添加依赖,一个是我们数据库也要用到的data-jpa(简化程序与数据库之间的交互),一个数据库驱动。(文件名:pom.xml),基本的配置在建好项目的时候就有了,只需把这个添加。我只是针对这个范围,其他项目也要找自己加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency>
2.我们的配置文件(项目名:application.yml)
spring: profiles: active: dev datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/dbgirl username: root password: root jpa: hibernate: ddl-auto: update show-sql: true
3接口GirlRepository(继承一个接口)
import org.springframework.data.jpa.repository.JpaRepository;public interface GirlRepository extends JpaRepository<Girl,Integer> {}
4类(运行,可以直接创建数据库girl)
import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Girl { @Id @GeneratedValue private Integer id; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } private String cupSize; private Integer age; /** * 记得要有一个构造函数 */ public Girl(){ }}
5类GirlController(核心代码聚集地)
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;@RestControllerpublic class GirlController { @Autowired public GirlRepository girlRepository; /** * 查询所有女生列表 * @return */ @GetMapping(value = "/girls") public List<Girl> girlList(){ return girlRepository.findAll(); } /** * 添加一个女生 */ @PostMapping(value = "/girls") public Girl addGirl(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){ Girl girl = new Girl(); girl.setCupSize(cupSize); girl.setAge(age); return girlRepository.save(girl); } /** * 查询一个女生 * @param id * @return */ @GetMapping(value = "/girls/{id}") public Girl GirlFindONe(@PathVariable("id") Integer id){ return girlRepository.findOne(id); } /** * 更新一个女生 * @param id * @param cupSize * @param age * @return */ @PutMapping(value = "/girls/{id}") public Girl girlUpdate(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){ Girl girl = new Girl(); girl.setId(id); girl.setCupSize(cupSize); girl.setAge(age); return girlRepository.save(girl); } /** * 删除一个女生 * @param id */ @DeleteMapping(value = "/girls/{id}") public void girlDelete(@PathVariable("id") Integer id){ girlRepository.delete(id); }}
6看我的项目框架吧,具体的文件在哪里。
问题:
给大家推荐一个好的应用,postman。Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件。(在商店里应用部分才搜的到)我的测试都是在这里运行的(增删改查)。然后在数据库中查看结果。具体的操作,大家搜搜哈。(我标出来的大家看看)
给大家看一下大概的内容。累,不更了。我把代码,配置全发了,就是没讲解。搜索啊,一般所搜到这,我感觉基本的知识大家也掌握了。就试试代码。看那么多代码。增删改查都有了,是不是很方便。
阅读全文
0 0
- SpringBoot的数据库操作
- SpringBoot 数据库的基本操作
- SpringBoot数据库操作出现的
- springboot JPA操作数据库
- SpringBoot操作MySQL数据库
- springboot数据库操作
- SpringBoot 数据库操作(上)
- SpringBoot 数据库操作(下)
- SpringBoot 数据库操作、事务
- SpringBoot-04SpringBoot使用JdbcTemplate操作数据库
- SpringBoot JPA标准规范操作数据库
- SpringBoot整合JPA操作数据库
- SpringBoot 使用JPA操作数据库
- springboot学习笔记(二):基于MySql数据库的JDBC操作
- springboot学习笔记(三):基于MySql数据库的JPA操作
- SpringBoot中Controller以及Jpa操作数据库的使用
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- 多线程进阶002 之 Executor框架
- 自己开发的分布式erp系统的界面设计
- 迪拜机器人警察上岗 精通6种语言
- jdk1.5新特性
- React
- SpringBoot的数据库操作
- C. Vladik and Memorable Trip dp 选择区间
- Java泛型中的命名规则(标识符含义)
- mac下配置jdk,tomcat,maven,mysql环境变量
- 新的开始
- float对内联元素和块元素的影响
- 编程第五十三天
- MSSQL:触发器
- 资源分享 | 摄影视频 | 杨最醉摄影视频(1-7)