EntityManagerFactory和EntityManager是JPA所定义的接口,在没有SPRING之前,如果选择由程序管理,则需要自己创建一个EntityManagerFactory的单例,然后再由该单例在需要进行数据库操作的方法中创建EntityManager实例,并在查询完毕之后关闭(注意:如果忘记关闭,会造成数据库链接不能及时释放,很快整个数据库就会因为链接被用尽而导致程序报错)。
在Spring中如果你使用了SPRING JPA框架,则上述的事情由该框架完成,你只需要在Controller中注入EntityManager, 执行相关查询,不需要管理创建,关闭,SPRING替你完成。
POM文件中所加入的依赖:
<?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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demotest</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
对方法的调用:
package demotest.controller
import demotest.entity.Role
import demotest.entity.User
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.persistence.EntityManager
import javax.persistence.EntityManagerFactory
import javax.persistence.Query
import java.util.List
@CrossOrigin(origins = "http://localhost:3000")
@RestController
@PreAuthorize("hasRole('ADMIN')")
public class EmController {
//注入EntityManager
@Autowired
private EntityManager entityManager
@RequestMapping("/emtest/listuser")
public List<User> getUserlist() {
//直接在方法中调用,不用关闭,Spring会进行处理
Query query = entityManager.createNativeQuery("select * from user", User.class)
return query.getResultList()
}
@RequestMapping("/emtest/listrole")
public List<Role> test2() {
Query query = entityManager.createNativeQuery("select * from role", Role.class)
return query.getResultList()
}
}