本系列博文包含 h5全栈 java全栈Python基础、前端开发、Web框架、缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!!

Spring data JPA 快速入门

1需求

​ 向客户中插入一条数据

​ 如果使用Jpa框架可以不用先建表 可以使用框架生成表

​ 2 实现步骤

​ a 创建工程 使用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>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
      </dependency>
      <!--hibernate 对jpa的支持-->
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>${project.hibernate.version}</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-c3p0</artifactId>
          <version>${project.hibernate.version}</version>
      </dependency>
      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
      </dependency>
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.6</version>
      </dependency>
  </dependencies>

​ b 创键一个配置文件

​ 配置文件的路径必须是:META-INF/persistence.xml

​ 配置连接数据库的相关配置

<?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/sprign_data-9501"/>
      <!--配置hibernate的属性-->
          <property name="hibernate.show_sql" value="true"></property>
          <!--sql语句是否格式化-->
          <property name="hibernate.format_sql" value="true"/>
          <!--是否自动创建数据库表
            value 可选值   create update   none
            create 程序自动创建数据库表 如果表存在 先删除后创建
            update 程序自动创建数据库表 如果表存在 不创建
            none   不会创建
          -->
          <property name="hibernate.hbm2ddl.auto" value="create"/>
      </properties>
  </persistence-unit>
</persistence>

​ c: 创键一个Entry类 对应数据库中每个表创键一个实体类


package cn.lijun.jpa.entity;

import javax.persistence.*;

/**
* @author lijun
* @date 2019/8/14 10:38
*/
// 该类是jpa的实体类
@Entity
// 配置实体类和数据库表中映射关系   name   对应的表名

@Table(name="cust_customer")
public class Customer {
  // 配置主键的生成策略     GenerationType.IDENTITY   自增长
  @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 getCustNmae() {
      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 + '\'' +
              '}';
  }
}

​ d: 编写测试程序 实现数据的添加

​ 1 创键一个EntityManagerFactory 对象 使用完关闭

​ 2 使用工厂对象EntityManagerFactory 就是一个连接

3 开启事物

​ 4 创键 Customer 对象

​ 5 使用Entitymanager 对象 的persist 方法向数据库添加数据

​ 6 事物提交

​ 7 关闭连接


package cn.lijun.jpa;

import cn.lijun.jpa.entity.Customer;
import org.junit.Test;

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

/**
* @author lijun
* @date 2019/8/14 10:52
*/
public class JpaTest {
  @Test
  public void firstTest(){
//       1 创键一个EntityManagerFactory 对象   使用完关闭
      EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
//       2 使用工厂对象EntityManagerFactory 就是一个连接
      EntityManager entityManager = factory.createEntityManager();
//       3 开启事物
      EntityTransaction transaction = entityManager.getTransaction();
      transaction.begin();
//       4 创键 Customer 对象
      Customer customer = new Customer();
      customer.setCustName("ruirui");
      customer.setCustLevel("vip");
      customer.setCustSource("网络");
      customer.setCustPhone("123456");
      customer.setCustAddress("懒人中心");
//       5 使用Entitymanager 对象 的persist 方法向数据库添加数据
      entityManager.persist(customer);
//       6 事物提交
      transaction.commit();

//       7 关闭连接
      entityManager.close();
      factory.close();


  }
}

 

posted on 2019-08-14 12:16  李俊互联网技术传播者  阅读(143)  评论(0编辑  收藏  举报