三、JPA增删改查常用方法
前言:创建EntityManager对象,需要先创建创建EntityManagerFactory对象
方式一:直接通过persistenceUnitName创建
String persistenceUnitName = "Jpa-1";
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
方式二:通过persistenceUnitName和properties一起创建
String persistenceUnitName = "Jpa-1";
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.show_sql", false);
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
2.创建一个测试增删改查的JPA项目
package com.jtfr.jpa.test; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence; import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class JpaTest { private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
private EntityTransaction transaction; @Before
public void create(){
entityManagerFactory = Persistence.createEntityManagerFactory("Jpa-1");
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
} @Test
public void testFind(){
System.out.println("没问题");
} @After
public void end(){
transaction.commit();
entityManager.close();
entityManagerFactory.close();
}
}
3.常用增删改查方法解释
查询方法
find方法:方法调用时,已经发出了sql进行查询
@Test
public void testFind(){
CustomerJpa customerJpa = entityManager.find(CustomerJpa.class, 100);
System.out.println("--------分割线作用:区分确实是find的时候就发出了sql语句---------");
System.out.println(customerJpa.toString());
}
getReference方法:方法调用时,返回的是代理对象,当代理对象使用时候才真正发送sql查询数据库,需要注意“懒加载”问题
@Test // 类似Hibernate的load方法
public void testGetReference(){
CustomerJpa customerJpa = entityManager.getReference(CustomerJpa.class, 100);
System.out.println("返回的是代理对象"+customerJpa.getClass().getName());
System.out.println("--------getReference方法调用的时候没有发sql-------------");
System.out.println(customerJpa.toString());
}
persist方法:方法调用为保存一条数据
// 类似 Hibernate 的 save 方法
@Test
public void testPersist(){
CustomerJpa customerJpa = new CustomerJpa();
customerJpa.setId(300100);
customerJpa.setLasName("夕阳游子");
customerJpa.setEmail("23228858@qq.com");
customerJpa.setAge(15);
customerJpa.setCreateTime(new Date());
customerJpa.setBirth(new Date());
entityManager.persist(customerJpa); // 注意不能设置 Id
}
三、JPA增删改查常用方法的更多相关文章
- spring boot(十五)spring boot+thymeleaf+jpa增删改查示例
快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...
- JavaJDBC【三、增删改查】
获取数据库连接后,可进行增删改查操作 语句生成: Statement s = con.createStatement(sql); //生成语句 PreparedStatement ps = (Prep ...
- Maybatis的一些总结(三:增删改查)
回顾一个点 之前不懂这句: UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 现在理解了一点点,相当于实现了userMap ...
- java连接mysql数据库 三 实现增删改查操作
同以前一样,先写一个数据库打开和关闭操作类 public class DBConnection { String driver = "com.mysql.jdbc.Driver"; ...
- MyBatis数据持久化(三)增删改查
上篇文章中我们使用mybatis成功建立数据库会话,并从表中查询出相应的数据,本文在此基础上介绍MyBatis另外几种操作,即插入.修改.删除记录. 1.修改User.xml文件,增加几条sql语句: ...
- SpringBoot JPA + H2增删改查示例
下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...
- 【讲义提纲】以一个实战新闻cms增删改查demo为例,给学院国创队伍培训php
PHP实战基础——以一个新闻cms的增删改查为例 一. 环境配置 二. 数据库创建 三. 增删改查demo 连接数据库 <?php $link=mysq ...
- springMVC之增删改查
一.核心原理 1. 用于发送请求给server: /home.htm 2. 请求被DispatchServlet拦截到 3. DispatchServlet通过HandleMapping检查url有没 ...
- spring boot2+jpa+thymeleaf增删改查例子
参考这遍文章做了一个例子,稍微不同之处,原文是spring boot.mysql,这里改成了spring boot 2.Oracle. 一.pom.xml引入相关模块web.jpa.thymeleaf ...
随机推荐
- TLSv网络安全标准,会话加密协议展望未来
本文是关于TLSv1.3采用的三部分系列的第三部分也是最后一部分.它解决了网络加密和监控的选项,包括备用会话加密协议. 通过TLSv1.3的批准,并在IETF出版物队列中,是时候考虑部署选项和障碍,并 ...
- Thymeleaf 基本用法总结
Thymeleaf 基本用法总结 一.引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 在html中引入此命名空间,可 ...
- js中的回钓函数,C#中的委托
$(function(){ myfunction(sayHi); }); var sayHi=function(){ alter('你好'); } function myfunction(a){ a( ...
- 看图了解RocksDB
它是一个高性能的Key-Value数据库.设计了完善的持久化机制,同时保证性能和安全性.能够良好的支持范围查询,因为K-V记录就是按照Key来排序的. 下图为写入的流程: 可以看到主要的三个组成部 ...
- spring-boot的helloWorld详解
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.Maven Plugin管理 pom.xml配置代码: <project xml ...
- HDU 6050 Funny Function —— 2017 Multi-University Training 2
Funny Function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 原生js深拷贝函数
function deepClone(data){ if(!data || !(data instanceof Object) || (typeof data=="function" ...
- 二分查找法:x 的平方根
实现 int sqrt(int x) 函数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. public int mySqrt(int x) { long left=0; long r ...
- 用Linux 搭建 PXE 网络引导环境
本例子中使用了CentOS7.4 minimal 系统,并且关闭了防火墙和selinux,并使用了dhcp.tftp.http和samba服务. 假设PXE服务器是192.168.4.104 ,tft ...
- Linux操作系统(四)_部署MySQL
一.部署过程 1.当前服务器的内核版本和发行版本 cat /etc/issue uname -a 2.检查系统有没有自带mysql,并卸载自带版本 yum list installed | grep ...