一:JDBC是什么?
JDBC统一了Java应用程序访问数据库的标准。
二:什么是JPA?
JPA统一了Java应用程序使用使用ORM框架的方式。
配置文件说明:
三:使用JPA的第一个实例。
1.创建JPA项目,也可以直接创建java项目,然后创建persistence.xml文件
2.导入jar包
3.persistence.xml :
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="Jpa-01" transaction-type="RESOURCE_LOCAL"> <!-- 配置使用什么ORM产品作为JPA的实现 1.实际上配置的是javax.persistence.spi.PersistenceProvider接口的实现类 2.若JPA项目中只有一个JPA的实现产品,则也可以不配置该节点 --> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- 添加持久化类 --> <class>com.atguigu.jpa.helloworld.Customer</class> <properties> <!-- 连接数据库的基本信息 --> <property name = "javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"></property> <property name = "javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"></property> <property name = "javax.persistence.jdbc.user" value="root"></property> <property name = "javax.persistence.jdbc.password" value="root"></property> <!-- 配置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>
4.创建实体类:
package com.atguigu.jpa.helloworld; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name = "JPA_CUSTOMERS") @Entity public class Customer { private Integer id; private String lastName; private String email; private int age; @GeneratedValue( strategy = GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="LAST_NAME") public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } 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; } }
5.测试类:
package com.atguigu.jpa.helloworld; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class Main { public static void main(String[] args) { //1.创建EntitymanagerFactory String persistenceUnitName = "Jpa-01"; EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); //2.创建EntityManager EntityManager entityManager = entityManagerFactory.createEntityManager(); //3.开启事务 EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); //4.进行持久化操作 Customer customer = new Customer(); customer.setAge(12); customer.setEmail("tom@atguigu.com"); customer.setLastName("tom"); entityManager.persist(customer); //5.提交事务 transaction.commit(); //6.关闭entityManager entityManager.close(); //7.关闭EntityManagerFactory entityManagerFactory.close(); } }
6.整体结构:
7.运行
四月 21, 2019 9:44:19 上午 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 四月 21, 2019 9:44:19 上午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.4.Final} 四月 21, 2019 9:44:19 上午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 四月 21, 2019 9:44:19 上午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 四月 21, 2019 9:44:20 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 四月 21, 2019 9:44:20 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 四月 21, 2019 9:44:20 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: true 四月 21, 2019 9:44:20 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///jpa] 四月 21, 2019 9:44:20 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****, autocommit=true, release_mode=auto} 四月 21, 2019 9:44:20 上午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 四月 21, 2019 9:44:20 上午 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 四月 21, 2019 9:44:20 上午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory 四月 21, 2019 9:44:20 上午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory 四月 21, 2019 9:44:22 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000228: Running hbm2ddl schema update 四月 21, 2019 9:44:22 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000102: Fetching database metadata 四月 21, 2019 9:44:22 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000396: Updating schema 四月 21, 2019 9:44:22 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata INFO: HHH000262: Table not found: JPA_CUSTOMERS 四月 21, 2019 9:44:22 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata INFO: HHH000262: Table not found: JPA_CUSTOMERS 四月 21, 2019 9:44:22 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata INFO: HHH000262: Table not found: JPA_CUSTOMERS 四月 21, 2019 9:44:22 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Hibernate: insert into JPA_CUSTOMERS (age, email, LAST_NAME) values (?, ?, ?) 四月 21, 2019 9:44:22 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH000030: Cleaning up connection pool [jdbc:mysql:///jpa]
8.查看表结构
9.完成