JPA(二)JPA配置

一、依赖导入,以maven 工程导入坐标为例


<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.hibernate.version>5.0.7.Final</project.hibernate.version>
    </properties>
    <dependencies>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
        <!-- hibernate 对 jpa 的支持包 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${project.hibernate.version}</version>
        </dependency>
        <!-- c3p0 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${project.hibernate.version}</version>
        </dependency>
        <!-- log 日志 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- Mysql and MariaDB -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
    </dependencies>

二、实体类

package com.it.jpa.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity //声明实体类
@Table(name="cst_customer")//建立实体类和表的映射关系
public class Customer {

    @Id//声明当前私有属性为主键
    @GeneratedValue(strategy=GenerationType.IDENTITY) //配置主键的生成策略
    @Column(name="cust_id") //指定和表中 cust_id 字段的映射关系
    private Long custId;

    @Column(name="cust_name") //指定和表中 cust_name 字段的映射关系
    private String custName;
    @Column(name="cust_source")//指定和表中 cust_source 字段的映射关系
    private String custSource;
    @Column(name="cust_industry")//指定和表中 cust_industry 字段的映射关系
    private String custIndustry;
    @Column(name="cust_level")//指定和表中 cust_level 字段的映射关系
    private String custLevel;
    @Column(name="cust_address")//指定和表中 cust_address 字段的映射关系
    private String custAddress;
    @Column(name="cust_phone")//指定和表中 cust_phone 字段的映射关系
    private String custPhone;



    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
常用注解的说明
@Entity
    作用:指定当前类是实体类。
@Table
    作用:指定实体类和表之间的对应关系。
    属性:
        name:指定数据库表的名称
@Id
    作用:指定当前字段是主键。
@GeneratedValue
    作用:指定主键的生成方式。。
    属性:
        generator:指定引用 hibernate 中声明的主键策略
        strategy :指定主键生成策略。
                1)、AUTO 自动选择一个最适合底层数据库的主键生成策略。如MySQL会自动对应auto increment。这个是默认选项,即如果只写@GeneratedValue,等价于@GeneratedValue(strategy=GenerationType.AUTO)。

              2)、IDENTITY 表自增长字段,Oracle不支持这种方式。

              3)、SEQUENCE 通过序列产生主键,MySQL不支持这种方式。

              4)、TABLE 通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库移植。不同的JPA实现商生成的表名是不同的,如 OpenJPA生成openjpa_sequence_table表,Hibernate生成一个hibernate_sequences表,而TopLink则生成sequence表。这些表都具有一个序列名和对应值两个字段,如SEQ_NAME和SEQ_COUNT。

@Column
    作用:指定实体类属性和数据库表之间的对应关系
    属性:
        name:指定数据库表的列名称。
        unique:是否唯一
        nullable:是否可以为空
        inserttable:是否可以插入
        updateable:是否可以更新
        columnDefinition: 定义建表时创建此列的 DDL
        secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在
        从表的名字

三、JPA 核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
    version="2.0">


    <!-- 
        在 maven 工程的 resources 路径下创建一个名为 META-INF 的文件夹,
        在此文件夹下创建一个名为persistence.xml 的配置文件。
        注意:META-INF文件夹名称不能修改。persistence.xml 文件名称不能改。
     -->
    <!--
          配置持久化单元 name:
          持久化单元名称 transaction-type:
                事务类型 RESOURCE_LOCAL:
                本地事务管理 JTA:分布式事务管理 
    -->
    <persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL">
        <!--配置 JPA 规范的服务提供商 -->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <!-- 数据库驱动 -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <!-- 数据库地址 -->
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/springdb" />
            <!-- 数据库用户名 -->
            <property name="javax.persistence.jdbc.user" value="root" />
            <!-- 数据库密码 -->
            <property name="javax.persistence.jdbc.password" value="root" />
            <!--jpa 提供者的可选配置:我们的 JPA 规范的提供者为 hibernate,所以 jpa 的核心配 置中兼容 hibernate 
                的配 -->
                <!-- 
                    show_sql:       显示sql
                    format_sql:     格式化sql
                    hbm2ddl.auto:     自动创建|更新|验证数据库表结构
                 -->
            <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>

猜你喜欢

转载自blog.csdn.net/houysx/article/details/80698043
JPA