1.新建Jpa项目

2.引入所需的jar 包

3.创建实体类

package com.watchfree.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
/**
* 创建实体类
* @author watchfree
* @date 2016年12月6日下午7:12:43
*/
@Entity
public class Employee { @Id private int id ;
private String name ;
private long salary ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public Employee() { } public Employee(int id){
this.id = id ;
} @Override
public String toString() {
// TODO Auto-generated method stub
return "employee: id: "+this.id + " ,name: "+this.name + " ,saray: "+this.salary;
} }

4.配置JPA Content 下的 persistence.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="EmployeeService"
transaction-type="RESOURCE_LOCAL">
<class>com.watchfree.entity.Employee</class>
<properties>
<!-- 数据库连接操作 -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" /> <!-- 配置 JPA 实现产品的基本属性. 配置 hibernate 的基本属性 -->
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

 5.创建操作Employee实体的服务类

package com.watchfree.service;

import javax.persistence.EntityManager;

import com.watchfree.entity.Employee;

/**
* 操作Employee实体的服务类
*
* @author watchfree
* @date 2016年12月6日下午6:47:42
*/
public class EmployeeService {
protected EntityManager entityManager; public EmployeeService(EntityManager entityManager) {
this.entityManager = entityManager;
} /**
* 创建Employee方法
*
* @param id
* @param name
* @param salary
* @return
*/
public Employee createEmployee(int id, String name, long salary) {
Employee employee = new Employee(id);
employee.setName(name);
employee.setSalary(salary);
entityManager.persist(employee);
return employee;
} /**
* 删除
*
* @param id
*/
public void removeEmployee(int id) {
Employee employee = findEmployee(id);
if (employee != null) {
entityManager.remove(employee);
}
} /**
* 查找
*
* @param id
* @return
*/
public Employee findEmployee(int id) {
return entityManager.find(Employee.class, id);
}
/**
* 更新
* @param id
* @param raise
* @return
*/ public Employee raiseEmployeeSalary(int id, long raise) {
Employee employee = entityManager.find(Employee.class, id);
if (employee != null) {
employee.setSalary(employee.getSalary() + raise);
}
return employee;
}

/**
    * 查詢所有
    * @return
    */
      public List<Employee> findAllEmployees() {
         TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e",Employee.class);
        return query.getResultList();
       }

}

6.创建测试类

package com.watchfree.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; import com.watchfree.entity.Employee;
import com.watchfree.service.EmployeeService; public class EmpoyeeTest { private EntityManagerFactory entityManagerFactory ;
private EntityManager entityManager ;
private EntityTransaction transaction ;
private EmployeeService employeeService ; @Before
public void init() {
entityManagerFactory = Persistence.createEntityManagerFactory("EmployeeService") ;
entityManager = entityManagerFactory.createEntityManager() ;
transaction = entityManager.getTransaction() ;
employeeService = new EmployeeService(entityManager) ;
} @After
public void destory(){
entityManager.close();
entityManagerFactory.close();
} }

6.1测试添加操作

  /**
* 创建测试
*/
@Test
public void createTest() {
transaction.begin();
Employee employee = employeeService.createEmployee(158, "watchfree", 35000);
transaction.commit();
System.out.println(employee.toString());
}

6.1.2  控制台输出:

6.1.3 查看数据库信息

6.2测试查询

/**
* find
*/
@Test
public void findTest() {
Employee employee = employeeService.findEmployee(158);
System.out.println(employee.toString());
}

6.2.1控制台输出

6.3 测试查询所有数据库数据

/**
* 查詢所有
* @return
*/
public List<Employee> findAllEmployees() {
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e",Employee.class);
return query.getResultList();
}

6.3.1控制台输出

6.4 更新操作测试

/**
* 更新測試
*/
@Test
public void updateTest (){
transaction.begin();
Employee employee = employeeService.raiseEmployeeSalary(158, 1000);
transaction.commit();
System.out.println(employee);
}

6.4.1控制台输出

6.4.2数据库信息

6.5删除数据测试

/**
* 刪除測試
*/
@Test
public void deleteTest() {
transaction.begin();
employeeService.removeEmployee(158);
transaction.commit();
System.out.println("remove Employee 158");
}

6.5.1控制台输出

6.5.3查看数据库信息 id为158的记录被删除

参考资料:《Pro JPA2 Mastering the Java Persistence API》

