码迷,mamicode.com
首页 > 其他好文 > 详细

【入门】 jpa--实体管理器的基本应用

时间:2016-12-06 20:25:08      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:property   t_sql   .sh   imp   let   sch   gen   idt   create   

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的记录被删除

技术分享

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

【入门】 jpa--实体管理器的基本应用

标签:property   t_sql   .sh   imp   let   sch   gen   idt   create   

原文地址:http://www.cnblogs.com/watchfree/p/6138907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!