• Hibernate笔记7--JPA CRUD


    1.环境搭建,注意包结构的问题,src下建立名为META-INF的文件夹,放persistence.xml,位置放错,读不到会报错.

      1     <?xml version="1.0" encoding="UTF-8"?>
      2      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
      3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      4          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
      5         http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      6          version="2.0">
      7 
      8          <!--配置持久化单元  -->
      9          <persistence-unit name="unit" transaction-type="RESOURCE_LOCAL">
     10              <!--配置jpa持久化类提供者  -->
     11              <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
     12              <!--配置数据库 Hibernate基本信息  -->
     13              <properties>
     14                  <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
     15                  <property name="hibernate.connection.url" value="jdbc:mysql:///day581"/>
     16                  <property name="hibernate.connection.username" value="root"/>
     17                  <property name="hibernate.connection.password" value="root"/>
     18                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
     19                  <property name="hibernate.hbm2ddl.auto" value="update"/>
     20                  <property name="hibernate.show_sql" value="true"/>
     21                  <property name="hibernate.format_sql" value="true"/>
     22                  <property name="hibernate.connection.isolation" value="4"/>
     23                      <!-- c3p0连接池 -->
     24                  <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
     25              </properties>
     26          </persistence-unit>
     27      </persistence>

    2.实体类编写

      1 package huguangqin.com.cnblogs.entity;
      2  import java.io.Serializable;
      3  import javax.persistence.Column;
      4  import javax.persistence.Entity;
      5  import javax.persistence.GeneratedValue;
      6  import javax.persistence.GenerationType;
      7  import javax.persistence.Id;
      8  import javax.persistence.Table;
      9 
     10 @Entity
     11  @Table(name = "cst_customer")
     12  public class Customer implements Serializable {
     13      private static final long serialVersionUID = 1L;
     14 
     15     @Id
     16      @GeneratedValue(strategy = GenerationType.IDENTITY)
     17      @Column(name = "cust_id")
     18      private Long custId;
     19 
     20     @Column(name = "cust_name")
     21      private String custName;
     22 
     23     @Column(name = "cust_source")
     24      private String custSource;
     25 
     26     @Column(name = "cust_industry")
     27      private String custIndustry;
     28 
     29     @Column(name = "cust_level")
     30      private String custLevel;
     31 
     32     @Column(name = "cust_address")
     33      private String custAddress;
     34 
     35     @Column(name = "cust_phone")
     36      private String custPhone;
     37 
     38     public Long getCustId() {
     39          return custId;
     40      }
     41 
     42     public void setCustId(Long custId) {
     43          this.custId = custId;
     44      }
     45 
     46     public String getCustName() {
     47          return custName;
     48      }
     49 
     50     public void setCustName(String custName) {
     51          this.custName = custName;
     52      }
     53 
     54     public String getCustSource() {
     55          return custSource;
     56      }
     57 
     58     public void setCustSource(String custSource) {
     59          this.custSource = custSource;
     60      }
     61 
     62     public String getCustIndustry() {
     63          return custIndustry;
     64      }
     65 
     66     public void setCustIndustry(String custIndustry) {
     67          this.custIndustry = custIndustry;
     68      }
     69 
     70     public String getCustLevel() {
     71          return custLevel;
     72      }
     73 
     74     public void setCustLevel(String custLevel) {
     75          this.custLevel = custLevel;
     76      }
     77 
     78     public String getCustAddress() {
     79          return custAddress;
     80      }
     81 
     82     public void setCustAddress(String custAddress) {
     83          this.custAddress = custAddress;
     84      }
     85 
     86     public String getCustPhone() {
     87          return custPhone;
     88      }
     89 
     90     public void setCustPhone(String custPhone) {
     91          this.custPhone = custPhone;
     92      }
     93 
     94     // toString
     95      @Override
     96      public String toString() {
     97          return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
     98                  + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
     99                  + ", custPhone=" + custPhone + "]";
    100      }
    101 
    102 }
    103 


    3.工具类-JPAUtil,用于启动创建一次EntityManagerFactory,并获取EntityManager
       

      1  package huguangqin.com.cnblogs.util;
      2      import javax.persistence.EntityManager;
      3      import javax.persistence.EntityManagerFactory;
      4      import javax.persistence.Persistence;
      5 
      6     public class JPAUtil {
      7          private static EntityManagerFactory factory;
      8 
      9         static {
     10              factory = Persistence.createEntityManagerFactory("unit");
     11          }
     12 
     13         public static EntityManager getEntityManager() {
     14              return factory.createEntityManager();
     15          }
     16      }
     17 

    4.测试代码

      1 package huguangqin.com.cnblogs.demo;
      2 
      3 import javax.persistence.EntityManager;
      4  import javax.persistence.EntityTransaction;
      5 
      6 import org.junit.Test;
      7 
      8 import huguangqin.com.cnblogs.entity.Customer;
      9  import huguangqin.com.cnblogs.util.JPAUtil;
     10 
     11 /**
     12   * 保存一个数据到客户表中:persist
     13   */
     14  public class JPATest1 {
     15      @Test
     16      public void addTest() {
     17          // 获取EntityManager
     18          EntityManager em = JPAUtil.getEntityManager();
     19          // 得到事务对象
     20         EntityTransaction et = em.getTransaction();
     21          // 开启事务
     22         et.begin();
     23          // CRUD
     24          Customer cust = new Customer();
     25          cust.setCustName("阿里");
     26          // 持久化
     27         em.persist(cust);
     28          // 提交事务
     29         et.commit();
     30          // 关闭资源
     31         em.close();
     32      }
     33 
     34     /**
     35       * 根据id查询:find
     36       */
     37      @Test
     38      public void findTest() {
     39          // 获取EntityManager
     40          EntityManager em = JPAUtil.getEntityManager();
     41          // 得到事务对象
     42         EntityTransaction et = em.getTransaction();
     43          // 开启事务
     44         et.begin();
     45          // 根据ID查询
     46         Customer customer = em.find(Customer.class, 1L);
     47          System.out.println(customer);
     48 
     49         // 提交事务
     50         et.commit();
     51          // 关闭资源
     52         em.close();
     53      }
     54 
     55     /**
     56       * 延迟加载测试(getReference),用到的时候才发送sql
     57       */
     58     @Test
     59      public void delayTest() {
     60          // 获取EntityManager
     61          EntityManager em = JPAUtil.getEntityManager();
     62          // 获取事务对象
     63         EntityTransaction et = em.getTransaction();
     64         // 开启事务
     65         et.begin();
     66          // 延迟加载
     67         Customer cust = em.getReference(Customer.class, 1L);
     68          System.out.println(cust);
     69          // 提交事务
     70         et.commit();
     71          // 关闭资源
     72         em.close();
     73      }
     74 
     75     /**
     76       * 修改:查询-修改(setter)
     77       */
     78      @Test
     79      public void updateTest() {
     80          // 获取EntityManager
     81          EntityManager em = JPAUtil.getEntityManager();
     82          // 获取事务对象
     83         EntityTransaction et = em.getTransaction();
     84          // 开启事务
     85         et.begin();
     86          // 修改
     87         Customer cust = em.find(Customer.class, 1L);
     88          cust.setCustName("Tecent");
     89 
     90         // 提交事务
     91         et.commit();
     92          // 关闭资源
     93         em.close();
     94      }
     95 
     96     /**
     97       *删除 remove
     98       */
     99      @Test
    100      public void delTest() {
    101          // 获取EntityManager
    102          EntityManager em = JPAUtil.getEntityManager();
    103          // 获取事务对象
    104         EntityTransaction et = em.getTransaction();
    105          // 开启事务
    106         et.begin();
    107          // 删除
    108         Customer cust = em.find(Customer.class, 1L);
    109          em.remove(cust);
    110          // 提交事务
    111         et.commit();
    112          // 关闭资源
    113         em.close();
    114      }
    115  }
    116 

  • 相关阅读:
    CF #305(Div.2) D. Mike and Feet(数学推导)
    CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)
    2015百度之星资格赛.1004放盘子(数学推导)
    poj.1988.Cube Stacking(并查集)
    lightoj.1048.Conquering Keokradong(二分 + 贪心)
    CMD 命令汇总
    PLSQL 安装与配置 Oracle
    用 jQuery 实现简单倒计时功能
    C# 从服务器下载文件并保存到客户端
    用 NPOI 组件实现数据导出
  • 原文地址:https://www.cnblogs.com/huguangqin/p/7436243.html
Copyright © 2020-2023  润新知