【入门】 jpa--实体管理器的基本应用的更多相关文章

  1. Android 编程之入门开发目录管理器开发抽屉与文件分类-4

    在此目录管理APP里,我们能够尝试引用一些新的元素.在这里我给打击介绍一个叫抽屉的布局,QQ就用到了抽屉布局.不 过他们又在原有的基础上自己开发了新的抽屉布局.而且还蛮高大上的,顺便说说分类管理.这些 ...

  2. Android 编程之入门开发目录管理器开发文件事件操作-2

    上一篇博客,我们已经得到了目录列表,我们须要对文件列表子项加入事件,比方我们点击的是文件.就运行 打开操作,点击的是目录运行打开目录操作,遍历文件清单.以此类推直到最后一个是文件位置,关于文件 与目录 ...

  3. MyEclipse使用教程:使用REST Web Services管理JPA实体

    MyEclipse 在线订购专享特惠!火爆开抢>> MyEclipse最新版下载 使用REST Web Services来管理JPA实体.在逆向工程数据库表后生成REST Web服务,下面 ...

  4. Spring Data JPA实体详解

    1. Spring Data JPA实体概述 JPA提供了一种简单高效的方式来管理Java对象(POJO)到关系数据库的映射,此类Java对象称为JPA实体或简称实体.实体通常与底层数据库中的单个关系 ...

  5. Hibernate JPA实体继承的映射(一) 概述

    http://www.cnblogs.com/yingsong/p/5179975.html   注:文章中据说的实体指的是被@Entity注解的类. JPA中对象关系映射通常情况下是一个实体对应一个 ...

  6. JPA实体继承映射

    注意:据说,在本文所指的实体是@Entity注解的类. JPA在对象 - 关系映射通常情况下一个实体对应表,不管是什么这两个实体之间的关系.假设两个实体之间的继承关系.那么它是如何映射? JPA实体支 ...

  7. 69. JPA实体Bean的生命周期【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 在使用JPA的时候,如果对bean的生命周期不了解的话,难免会碰到一些问题,比如:InvalidDataAccessApiUsageExcepti ...

  8. 跟我学Spring3(9.2):Spring的事务之事务管理器

    原文出处: 张开涛9.2.1 概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口PlatformTransactionManage ...

  9. 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity

    问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...

  10. VS2013中Nuget程序包管理器控制台使用入门(三)-项目实战(原创)

    VS2013中Nuget程序包管理器控制台使用入门(三)-项目实战 1.给指定项目安装Newtonsoft.Json ,Version 4.5.11 PM> Install-Package Ne ...

随机推荐

  1. 【XLL 框架库函数】 TempActiveRow/TempActiveRow12

    创建一个包含所有激活工作表行的 XLOPER/XLOPER12 LPXLOPER TempActiveRow(WORD row); LPXLOPER12 TempActiveRow12(ROW row ...

  2. sql 删除表中某字段的重复数据

    重复字段:BarCode SELECT * FROM dbo.AssetBarCode WHERE BarCode IN (SELECT BarCode FROM dbo.AssetBarCode G ...

  3. Java的几种常用设计模式

    何为设计模式? 就是对一些常见问题进行归纳总结,并针对具体问题给出一套通用的解决办法(强调的是解决问题的思想): 在开发中,只要遇到这类问题,就可以直接使用这些设计模式解决问题. ---------- ...

  4. jquery.smint.js 页面菜单浮动之谷歌浏览器异常

    jquery.smint.js 做公司项目时,谷歌测试,页面向下拉,刷新后,导航栏菜单与顶部距离发生变动,并不在设置的relative元素top下固定像素 我的relative元素的高为80,然后在j ...

  5. MyBatis学习(二)

    mybatis开发dao的方法 作用范围 SqlSessionFactoryBuilder是以工具类方式来使用,需要创建sqlSessionFactory就new一个SqlSessionFactory ...

  6. 2.oracle 12c 创建-访问-关闭-删除PDB

    1.创建PDB SQL> select name from v$datafile;   NAME ------------------------------------------------ ...

  7. 复制Eclipse工作空间设置

    将新建的workspace下的.metadata.plugins内容全部删除: 将原来的workspace下的.metadata.plugins内容除了org.eclipse.core.resourc ...

  8. Linux 第06天

    Linux 第06天 1.SAMBA服务器————(linux和windows的文件共享) 1.1 安装 yum install samba -yum 1.2 配置文件 /etc/samba/smb. ...

  9. windows 2008 server NTP Server

    1. 选择一台服务器作为时间同步服务器. 2. 运行Regedit,打开注册表编辑器. 3. 找到注册表项HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Ser ...

  10. Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)

    概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例 ...