国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JPA學習筆記【二】【helloworld】

2019-11-11 04:26:19
字體:
來源:轉載
供稿:網友

使用JPA持久化對象的步驟

此處有個小問題,在創建JPA工程的時候,提示下圖這樣一個問題,at least one user library must be selected,不解決則無法創建這個工程,網上搜索了下,參考http://www.cnblogs.com/lj95801/p/5001882.html解決了此問題。

persistence.xml代碼

```

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0"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"><persistence-unit name="jpa-1"><!-- 配置使用什么ORM產品來做作為JPA的實現 1.實際上配置的是javax.persistence.spi.PersistencePRovider接口的實現類 2.若JPA項目中只有一個JPA實現產品,則也可以不配置該節點 --><provider>org.hibernate.ejb.HibernatePersistence</provider><class>com.abcd.helloworld.Customer</class><properties><!-- 鏈接數據庫的基本信息 --><property name="javax.persistence.jdbc.driver" value="com.MySQL.jdbc.Driver" /><property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa" /><property name="javax.persistence.jdbc.user" value="test" /><property name="javax.persistence.jdbc.passWord" value="1234" /><!-- 配置JPA實現產品的基本屬性,配置Hibernate的基本屬性 --><property name="hibernate.hbm2ddl.auto" value="update" /><property name="hibernate.show_sql" value="true" /><property name="hibernate.format_sql" value="true" /><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/></properties></persistence-unit></persistence>

```

測試代碼

```

//1.創建EntityManagerFactoryString persistenceUnitName = "jpa-1";EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName);//2.創建 EntityManageerEntityManager manager = factory.createEntityManager();//3.開啟事務EntityTransaction traction =  manager.getTransaction();traction.begin();System.out.println(traction);//4.進行持久化操作Customer customer = new Customer();customer.setAge(11);customer.setEmail("abc@abcd.com");customer.setLastName("tom");manager.persist(customer);//5.提交事務traction.commit();//6.關閉EntityManagermanager.close();//7.關閉EntityManagerFactoryfactory.close();

```


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郓城县| 行唐县| 富宁县| 东丽区| 错那县| 普格县| 绵竹市| 龙口市| 奈曼旗| 湘西| 河曲县| 城市| 涞源县| 天津市| 金塔县| 甘孜| 敦化市| 远安县| 衢州市| 襄汾县| 法库县| 明星| 岗巴县| 钟祥市| 河北区| 石泉县| 大悟县| 泾源县| 周宁县| 理塘县| 安泽县| 大新县| 西城区| 永新县| 灌南县| 溧水县| 乌苏市| 安徽省| 永州市| 富宁县| 夹江县|