springboot使用jpa案例
1 创建entity实体类并生成数据库表
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer stuid;
private String stuname;
public Integer getStuid() {
return stuid;
}
public void setStuid(Integer stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
}
2 创建Dao接口
@Repository
public interface StudentDao extends JpaRepository<Student,Integer> {
}
3 创建service层接口
public interface StudentService {
//查询所有学生信息
public Iterable<Student> getStudent();
//添加学生信息
public Student addStudent(Student student);
//修改学生信息
public Student updaStudent(Student student);
//删除学生信息
public void delStuddent(Integer stuid);
}
4 创建service层接口实现类
@Service("studentService")
public class StudentServiceImpl implements StudentService{
@Resource
private StudentDao studentDao;
//查询所有学生信息
@Override
public Iterable<Student> getStudent() {
return studentDao.findAll();
}
//添加学生信息
@Override
public Student addStudent(Student student) {
return studentDao.save(student);
}
//修改学生信息
@Override
public Student updaStudent(Student student) {
return studentDao.save(student);
}
//删除学生信息
@Override
public void delStuddent(Integer stuid) {
studentDao.delete(stuid);
}
}
5 编写application.yml文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///springbootjpa
username: root
password: 123
jpa:
hibernate:
ddl-auto: update
6 编写Controller层
@RestController
@RequestMapping("/onejpa")
public class JpaController {
@Resource
private StudentService studentService;
//查询数据
@RequestMapping("/getStudent")
public Iterable<Student> getStudent(){
return studentService.getStudent();
}
//添加数据
@RequestMapping("/addStudent")
public Student addStudent(){
Student student=new Student();
student.setStuname("张三");
return studentService.addStudent(student);
}
//修改数据
@RequestMapping("/updaStudent")
public Student updaStudent(){
Student student=new Student();
student.setStuid(1);
student.setStuname("李四");
return studentService.updaStudent(student);
}
//删除数据
@RequestMapping("/delStudent")
public void delStudent(){
studentService.delStuddent(1);
}
}
7 启动程序
@SpringBootApplication
public class StartSpringBoot {
public static void main(String[] args) {
SpringApplication.run(StartSpringBoot.class,args);
}
}
@Entity@Table(name="student")public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer stuid; private String stuname;
public Integer getStuid() { return stuid;}
public void setStuid(Integer stuid) { this.stuid = stuid;}
public String getStuname() { return stuname;}
public void setStuname(String stuname) { this.stuname = stuname;}}
springboot使用jpa案例的更多相关文章
- Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务
1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...
- 【极简版】SpringBoot+SpringData JPA 管理系统
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在上一篇中已经讲解了如何从零搭建一个SpringBo ...
- 带你搭一个SpringBoot+SpringData JPA的环境
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家对SpringBoot和Spring Da ...
- 二、springboot使用jpa
花了几天时间,好好看了看springboot的jpa部分,总结了常用的形式. 1.通过STS工具添加jpa的依赖项 要连mysql,测试的时候需要web,顺便添加了lombok不写set和get方法了 ...
- Springboot+MyBatis+JPA集成
1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Sp ...
- 第11章—使用对象关系映射持久化数据—SpringBoot+SpringData+Jpa进行查询修改数据库
SpringBoot+SpringData+Jpa进行查询修改数据库 JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分.但它又不限于EJB 3.0,你可以在Web应用.甚至桌面应用 ...
- 集成Springboot+MyBatis+JPA
1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...
- SpringBoot Data JPA 关联表查询的方法
SpringBoot Data JPA实现 一对多.多对一关联表查询 开发环境 IDEA 2017.1 Java1.8 SpringBoot 2.0 MySQL 5.X 功能需求 通过关联关系查询商店 ...
- 用SpringBoot+MySql+JPA实现对数据库的增删改查和分页
使用SpringBoot+Mysql+JPA实现对数据库的增删改查和分页 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述 ...
随机推荐
- php中让数组顺序随机化,打乱顺序等
php中有很多排序的函数,sort,rsort,ksort,krsort,asort,arsort,natcasesort,这些函数用来对数组的键或值进行这样,或那样的排序. 可以终究有时候还需要一些 ...
- Django框架之DRF APIView Serializer
一.APIView 我们在使用DjangoRestfulFramework的时候会将每个视图类继承APIView,取代原生Django的View类 APIView的流程分析: rest_framewo ...
- C程序编译和执行
编译 & 执行 C 程序 首先准备一个源码文件 hello.c 键入如下代码: #include <stdio.h> int main() { /* 我的第一个 C 程序 */ p ...
- git设置代理模式,仅为github设置代理
设置代理: 全局代理 git config --global http.proxy 127.0.0.1:1087 局部代理,在github clone 仓库内执行 git config --local ...
- Entity Framework Codefirst的配置步骤
Entity Framework Codefirst的配置步骤: (1) 安装命令: install-package entityframework (2) 创建实体类,注意virtual关键字在导航 ...
- python class 中__next__用法
class A(): def __init__(self,b): self.b=b # def __iter__(self): # 这个函数可以用,表示迭代标志,但也可以省略 # return sel ...
- 2019-07-24 Smarty模板引擎的简单应用
smarty是什么? Smarty是一个使用PHP写出来的模板引擎,是业界最著名的PHP模板引擎之一.Smarty分离了逻辑代码和外在的内容,提供一种易于管理和使用的方法,用来将原本与HTML代码混杂 ...
- TCP连接与断开
两台宿主机 一台:作为服务器 另一台:作为客户端 服务器的步骤: 打开[控制面板]----点击[程序]---点击程序和功能中[打开或关闭Windows功能]---点击[Telnet服务器]--点击[确 ...
- 【转载】C#中List集合使用GetRange方法获取指定索引范围内的所有值
在C#的List集合中有时候需要获取指定索引位置范围的元素对象来组成一个新的List集合,此时就可使用到List集合的扩展方法GetRange方法,GetRange方法专门用于获取List集合指定范围 ...
- 数组的push()、pop()、shift()和unshift()方法
JavaScript的数组是一个拥有堆栈和队列自身优点的global对象.也就是说JavaScript数组可以表现的像栈(LIFO)和队列(FIFO)一样操作.这也是JavaScript数组强大的可操 ...