Spring ORM - 更新项目


让我们在项目中的相关包中添加以下类。

项目结构

要保存在数据库中的员工类。

员工.java

package com.tutorialspoint.jpa.entity;

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(name = "Employees")
public class Employee {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY) 	
   private int id;

   @Column(name = "NAME")
   private String name;

   @Column(name = "SALARY")
   private double salary;

   @Column(name = "DESIGNATION")
   private String designation;

   public Employee( ) {
   }

   public Employee(String name, double salary, String designation) {
      this.name = name;
      this.salary = salary;
      this.designation = designation;
   }
   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 double getSalary() {
      return salary;
   }
   public void setSalary(double salary) {
      this.salary = salary;
   }
   public String getDesignation() {
      return designation;
   }
   public void setDesignation(String designation) {
      this.designation = designation;
   }
}

Employee DAO 类对数据库进行操作。

EmployeeDao.java

package com.tutorialspoint.jpa.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.springframework.stereotype.Repository;
import com.tutorialspoint.jpa.entity.Employee;

@Repository
public class EmployeeDao {
   @PersistenceContext
   private EntityManager em;

   public void add(Employee employee) {
      em.persist(employee);
   }
   public List<Employee> listEmployees() {
      CriteriaQuery<Employee> criteriaQuery = em.getCriteriaBuilder().createQuery(Employee.class);
      @SuppressWarnings("unused")
      Root<Employee> root = criteriaQuery.from(Employee.class);
      return em.createQuery(criteriaQuery).getResultList();
   }
}

Employee Service 类使用 Employee DAO 类。

EmployeeService.java

package com.tutorialspoint.jpa.service;

import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tutorialspoint.jpa.dao.EmployeeDao;
import com.tutorialspoint.jpa.entity.Employee;

@Service
public class EmployeeService {
   @Autowired
   private EmployeeDao employeeDao;

   @Transactional
   public void add(Employee employee) {
      employeeDao.add(employee);
   }
   @Transactional()
   public List<Employee> listEmployees() {
      return employeeDao.listEmployees();
   }
}

Spring 框架使用的应用程序配置。

应用程序配置.java

package com.tutorialspoint.jpa;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.tutorialspoint.jpa.dao"),
@ComponentScan("com.tutorialspoint.jpa.service") })
public class AppConfig {
   @Bean
   public LocalEntityManagerFactoryBean geEntityManagerFactoryBean() {
      LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
      factoryBean.setPersistenceUnitName("Hibernate_JPA");
      return factoryBean;
   }
   @Bean
   public JpaTransactionManager geJpaTransactionManager() {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(geEntityManagerFactoryBean().getObject());
      return transactionManager;
   }
}

用于运行和测试功能的主应用程序。

主应用程序.java

package com.tutorialspoint.jpa;

import java.sql.SQLException;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.tutorialspoint.jpa.entity.Employee;
import com.tutorialspoint.jpa.service.EmployeeService;


public class MainApp {
   public static void main(String[] args) throws SQLException {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      EmployeeService employeeService = context.getBean(EmployeeService.class);

      employeeService.add(new Employee("Julie", 10000, "Technical Manager"));
      employeeService.add(new Employee("Robert", 20000, "Senior Manager"));
      employeeService.add(new Employee("Anil", 5000, "Software Engineer"));

      // Get Persons
      List<Employee> employees = employeeService.listEmployees();
      for (Employee employee : employees) {
         System.out.println("Id : "+employee.getId());
         System.out.println("Name : "+employee.getName());
         System.out.println("Salary = "+employee.getSalary());
         System.out.println("Designation = "+employee.getDesignation());
         System.out.println();
      }
      context.close();
   }
}