Spring Orm 简明教程
Spring ORM - Update Project
让我们在相关的包中向项目添加以下类。
Let’s add the following classes in the project in the relevant packages.
Employee 类,用于保留在数据库中。
Employee class to be persisted in database.
Employee.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 类,用于对数据库执行操作。
Employee DAO class to do operations on database.
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 类。
Employee Service class to use Employee DAO class.
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();
}
}
Application Configuration,供 Spring 框架使用。
Application Configuration to be used by Spring framework.
AppConfig.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;
}
}
Main Application,用于运行并测试功能。
Main Application to run and test the functionalities.
MainApp.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();
}
}