1 JPA入门----项目搭建以及CRUD
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jap-xx</artifactId>
<groupId>com.cmos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>jpa-01</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>4.3.8.Final</hibernate.version>
<junit.version>4.12</junit.version>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--hibernate-core-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- hibernate-entitymanager-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--javax.persistence-->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="com.cmos.jpa" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql:///jpa"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="gosaint"/>
<!--mysql方言-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<!--显示sql-->
<property name="hibernate.show_sql" value="true"/>
<!--自动建表-->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<!--格式化sql-->
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
package com.cmos.jpa; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
private String password;
...
省略getter()和setter()
}
4 新建测试类EmployeeTest
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence; import com.cmos.jpa.Employee; import org.junit.Test; public class EmployeeTest { @Test
public void save() throws Exception {
Employee employee=new Employee();
employee.setName("张三");
employee.setPassword("123456");
String persistenceUnitName="com.cmos.jpa";
// 1 获取实例管理工厂
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
// 2 获取实例管理对象
EntityManager entityManager = entityManagerFactory.createEntityManager();
// JAP默认事务不开启
EntityTransaction transaction = entityManager.getTransaction();
//3 开启事务
transaction.begin();
//4 持久化操作
entityManager.persist(employee);
//5 提交事务
transaction.commit();
//6 关闭资源
entityManager.close();
entityManagerFactory.close(); }
}
public class JPAUtils {
private static EntityManagerFactory entityManagerFactory;
static {
try {
String persistenceUnitName="com.cmos.jpa";
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
}catch (Exception e){
throw new RuntimeException("配置文件出错"+e.getMessage());
}
}
public static EntityManager getEntity(){
return entityManagerFactory.createEntityManager();
}
public static void close(){
if(entityManagerFactory!=null){
entityManagerFactory.close();
}
}
}
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence; import com.cmos.jpa.Employee;
import com.cmos.jpa.JPAUtils; import org.junit.Before;
import org.junit.Test; /**
* 查询一个: find(T.class,Long id)
* 修改: merge(T)
* 保存:persistence(T)
* 删除:remove(T) 直接删除对象
*
*/
public class EmployeeTest2 {
/**
* 保存
*/
@Before
public void save(){
Employee employee=new Employee();
employee.setName("张三");
employee.setPassword("123456");
Employee employee2=new Employee();
employee2.setName("里斯");
employee2.setPassword("123456");
EntityManager entity = JPAUtils.getEntity();
EntityTransaction transaction = entity.getTransaction();//获取事务
transaction.begin();
entity.persist(employee);
transaction.commit();
}
@Test
public void queryAndUpdate(){
EntityManager entity = JPAUtils.getEntity();
entity.getTransaction().begin();
//查询
Employee employee = entity.find(Employee.class, 1L);
System.out.println(employee);
employee.setName("xyz");
employee.setPassword("99999");
//修改
entity.merge(employee);
System.out.println(employee);
entity.getTransaction().commit();
} @Test
public void delete(){
EntityManager entity = JPAUtils.getEntity();
entity.getTransaction().begin();
Employee employee = entity.find(Employee.class, 1L);
entity.remove(employee);
System.out.println(employee);
entity.getTransaction().commit();
} //JPQL查询
@Test
public void queryJPQL(){
EntityManager entity = JPAUtils.getEntity();
entity.getTransaction().begin();
String jpql="select o from Employee o";
Query query = entity.createQuery(jpql);
List<Employee> resultList = query.getResultList();
for (Employee e:resultList) {
System.out.println("0000000000");
System.out.println(e);
}
entity.getTransaction().commit(); } }
1 JPA入门----项目搭建以及CRUD的更多相关文章
- 004-Spring boot 快速入门-项目搭建与启动、SpringBootApplication、启动图标
一.官方地址 Spring:http://spring.io/ Spring Project:http://spring.io/projects Spring boot:https://project ...
- 002-Spring4 快速入门-项目搭建、基于注解的开发bean,Bean创建和装配、基于注解的开发bean,Bean初始化销毁、Bean装配,注解、Bean依赖注入
一.项目搭建 1.项目创建 eclipse→project explorer→new→Project→Maven Project 默认配置即可创建项目 2.spring配置 <dependenc ...
- 【Unity/Kinect】Kinect入门——项目搭建
本文是Unity Store里的官方Demo包中的ReadMe翻译(别人翻的),介绍了用Unity如何入门搭建起一个Kinect项目工程. 非常感谢下面这位大大的无私奉献! http://www.ma ...
- eslint 入门项目搭建过程
github 地址 : https://github.com/gebin/eslint-demo 运行该项目 npm install npm start 访问 http://localhost:900 ...
- Vue -cli 入门 --项目搭建(一)
一. 安装node.js环境. 在node.js官网下载稳定版本(https://nodejs.org/en/) 下载完成后点击安装,安装过程很简单,一直next即可,安装完成会自动添加node及np ...
- 架构师入门:搭建双注册中心的高可用Eureka架构(基于项目实战)
本文的案例是基于 架构师入门:搭建基本的Eureka架构(从项目里抽取) 改写的. 在上文里,我们演示Eureka客户端调用服务的整个流程,在这部分里我们将在架构上有所改进.大家可以想象下,在上文里案 ...
- Vue-cli入门(一)——项目搭建
Vue-cli入门(一)——项目搭建 前言: Vue-cli是一款基于vue的项目脚手架工具,其集成了webpack环境和主要的依赖,对于我们的项目搭建.开发.打包.维护管理等都是非常的方便. 主要内 ...
- 入门项目数字手写体识别:使用Keras完成CNN模型搭建(重要)
摘要: 本文是通过Keras实现深度学习入门项目——数字手写体识别,整个流程介绍比较详细,适合初学者上手实践. 对于图像分类任务而言,卷积神经网络(CNN)是目前最优的网络结构,没有之一.在面部识别. ...
- SpringCloud学习之手把手教你用IDEA搭建入门项目(三)
本篇博客是承接上一篇<手把手教你用IDEA搭建SpringCloud入门项目(二)>,不清楚的请到我的博客空间查看后再看本篇博客,上面两篇博客成功创建了一个简单的SpringCloud项目 ...
随机推荐
- C++友元类实现
C++中的友元既可以实现友元函数,也可以实现友元类,也就是说一个类也可以作为另外一个类的友元.当作为一个类的友元时,它的所有成员函数都是另一个类的友元函数,都可以访问另一个类的私有或者公有成员. 请看 ...
- dojo chart详解
Dojo提供了一套很完善的统计图(Chart)接口,在dojox/charting下面,可以支持很多种类型的. .简介 Dojo统计图提供快速的.简单的接口实现美观的.交互性强的web统计图表的实现. ...
- docker中宿主机与容器(container)互相拷贝传递文件的方法
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/71425077 本文出自[我是干勾鱼的博客] 前面讲解过如何进入.退出docker ...
- cmd 操作WinService
1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:\Windows\Microsoft.NET ...
- 行为驱动开发BDD概要
BDD脱胎于TDD 行为驱动开发(Behavior-Driven Development,简称BDD),是在测试驱动开发(Test-Driven Development,TDD)基础上发展而来的一种软 ...
- test20181219(期末考试)
Written with StackEdit. \(noip\)爆炸后就好久没考试了...结果今天又被抓去,感觉很慌啊... 考完了.过来填坑. T1 Description 使得\(x^x\)达到或 ...
- iOS当前屏幕截屏
需求描述: 有两个ViewController 我们记做 A.B ,其中B controller只是显示下半部分: 如下图效果: 实现这种的方案很多,可以用添加View方法, 也可以用UIWindo ...
- 阿里云接口异常-Can not find endpoint to access
最近在做公司的资产盘点,需要请求阿里云的接口获取公司的云服务器信息.在获取实例列表的过程中,通过异常机制捕获了 Can not find endpoint to access 这个错误.经过多次排查, ...
- 在idea下两个项目之间的maven父子级项目依赖
配置:idea 在一个项目中的父子级依赖网上轮子太多,我就不重复造了,留个链接 http://www.cnblogs.com/tibit/p/6185704.html 说说一些我自己注意的问题,在pr ...
- Java 引用类型变量的声明和使用
引用类型变量的声明和使用 (1)把类名当作是一种类型来声明变量,这种变量叫引用类型变量.如:People people; (2)引用类型变量保存对象的“引用”,即对象的地址. (3)对象的创建 new ...