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项目 ...
随机推荐
- jdk1.8 HashMap 实现 数组+链表/红黑树
转载至 http://www.cnblogs.com/leesf456/p/5242233.html 一.前言 在分析jdk1.8后的HashMap源码时,发现网上好多分析都是基于之前的jdk,而Ja ...
- MySql必知必会实战练习(六)游标
游标主要用于交互式应用,滚动屏幕上的数据,并对数据进行浏览或做出更改 看一下下面的例子: drop procedure IF EXISTS processorders; create procedur ...
- SQL Server: Top 10 Secrets of a SQL Server Expert
转载自:http://technet.microsoft.com/en-us/magazine/gg299551.aspx Many companies have downsized their IT ...
- 洛谷 P3015 [USACO11FEB]最好的括号Best Parenthesis
传送门 题目大意:给出括号的得分标准. ()得分为1,如果A的得分为S(A),那么 (A)的得分为2*S(A). 题解:搜索 #include<iostream> #include< ...
- linux centos6.5 安装gcc-c++时出现 yum install gcc-c++ cannot find a valid baseurl for repo...
1.输入命令:cd /etc/sysconfig/network-scripts/ 2.ls查看该文件夹下 3.vi ifcfg-eth0 按i进行编辑,添加如下两行后,esc →shift+:→wq ...
- 一分钟理解js闭包
什么是闭包?先看一段代码: ? 1 2 3 4 5 6 7 8 9 10 function a(){ var n = 0; function inc() { n++; cons ...
- maven依赖顺序原则
使用maven的程序员都会遇到一个问题,那就是maven依赖冲突的问题,这会导致ClassNotFound或者MethodNotFound这样的异常.其实只要明白maven依赖的根本性的原则就不怕这样 ...
- 洛谷 1600 (NOIp2016) 天天爱跑步——树上差分
题目:https://www.luogu.org/problemnew/show/P1600 看TJ:https://blog.csdn.net/clove_unique/article/detail ...
- 转JMeter 利用Jmeter批量数据库插入数据
1. 启动Jmeter 2. 添加 DBC Connection Configuration 右键线程组->添加->配置元件->JDBC Connection Configu ...
- oscache使用经历
oscache作为一款老的本地缓存,应用场景主要有页面缓存和对象缓存.这里拿在maven项目中使用oscache作为对象缓存举例说明下用法: 1.导入jar包 <dependency> & ...