springdata-jpa之jpa开端,入门程序之相关配置文件。

创建一个maven工程,引入相关依赖

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.hibernate.version>5.0.7.Final</project.hibernate.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</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>

 

相关约束配置文件=》注:文件名必须是:persistence.xml,放置路径必须在META-INF文件夹下,否则会报找不到文件得错误。

 

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <!--配置持久化单元   name:持久化单元的名称-->
    <!--transaction-type:事物的类型-->
    <!--RESOURCE_LOCAL  单数据库的事物-->
    <!--JTA:  分布式事物  跨数据库的事物-->

    <persistence-unit name="myjpa" transaction-type="RESOURCE_LOCAL">

        <properties>
            <!--数据库连接属性配置-->
            <property name="javax.persistence.jdbc.user" value="root"/>  
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/springdatajpa9502"/> //自己的数据库
            <!--hibernate属性配置-->
            <!--是否显示sql 语句  在控制台-->
            <property name="hibernate.show_sql" value="true"/>
            <!--是否格式化sql 语句-->
            <property name="hibernate.format_sql" value="true"/>
            <!--是否自动 创建数据库的表-->
            <!--值:update   程序自动创建表 假如有这个表就不在创建-->
            <!--create   程序自动创建表  有  先删  再创建-->
            <!--none   不会创建表-->
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

 

设置相关实体类,数据库中可以没有表,在运行时会自动创建数据表。

import javax.persistence.*;
// jpa的实体类
//配置实体类和数据库表的映射关系
@Entity
@Table(name="cst_customer")
public class Customer {
    // 配置主键生成的策略
    @GeneratedValue(strategy = GenerationType.IDENTITY)// 配置主键使用的字段
    @Id   //注意此处不写主键注解会报错
    @Column(name="cust_id")
    private long custId;
    @Column(name="cust_name")
    private String custName;
    @Column(name="cust_source")
    private String custSource;
    @Column(name="cust_industry")
    private String  custIndustry;
    @Column(name="cust_level")
    private String custLevel;
    @Column(name="cust_address")
    private String custAddress;
    @Column(name="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;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "custId=" + custId +
                ", custName='" + custName + '\'' +
                ", custSource='" + custSource + '\'' +
                ", custIndustry='" + custIndustry + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custAddress='" + custAddress + '\'' +
                ", custPhone='" + custPhone + '\'' +
                '}';
    }
}

 

 

等下更新测试程序。

posted on 2019-10-08 18:15  汪汪爱学习  阅读(96)  评论(0)    收藏  举报

导航