SpringBoot中Controller以及Jpa操作数据库的使用
来源:互联网 发布:北京日结美工招聘信息 编辑:程序博客网 时间:2024/05/29 18:33
常用注解
- @PathVariable:获取Url中的数据
- @RequestParam: 获取请求参数的值
- @GetMapping : 组合注解,相当于@RequestMapping(method = RequestMethod.GET)
- @PostMapping: 组合注解,相当于@RequestMapping(method = RequestMethod.POST)
@RestController:组合注解,相当于@Controller 加 @ResponseBody
## Jpa ##
Spring-Data-Jpa定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate,TopLink.
pom依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--web项目必须引入的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--单元测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--Jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--mysql附件--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--spring官方模板,跳转指定页面--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
数据库配置:
增删改查代码:
实体类 :
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<Girl> findByAge(Integer age);}
Controller层(逻辑实际是写在servicec层,这里为了简化):
/** * Created by hasee * on 2017/4/15. */@RestControllerpublic class GirlController { @Autowired private GirlPreposity girlPreposity; /** *查询所有列表 */ @GetMapping(value = "/girls") public List<Girl> 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<Girl> 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); }}
0 0
- SpringBoot中Controller以及Jpa操作数据库的使用
- SpringBoot 使用JPA操作数据库
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- springboot JPA操作数据库
- SpringBoot JPA标准规范操作数据库
- SpringBoot整合JPA操作数据库
- 三、springboot项目的简单使用之:JPA使用操作数据库
- SpringBoot intercept拦截器中获取JPA数据库操作实例
- springBoot Controller的使用
- springboot-controller的使用
- springboot学习笔记(三):基于MySql数据库的JPA操作
- Springboot 之 JPA数据库操作进阶篇
- springBoot+Jpa(hibernate)数据库基本操作
- Springboot 之 使用JPA进行分页操作
- springboot中使用spring -data-jpa (一)
- springboot中使用spring -data-jpa (二)
- SpringBoot的数据库操作
- 看门狗电路
- 排序算法---桶排序
- 前端实现大小图交互
- 人人都是产品经理
- 沃罗诺伊图(Voronoi Diagram,也称作Dirichlet tessellation,狄利克雷镶嵌 )归纳
- SpringBoot中Controller以及Jpa操作数据库的使用
- 升级10.12,想要允许任何来源应用
- ubuntu安装jdk和eclipse
- SetUid的权限问题
- 2017.4.15.B组模拟赛总结
- jzoj 5060. 【GDOI2017第二轮模拟day1】公路建设 线段树+最小生成树
- 【数据结构】中缀表达式转换后缀表达式(逆波兰式)
- 对【2017.4.15B组NOIP提高模拟赛】总结
- 腾讯十天Vue.js课程之一:Vue.js介绍