jpaHello

一、配置xml文件


<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
	
    <persistence-unit name="NewPersistenceUnit">
        <provider>org.hibernate.jpa.HibernatePersistenceProviderprovider>
        <properties>
            <property name="hibernate.connection.url" value=""/>
            <property name="hibernate.connection.driver_class" value=""/>
            <property name="hibernate.connection.username" value=""/>
            <property name="hibernate.connection.password" value=""/>


            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        properties>
    persistence-unit>
persistence>

1、persistence-unit 配置单元

name=jpaHello 测试单元的名称

2、 jpa 的提供者

<provider>org.hibernate.ejb.HibernatePersistenceprovider>

3、jpa 的核心配置

4、jqa的配置

name 描述
hibernate.connection.url jdbc:mysql:///test 数据库地址
hibernate.connection.driver_class com.jdbc.mysql.Driver 数据库驱动
hibernate.connection.username root 数据库用户名
hibernate.connection.password **** 数据库密码
hibernate.archive.autodetection class 生成方式
hibernate.format_sql true或false 是否格式化
hibernate.show_sql true或false 是否显示sql
hibernate.hbm2ddl.auto update 怎么更新

二、配置Jpa实体类

1、@Entity

表示他为Entiry的实体类

2、@Table(name = “User_01”)

配置表名

3、@Id

配置javaBean 的id

@GeneratedValue id生成策略

package com.kuang.pojo;

import javax.persistence.*;
import java.util.Date;

@Table(name = "User_01")
@Entity
public class User {
    private int id;
    private String name;
    private int age;
    private Date Birth;
    private boolean sex;

    @Id
    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirth() {
        return Birth;
    }

    @Temporal(value = TemporalType.DATE )
    public void setBirth(Date birth) {
        Birth = birth;
    }


    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }
}

三、在persistence.xml配置实体类

在persistence.xml 中配置 com.kuang.pojo.User


<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="NewPersistenceUnit">
        <class>com.kuang.pojo.Userclass>
        
          <provider>org.hibernate.ejb.HibernatePersistenceprovider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql:///jpa"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="123456"/>


            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        properties>
    persistence-unit>
persistence>

四、生成数据表数据

1、创建EntityManagerFactory

Persistence.createEntityManagerFactory(persistenceUnitName);

参数为测试单元名称

2、创建EntityManager对象

通过entityManagerFactory的createEntityManager的方法可以得到EntityMannager对象

3、得到事务EntityTransaction对象

通过entityManager的getTransaction的方法可以得到Transaction对象

4、开启事务

transaction.begin();开闭事务

5、配置实体类

User user = new User();

    user.setAge(18);
    user.setBirth(new Date());
    user.setName("smaleRed");
    user.setSex(true);

6、持久化实体类

entityManager.persist(user);

7、提交事务、关闭连接

关闭连接

        // 7、提交事务、关闭连接
        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();
public class Test {
    public static void main(String[] args) {

        String persistenceUnitName="jpaHello";

        // 1。创建EntityManagerFactory对象
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);

        // 2.创建EntityManager对象
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        // 3.得到事务EntityTransaction对象
        EntityTransaction transaction = entityManager.getTransaction();

        // 4、开启事务
        transaction.begin();
        // 5、配置实体类
        User user = new User();

        user.setAge(18);
        user.setBirth(new Date());
        user.setName("smaleRed");
        user.setSex(true);

        // 6、持久化实体类
        entityManager.persist(user);
        // 7、提交事务、关闭连接
        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();


    }
}

你可能感兴趣的:(学习记录,数据库,jpa,hibernate)