<?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>com</groupId> <artifactId>jpa</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.14</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.4.1.Final</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.6.3</version> </dependency> </dependencies> <build> <plugins> <!-- 指定jdk --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence 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_2.xsd" version="2.2"> <persistence-unit name="jpaname" transaction-type="RESOURCE_LOCAL"> <!-- 配置使用什么 ORM 产品来作为 JPA 的实现 1. 实际上配置的是 javax.persistence.spi.PersistenceProvider 接口的实现类 2. 若 JPA 项目中只有一个 JPA 的实现产品, 则也可以不配置该节点. --> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <!-- 添加持久化类 --> <class>com.jpa.entity.Customer</class> <!-- 配置二级缓存的策略 ALL:所有的实体类都被缓存 NONE:所有的实体类都不被缓存. ENABLE_SELECTIVE:标识 @Cacheable(true) 注解的实体类将被缓存 DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的所有实体类 UNSPECIFIED:默认值,JPA 产品默认值将被使用 --> <!--<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>--> <properties> <!-- 连接数据库的基本信息 --> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://xxx.xxx.xxx.xxx/jpa?useSSL=false"/> <property name="javax.persistence.jdbc.user" value="xxx"/> <property name="javax.persistence.jdbc.password" value="xxx"/> <!-- 配置 JPA 实现产品的基本属性. 配置 hibernate 的基本属性 --> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <!-- 二级缓存相关 --> <!--<property name="hibernate.cache.use_second_level_cache" value="true"/>--> <!--<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>--> <!--<property name="hibernate.cache.use_query_cache" value="true"/>--> </properties> </persistence-unit> </persistence>
package com.jpa.entity; import javax.persistence.*; @Table(name = "JPA_CUTOMERS") @Entity public class Customer { private Integer id; private String lastName; private String email; private int age; public Customer() {} public Customer(String lastName, int age) { super(); this.lastName = lastName; this.age = age; } @GeneratedValue(strategy = GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "LAST_NAME", length = 50, nullable = false) public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Basic public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Customer{" + "id=" + id + ", lastName=‘" + lastName + ‘\‘‘ + ", email=‘" + email + ‘\‘‘ + ", age=" + age + ‘}‘; } }
package com.jpa; import com.jpa.entity.Customer; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { //1. 创建 EntitymanagerFactory String persistenceUnitName = "jpaname"; Map<String, Object> properites = new HashMap<String, Object>(10); properites.put("hibernate.show_sql", true); EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); // Persistence.createEntityManagerFactory(persistenceUnitName, properites); //2. 创建 EntityManager. 类似于 Hibernate 的 SessionFactory EntityManager entityManager = entityManagerFactory.createEntityManager(); //3. 开启事务 EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); //4. 进行持久化操作 Customer customer = new Customer(); customer.setAge(12); customer.setEmail("tom@tom.com"); customer.setLastName("Tom"); entityManager.persist(customer); //5. 提交事务 transaction.commit(); //6. 关闭 EntityManager entityManager.close(); //7. 关闭 EntityManagerFactory entityManagerFactory.close(); } }
原文:https://www.cnblogs.com/jhxxb/p/10347729.html