JPA学习(一) 创建DEMO

一个简单的Maven项目的demo头都给我弄大了。赶紧记录一下

跟着视频走的。下包,导依赖。最后报错 太恶心了。用maven直接将对应的依赖导进来就可以了,没必要手动导包


目录

1.导包

2.配置persistence.xml

3.创建持久类

4.测试运行


1.导包

其实依赖不多。一个简单的demo需要三个依赖就足够了

 <dependencies>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.7.Final</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>
    </dependencies>

第一个是herbinate 的依赖包。里面会将核心的一些依赖包导入进去

第二个是测试类依赖包

第三个是mysql的驱动依赖包

2.配置persistence.xml

这个比较关键。

路径实在当前项目的resources包下的META-INF文件夹下 一定是这样的。

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_version=1">
    <persistence-unit name="mysqlJPA" transaction-type="RESOURCE_LOCAL">
        <properties>
            <!--方言 不同数据库驱动的方言是不一致的-->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> 
            <!--连接数据库的连接属性-->
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpadata"/>             
            <!-- 指定hibernate是否要根据持久化类自动建立数据表 hbm2ddl是吧映射文件转成了ddl语句 -->
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <!-- 显示Hibernate持久化操作所生成的SQL -->
            <property name="hibernate.show_sql" value="true"/>
            <!-- 将SQL脚本进行格式化后再输出 -->
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

 

3.创建持久类

一个employee类



import javax.persistence.*;
import java.io.Serializable;


@Entity
//把实体转成数据库的一张表
@Table(name = "t_employee")
//配置表明 如果没有@Table 默认使用类名作为表名
public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id //配置主键
    @GeneratedValue(strategy = GenerationType.AUTO) //配置主键的生成
    @Column(name = "t_id") //修改数据库的列名
    private Long id;
    @Column(name = "t_name")

    private String name;
    @Column(name = "t_password")
    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

4.测试运行


import org.junit.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class HelloTest {

    @Test
    public void saveTable() {
        //获取实例管理对象
        String persistenceUnitName = "mysqlJPA"  ;
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
        //获取实例管理
        EntityManager manager = entityManagerFactory.createEntityManager();
    }
}

运行可以看到 

十月 14, 2019 3:55:34 下午 org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
十月 14, 2019 3:55:34 下午 org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
十月 14, 2019 3:55:34 下午 org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
十月 14, 2019 3:55:34 下午 org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
	name: mysqlJPA
	...]
十月 14, 2019 3:55:35 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.7.Final}
十月 14, 2019 3:55:35 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十月 14, 2019 3:55:35 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十月 14, 2019 3:55:35 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
十月 14, 2019 3:55:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
十月 14, 2019 3:55:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [null] at URL [jdbc:mysql://localhost:3306/jpadata]
十月 14, 2019 3:55:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
十月 14, 2019 3:55:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
十月 14, 2019 3:55:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十月 14, 2019 3:55:36 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
十月 14, 2019 3:55:36 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
十月 14, 2019 3:55:37 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: 
    drop table if exists t_employee
Hibernate: 
    create table t_employee (
        t_id bigint not null auto_increment,
        t_name varchar(255),
        t_password varchar(255),
        primary key (t_id)
    )
十月 14, 2019 3:55:37 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete

去连接了数据库  因为配置文件中的自动建表使用的create 每次都会检测如果有表 会删除并且重新建立

可以改成update,这样就不会每次将表给删除了


2019.10.15更,关于策略的用法 

配置自动生成表
属性名称:hibernate.hbm2ddl.auto
如果需要生成表,只需要表名列名和对应列名的类型。
创建表的时间就是在 new 出 EntityManagerFactory对象的时候


属性名称:create-drop
hibernate.hbm2ddl.auto create-drop
开发时候不会用这个
先删表  创建表 再删除表 drop ->create ->drop
删表时间 关闭EntityManagerFactory的时候

属性:create 只有测试时用
hibernate.hbm2ddl.auto create
删表->建表  不删表
如果更改了domain的类的映射,会马上生效

属性:update 测试和web项目使用
hibernate.hbm2ddl.auto update
不删表
如果没有表 那么就根据最新的映射信息创建表
如果表里的属性存在了 如果Domain修改了属性,不会更新的
如果需要更新,需要删除当前对应的表或者删除对应的列名
如果表里没有这个属性 映射文件存在 增加这个列


属性:validate 正式使用
hibernate.hbm2ddl.auto validate
不删表
表不存在,会抛出异常
映射文件少属性,表里的列名比映射文件多 不会报错,反之报错(missing table)
以及定义的属性生成的列名,使用了数据库的关键字(语法错误)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值