SpringBoot整合SpringDataJPA

1、JPA概念

JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.

JPA的总体思想和现有Hibernate、TopLink、JDO等ORM框架大体一致。总的来说,JPA包括以下3方面的技术:

ORM映射元数据

JPA支持XML和JDK5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;

API

用来操作实体对象,执行CRUD操作,框架在后台替代我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。

查询语言

这是持久化操作中很重要的一个方面,通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合

2.springdataJPA的快速入门

创建maven工程并导入依赖:(这里用的是sprigcloud2.1.16的)

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.16.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
<dependency>
<groupId>org.sprin
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

2、配置文件application.yml

server:
port: 9090
spring:
application:
name: spring-data-jpa
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///user_db?serverTimezone=UTC
username: root
password: root
#jpa配置
jpa:
show-sql: true
hibernate:
ddl-auto: update

注意: url这里的配置 必须要加入serverTimezone=UTC传递时区,不然操作数据库会抛异常的,///代表的是127.0.0.1:3306 本地的可以省略

ServerTimeZone时区的问题

在设定时区的时候,如果设定serverTimezone=UTC,会比中国时间早8个小时,如果在中国,可以选择Asia/Shanghai或者Asia/Hongkong,例如:

url:jdbc:mysql://localhost:3306/mango?serverTimezone=Asia/Shanghai&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8

解析:

ddl-auto表示对数据库进行自动化配置

值有三个选择:

create表示创建,如果说数据库中不管有没有这张表,都会先干掉,然后在重新创建,不适合生产环境

update表示更新,如果说数据库中有这张表,则执行进行更新表中的数据,如果没有这张表,则直接创建这张表,适合生产环境

none 没有任何操作

3、实体类

@Data
@Entity//表示当前类是实体类
@Table(name="tb_user",catalog = "user_db")
public class UserInfo {
/**
* 如果数据库表中 的字段名称和实体类中的属性名称保持一致的话,可以不需要加@Column注解
* @GeneratedValue(strategy=GenerationType.IDENTITY)主键生成策略
* GenerationType.IDENTITY表示针对于mysql中有自增长的数据的生成策略
* GenerationType.SEQUENCE表示针对于oracle数据中的主键生成策略
* GenerationType.AUTO是默认的选项,会根据数据库自动选择

@table 中的name值是指定数据库中的表名   catalog表示的是指定database名

*/
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

private String name;
private String gender;
private Integer age;
private String address;
private String qq;
private String email;
private String username;
private String password;
private String phone;
}

4.dao接口

/**
* dao接口
*/
@Repository
public interface UserMapper extends JpaRepository<UserInfo,Integer> {
}

jpaRepository的泛型 为 userinfo 为实体类  integer为实体类的id也就是数据库对应的主键

5、测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaTest {

@Autowired
private UserMapper userMapper;

//查询方法
@Test
public void queryAll(){
List<UserInfo> infoList = userMapper.findAll();
System.out.println(infoList);
}
}

在实体类上要加入lombok的注解:

@AllArgsConstructor #有参构造的注解
@NoArgsConstructor #无参构造的注解

6、基于@Query注解查询与更新

欢迎大家一起交流学习...

SpringBoot整合SpringDataJPA,今天没啥事情就看了一下springboot整合springdataJPA,实在是香啊,SQL语句都不用写了的更多相关文章

  1. SpringBoot thymeleaf模板页面没提示,SpringBoot thymeleaf模板插件安装

    SpringBoot thymeleaf模板插件安装 SpringBoot thymeleaf模板Html页面没提示 SpringBoot  thymeleaf模板页面没提示 SpringBoot t ...

  2. springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用

    百度百科: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBat ...

  3. SpringBoot内置的各种Starter是怎样构建的?--SpringBoot源码(六)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 温故而知新 本篇接 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五) 温 ...

  4. Springboot中mybatis控制台打印sql语句

    Springboot中mybatis控制台打印sql语句 https://www.jianshu.com/p/3cfe5f6e9174 https://www.jianshu.com/go-wild? ...

  5. Spring3+MyBatis3整合log4j无法输出SQL语句问题的解决

    今天遇到了跟下面文章一模一样的问题,下面文章的解决方案很好,在这里记录保存一下. Spring3+MyBatis3整合无法输出SQL语句问题的解决

  6. 在springboot 和 mybatis 项目中想要显示sql 语句进行调试

    在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增: logging.level.com.dy.springboot.server.mapper= ...

  7. sql语句去重 最后部分没看 看1 有用

    一 数据库 1.常问数据库查询.修改(SQL查询包含筛选查询.聚合查询和链接查询和优化问题,手写SQL语句,例如四个球队比赛,用SQL显示所有比赛组合:举例2:选择重复项,然后去掉重复项:) 数据库里 ...

  8. 新手也能看懂的 SpringBoot 异步编程指南

    本文已经收录自 springboot-guide : https://github.com/Snailclimb/springboot-guide (Spring Boot 核心知识点整理. 基于 S ...

  9. 9、springcloud整合logback打印sql语句

    Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core.logback- classic和logback-access.logback-c ...

随机推荐

  1. node-sass安装失败解决方法

    node-sass安装失败,提示如下: gyp verb check python checking for Python executable "python" in the P ...

  2. 【Pod Terminating原因追踪系列之一】containerd中被漏掉的runc错误信息

    前一段时间发现有一些containerd集群出现了Pod卡在Terminating的问题,经过一系列的排查发现是containerd对底层异常处理的问题.最后虽然通过一个短小的PR修复了这个bug,但 ...

  3. Python 中的数字到底是什么?

    花下猫语:在 Python 中,不同类型的数字可以直接做算术运算,并不需要作显式的类型转换.但是,它的"隐式类型转换"可能跟其它语言不同,因为 Python 中的数字是一种特殊的对 ...

  4. MD5截断比较验证

    最近在打De1CTF时在Web项目中碰到了两次MD5截断比较验证的题目,在做题时为了方便顺手写了一个小脚本来爆破Code,下面就简单分享一下思路 以De1CTF线上赛Web3为例,在De1CTF中,我 ...

  5. C++——大小写转换

    代码如下: #include <iostream> using namespace std; int main() { char ch; cin>>ch; if(ch>' ...

  6. 从架构到部署,全面了解K3s

    Kubernetes无处不在--开发者的笔记本.树莓派.云.数据中心.混合云甚至多云上都有Kubernetes.它已然成为现代基础设施的基础,抽象了底层的计算.存储和网络服务.Kubernetes隐藏 ...

  7. linux下部署python项目到jenkins

    环境:linux+jenkins+tomcat+git+python3.7 1.安装jdk 上传安装包到usr/local 解压 配置环境变量 vim /etc/profile export JAVA ...

  8. 在express中使用ES7装饰器构建路由

    在Java的Spring框架中,我们经常会看到类似于@Controller这样的注解,这类代码能够极大的提高我们代码的可读性和复用性.而在Javascript的ES7提案中,有一种新的语法叫做deco ...

  9. 如何编写一个简单的Linux驱动(二)——完善设备驱动

    前期知识 1.如何编写一个简单的Linux驱动(一)——驱动的基本框架 2.如何编写一个简单的Linux驱动(二)——设备操作集file_operations 前言 在上一篇文章中,我们编写设备驱动遇 ...

  10. ssm框架spring-mvc.xml和spring-mybatis.xml报错项目无法正常启动问题

    报错信息 Multiple annotations found at this line: - Referenced file contains errors (http://www.springfr ...