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();


  }
}

最新文章

  1. css 垂直水平居中总结
  2. docker-9 supervisord 参考docker从入门到实战
  3. linux根文件系统制作
  4. jquery层级原则器(匹配父元素下的子元素)
  5. selenium操作H5视频
  6. apply,call,bind的区别
  7. Linux第十一次学习笔记
  8. eclipse 拨打电话、拨号,发短信
  9. USACO Section 4.2: Drainage Ditches
  10. (一)使用log4net生成日志文件
  11. com.mchange.v2.c3p0.ComboPooledDataSource
  12. 实现html转Xml
  13. CLOUDSTACK接管VCENTER,意外频出,但最终搞定
  14. webpack+babel项目在IE下报Promise未定义错误引出的思考
  15. 跟我一起读postgresql源码(十四)——Executor(查询执行模块之——Join节点(下))
  16. anaconda 命令集合
  17. Python语言学习之Python入门到进阶
  18. 题解-TIOJ1905 最理想的身高差
  19. python 爆破
  20. javaweb学习3——验证码

热门文章

  1. python 环境准备-centos7
  2. C语句模拟多任务实例
  3. eclipse设置tomcat部署目录地址
  4. leetcode-165周赛-1276-不浪费原料的汉堡制作方案
  5. SparkStreaming反压机制
  6. php abs()函数 语法
  7. nucleus plus学习总结(后续)
  8. windows 下安装python 的requests模块
  9. 接口测试——postman安装
  10. 从输入 URL 到页面展示,到底发生了什么