【idea】Springboot整合jpa
第一步快速搭建springboot项目:在你建立的工程下创建 Module 选择Spring initializr创建。
第二步:修改包名、项目名、web项目打成war包、在Type处选择: Maven Project(项目的构建工具)
第三步:选择你项目需要的基本依赖
第四步:结束
springboot项目的结构:
注意点:
1、.mvn文件、mvnw、mvnw.cmd可以删掉
2、程序启动类必须在所有接口类的上一层,才能被扫描到
配置数据库连接文件:(两种文件形式的:properties、yml)默认扫描:application开头的文件
具体类容:yml文件形式
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx?useSSL=true&verifyServerCertificate=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8
username: xxx
password: xxx
创建实体类:
@Entity --需要导入jpa依赖包
@Table(name="employee") ---指向数据库的表名
public class Employee {
@Id --表中的主键、自增长形式
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer empNo;
private String empName;
private String empSex;
private Integer empAge;
private Double sal;
private Date history;
public Employee() {
} public Employee(Integer empNo, String empName, String empSex, Integer empAge, Double sal, Date history) {
this.empNo = empNo;
this.empName = empName;
this.empSex = empSex;
this.empAge = empAge;
this.sal = sal;
this.history = history;
} public Integer getEmpNo() {
return empNo;
} public void setEmpNo(Integer empNo) {
this.empNo = empNo;
} public String getEmpName() {
return empName;
} public void setEmpName(String empName) {
this.empName = empName;
} public String getEmpSex() {
return empSex;
} public void setEmpSex(String empSex) {
this.empSex = empSex;
} public Integer getEmpAge() {
return empAge;
} public void setEmpAge(Integer empAge) {
this.empAge = empAge;
} public Double getSal() {
return sal;
} public void setSal(Double sal) {
this.sal = sal;
} public Date getHistory() {
return history;
} public void setHistory(Date history) {
this.history = history;
} @Override
public String toString() {
return "Employee{" +
"empNo=" + empNo +
", empName='" + empName + '\'' +
", empSex='" + empSex + '\'' +
", empAge=" + empAge +
", sal=" + sal +
", history=" + history +
'}';
}
}
数据层:直接继承JpaRepositoryAPI类
public interface EmployeeDaoImpl extends JpaRepository<Employee,Integer> { } 注意此处省略service层、是为了测试小demo
控制层:
@RestController ---rest分格
@RequestMapping("/jpa") ----请求模块分层
public class JpaController {
@Autowired ----注入
private EmployeeDaoImpl employeeDaoImpl;
@RequestMapping(value = "/select",method = RequestMethod.GET)
public List<Employee> selectUser(){//查
return employeeDaoImpl.findAll();
}
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String addUser(Employee emp){//增
Employee employee=new Employee();
employee.setEmpName(emp.getEmpName());
employee.setEmpAge(emp.getEmpAge());
employee.setEmpSex(emp.getEmpSex());
employee.setHistory(new Date());
employee.setSal(emp.getSal());
Employee employee1=employeeDaoImpl.save(employee);
return employee1.toString();
}
@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
public void deleteUser(@RequestParam(value = "id")Integer id){//删
employeeDaoImpl.deleteById(id);
}
@RequestMapping(value="/{id}" ,method=RequestMethod.GET)
public Optional<Employee> getAccountById(@PathVariable("id") int id){
return employeeDaoImpl.findById(id);
}
@RequestMapping(value = "/update",method = RequestMethod.PUT)
public Employee updateUser(Employee emp){//修
return employeeDaoImpl.save(emp);
} }
postman测试:CRUD
---不足之处,请多指教
【idea】Springboot整合jpa的更多相关文章
- SpringBoot数据访问(二) SpringBoot整合JPA
JPA简介 Spring Data JPA是Spring Data大家族的一部分,它可以轻松实现基于JPA的存储库.该模块用于增强支持基于JPA的数据访问层,它使我们可以更加容易地构建使用数据访问技术 ...
- springboot整合JPA(简单整理,待续---)
整合步骤 引入依赖: <dependencies> <dependency> <groupId>org.springframework.boot</group ...
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot4-jpaJ/ 本文出自方志朋的博客 JPA全称J ...
- SpringBoot 整合 Jpa
项目目录结构 pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- springboot整合JPA创建数据库表失败
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table t_s ...
- springboot整合jpa和mybatis实现主从复制
百度多方参考终于配出我自己的了,以下仅供参考 参考https://www.cnblogs.com/cjsblog/p/9712457.html 代码 首先数据源配置 spring.datasource ...
- SpringBoot整合JPA遇到的问题
在学习SpringBoot中使用Repository时出现这种错误 或者使用findOne也会报错,只需要改为 应该是SpringBoot版本的原因,fingOne()方法好像已经不用了.
- springBoot整合jpa
https://blog.csdn.net/qq_35180973/article/details/82316438 总体来讲只是在service调用dao的时候用接口代替dao继承CrudRepos ...
- SpringBoot整合系列-整合JPA
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959865.html SpringBoot整合JPA进行数据库开发 步骤 第一步:添加必 ...
随机推荐
- iOS学习——核心动画之Layer基础
iOS学习——核心动画之Layer基础 1.CALayer是什么? CALayer我们又称它叫做层.在每个UIView内部都有一个layer这样一个属性,UIView之所以能够显示,就是因为它里面有这 ...
- CkEditor批量上传图片(java)
CKEditor上传视频CKEditor批量上传图片flvplayer.swf播放器CKEditor整合包(v4.6.1) ------------------------------------ 最 ...
- Elasticsearch Search API
当执行一个搜索时,它将这个搜索请求广播给所有的索引分片.可以通过提供路由参数来控制要搜索哪些分片.例如,当检索tweets这个索引时,路由参数可以设置为用户名: curl -X POST " ...
- 深入理解Spring AOP思想
什么是AOP?AOP解决了什么问题? 在传统的开发模式中,以下层次的是非常常见的一种,业务层每一个方法都要有重复的事务代码 如何改善这个问题? AOP希望将A.B 这些分散在各个业务逻辑中的相同代码, ...
- Educational Codeforces Round 48 (Rated for Div. 2)——A. Death Note ##
A. Death Note time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Flow 常用知识点整理
Flow入门初识 Flow是facebook出品的JavaScript静态类型检查工具. 由于JavaScript是动态类型语言,它的灵活性也会造成一些代码隐患,使用Flow可以在编译期尽早发现由类型 ...
- FIVE1
Topic Link http://ctf5.shiyanbar.com/stega/FIVE1/1111110000000000.jpg 1) 直接放到HXD中,你会发现里面有一个zip文件 2)提 ...
- leetcode — scramble-string
import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/scramble-string/ * * Given ...
- [二十]JavaIO之StringReader 与 StringWriter
功能简介 还记得前面说过的CharArrayReader 和 CharArrayWriter吗? CharArray 是数据源 CharArrayReader 是读, 从一个CharArray 中读 ...
- Oracle游标的使用示例
此文是使用Oracle游标的几种方式,for...in会自动打开游标,fetch...into需要手动打开游标,游标类似于一个只会往前移动的指针,每次指向数据集中的一行数据,通过游标可以打开数据集,也 ...