JPA本身是一种规范,它的本质是一种ORM规范(不是ORM框架,因为JPA并未提供ORM实现,只是制定了规范)因为JPA是一种规范,所以,只是提供了一些相关的接口,但是接口并不能直接使用,JPA底层需要某种JPA实现,JPA现在就是Hibernate功能的一个子集
- "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.0modelVersion>
-
- <groupId>com.lbgroupId>
- <artifactId>jpa-hibernateartifactId>
- <version>1.0-SNAPSHOTversion>
-
- <properties>
- <maven.compiler.target>1.8maven.compiler.target>
- <maven.compiler.source>1.8maven.compiler.source>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.13version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>org.hibernategroupId>
- <artifactId>hibernate-entitymanagerartifactId>
- <version>5.4.32.Finalversion>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.46version>
- dependency>
-
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.16.10version>
- dependency>
-
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- configuration>
- plugin>
- plugins>
- build>
- project>
- hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
- <session-factory>
-
- <property name="hbm2ddl.auto">updateproperty>
-
- <property name="show_sql">trueproperty>
-
- <property name="format_sql">trueproperty>
-
- <property name="dialect">org.hibernate.dialect.MySQL55Dialectproperty>
-
-
- <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
- <property name="connection.url">jdbc:mysql://localhost:3306/springdata_jpa?useUnicode=true&characterEncoding=UTF8&useSSL=falseproperty>
- <property name="connection.username">rootproperty>
- <property name="connection.password">rootproperty>
-
-
- <property name="connection.pool_size">5property>
-
- <property name="jdbc.fetch_size">50property>
-
- <property name="jdbc.batch_size">30property>
-
- <property name="current_session_context_class">threadproperty>
-
-
-
-
- <mapping class="com.lb.pojo.Customer" />
-
- session-factory>
- hibernate-configuration>
注意dialect方言的选择, 会影响是否可以成功创建表,或者表的引擎的选择。
- @Data
- @Entity //作为hibernate实体类
- @Table(name = "tb_customer") //映射的表名
- public class Customer {
- /**
- * @Id 声明主键的配置
- * @GeneratedValue 配置主键的生成策略
- * strategy
- * GenerationType.IDENTITY : 自增 mysql
- * 底层数据库必须支持自动增长。对Id自增
- * GenerationType.SEQUENCE : 序列,oracle
- * 定数据库必须支持序列
- * GenerationType.TABLE : JPA提供的一种机制,通过一张数据库表的形式帮助我们完成主键生成
- * GenerationType.AUTO : 由程序自动的帮助主键生成策略
- * @Column 配置属性和字段的映射关系
- * name : 数据库字段名称
- */
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "cust_id")
- private Long custId;
-
- @Column(name = "cust_name")
- private String custName;
-
- @Column(name = "cust_address")
- private String custAddress;
- }
- public class HibernateTest {
- private SessionFactory sessionFactory;
-
- @Before
- public void init() {
- StandardServiceRegistry registry =
- new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
-
- sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
-
- }
- @Test
- public void test01(){
- try(Session session = sessionFactory.openSession()){
- Transaction tx = session.beginTransaction();
-
- Customer customer = new Customer();
- customer.setCustName("李四");
- session.save(customer);
-
- tx.commit();
- }
- }
- }
注意当Customer对应的表在数据库中不存在时,会执行创建表的操作,在进行save操作
log:
- Hibernate:
-
- create table tb_customer (
- cust_id bigint not null auto_increment,
- cust_address varchar(255),
- cust_name varchar(255),
- primary key (cust_id)
- ) engine=InnoDB
- Hibernate:
- insert
- into
- tb_customer
- (cust_address, cust_name)
- values
- (?, ?)
- public class HibernateTest {
- private SessionFactory sessionFactory;
-
- @Before
- public void init() {
- StandardServiceRegistry registry =
- new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
-
- sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
-
- }
-
- @Test
- public void test_save() {
- try (Session session = sessionFactory.openSession()) {
- Transaction tx = session.beginTransaction();
-
- Customer customer = new Customer();
- customer.setCustName("李四");
- session.save(customer);
-
- tx.commit();
- }
- }
-
- @Test
- public void test_find() {
- try (Session session = sessionFactory.openSession()) {
- Transaction tx = session.beginTransaction();
-
- Customer customer = session.find(Customer.class, 1L);
- System.out.println(customer);
- tx.commit();
- }
- }
-
- @Test
- public void test_load() {
- try (Session session = sessionFactory.openSession()) {
- Transaction tx = session.beginTransaction();
- //load懒加载,使用时才会执行查询
- Customer customer = session.load(Customer.class, 1L);
- System.out.println("========================");
- System.out.println(customer);
- tx.commit();
- }
- }
-
- @Test
- public void test_update() {
- try (Session session = sessionFactory.openSession()) {
- Transaction tx = session.beginTransaction();
-
- Customer customer = new Customer();
- customer.setCustId(1L);
- customer.setCustName("李四2");
- //有id修改,没有id插入,或者直接使用update
- session.saveOrUpdate(customer);
-
- tx.commit();
- }
- }
-
- @Test
- public void tes