步骤

1.在pom.xml添加mysql,spring-data-jpa依赖
2.在application.properties文件中配置mysql连接配置文件
3.在application.properties文件中配置JPA配置信息
4.编写测试例子

内容

1.在pom.xml添加mysql,spring-data-jpa依赖

1
2
3
4
5
6
7
8
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2.在application.properties文件中配置mysql连接配置文件

1
2
3
4
5
6
7
8
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

3.在application.properties文件中配置JPA配置信息

1
2
3
4
5
6
7
8
9
//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
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

4.编写测试例子
(1)创建实体类Demo,如果已经存在,可以忽略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 创建了一个实体类。
*
* 如何持久化呢?
*
* 1、使用@Entity进行实体类的持久化操作,当JPA检测到我们的实体类当中有
*
* @Entity 注解的时候,会在数据库中生成对应的表结构信息。
*
*
* 如何指定主键以及主键的生成策略?
*
* 2、使用@Id指定主键.
*/
@Entity
public class Cat {
/**
* 使用@Id指定主键.
*
* 使用代码@GeneratedV 大专栏  SpringBoot(七)-SpringBoot JPA-Hibernatealue(strategy=GenerationType.AUTO)
* 指定主键的生成策略,mysql默认的是自增长。
*
*/
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;//主键.
private String catName;//姓名. cat_name
private int catAge;//年龄. cat_age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
public int getCatAge() {
return catAge;
}
public void setCatAge(int catAge) {
this.catAge = catAge;
}
}

(2)创建jpa repository类操作持久化(CrudRepository)

1
2
public interface CatRepository extends CrudRepository<Cat, Integer>{
}

(3)创建service类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Service
public class CatService {
@Resource
private CatRepository catRepository;
/**
* save,update ,delete 方法需要绑定事务.
*
* 使用@Transactional进行事务的绑定.
*
* @param cat
*/
//保存数据.
@Transactional
public void save(Cat cat){
catRepository.save(cat);
}
//删除数据》
@Transactional
public void delete(int id){
catRepository.delete(id);
}
//查询数据.
public Iterable<Cat> getAll(){
return catRepository.findAll();
}
}

(4)创建restful请求类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@RestController
@RequestMapping("/cat")
public class CatController {
@Resource
private CatService catService;
@RequestMapping("/save")
public String save(){
Cat cat = new Cat();
cat.setCatName("jack");
cat.setCatAge(3);
catService.save(cat);
return "save ok.";
}
@RequestMapping("/delete")
public String delete(){
catService.delete(1);
return "delete ok";
}
@RequestMapping("/getAll")
public Iterable<Cat> getAll(){
return catService.getAll();
}
}

(5)测试

在前端输入save.do,delete.do,getAll时候后台数据库执行正确
参考源码

SpringBoot(七)-SpringBoot JPA-Hibernate的更多相关文章

  1. SpringBoot之使用jpa/hibernate

    Springboot版本是2.1.3.RELEASE 1.依赖 List-1.1 <dependency> <groupId>org.springframework.boot& ...

  2. SpringBoot(七) SpringBoot中的缓存机制

    随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...

  3. springboot(七).springboot整合jedis实现redis缓存

    我们在使用springboot搭建微服务的时候,在很多时候还是需要redis的高速缓存来缓存一些数据,存储一些高频率访问的数据,如果直接使用redis的话又比较麻烦,在这里,我们使用jedis来实现r ...

  4. SpringBoot(七):SpringBoot中如何使用过滤器(Filter)?

    方式一: 通过注解方式实现: 1.编写一个Servlet3的注解过滤器(和上一章Servlet相似) 贴代码: package com.example.springbootweb.filter; im ...

  5. SpringBoot + Jpa(Hibernate) 架构基本配置

    1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 一.maven的pom ...

  6. springboot 集成 jpa/hibernate

    pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  7. javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful

    一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...

  8. [读书笔记] 四、SpringBoot中使用JPA 进行快速CRUD操作

    通过Spring提供的JPA Hibernate实现,进行快速CRUD操作的一个栗子~. 视图用到了SpringBoot推荐的thymeleaf来解析,数据库使用的Mysql,代码详细我会贴在下面文章 ...

  9. 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

随机推荐

  1. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:读取MNIST手写图片数据写入的TFRecord文件

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

  2. 使用XAMPP集成开发环境安装Apache、PHP的配置说明

    一.安装XAMPP 双击安装包xamppinstaller.exe,可完成XAMPP的安装,与其他软件安装并无区别. 二.配置Apache端口,使用其可以正确启动 点击Start,启动Apache时可 ...

  3. tensorflow(七)

    一.模型托管工具 TensorFlow Serving TensorFlow Serving支持生产级的服务部署,允许用户快速搭建从模型训练到服务发布的工作流水线. 工作流水线主要由三部分构成 (1) ...

  4. SVN一直清理解决

    svn作为我们经常使用的版本管理服务器,在使用过程中经常需要通过clean up操作来完成本地文件与服务器文件信息及版本信息同步,然而有时会在执行清理命令时提示“清理失败,请执行清理”,并且提示的中文 ...

  5. vue项目打包部署elementUI的字体图标丢失问题

    自己搭建的Vue项目,没有使用vue-cli,引入elementUI时,使用的是webpack直接打包工具,发现字体图标丢失你 记录一下解决办法: webpack module配置:(build目录下 ...

  6. element ui 自定义异步验证

    之前提到过,axios是一个异步请求,但是很多时候我们都需要同步请求,比如在element的表单验证中需要验证一个用户名是否存在的时候,异步请求好像就不太好用了.前边博客中提到过,这种情况可以用es6 ...

  7. python-django框架-电商项目-用户模块开发_20191117

    实现注册的基本逻辑: 1,注册页面 注意:注册页面需要静态文件的支持,另外注册页面是基础基类的, 1,url,路由系统, 2,views,视图系统,还是使用类视图,里面有很多的函数, 2,views. ...

  8. python三目运算和递归的小练习

    应用前: ''' 递归的简单实现,输出i = 5的时候的结果 ''' def diGui(i = 0): i = i +1 if i >=5: return ("{0}大于等于5&qu ...

  9. android适配全机型悬浮框、视频APP项目、手势操作、Kotlin妹子App、相机图片处理等源码

    Android精选源码 图片滤镜处理,相机滤镜实时处理,相机拍照录制 android仿爱壁纸App更换壁纸效果源码 基于Kotlin+MVP+Retrofit+RxJava+Glide 等架构实现短视 ...

  10. php测试使用小的mysql存储过程

    <?php //前提是php.ini里面要开通mysqli的扩展./*$link = mysqli_connect('localhost','root','','chinatupai');  $ ...