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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

hibernate對JPA_Annotation的支持實(shí)例講解

2019-11-08 03:14:02
字體:
供稿:網(wǎng)友

        JPA全稱java Persistence API.JPA通過JDK 5.0注解或xml描述對象-關(guān)系表的映射關(guān)系,并將運(yùn)行期的實(shí)體對象持久化到數(shù)據(jù)庫中.JPA是一種規(guī)范,而hibernate是JPA的實(shí)現(xiàn),除了hibernate還有EclipseLink也是JPA的實(shí)現(xiàn).JPA 是 JCP 組織發(fā)布的 Java EE 標(biāo)準(zhǔn)之一,因此任何聲稱符合 JPA 標(biāo)準(zhǔn)的框架都遵循同樣的架構(gòu),提供相同的訪問API,這保證了基于JPA開發(fā)的企業(yè)應(yīng)用能夠經(jīng)過少量的修改就能夠在不同的JPA框架下運(yùn)行。

第一個(gè)hibernate(hibernate annotation)JPA項(xiàng)目

1.建立java項(xiàng)目

2.創(chuàng)建User Library,加入依賴包* HIBERNATE_HOME/lib/*.jar* HIBERNATE_HOME/hibernate3.jar* 加入數(shù)據(jù)庫驅(qū)動(dòng)(MySQL驅(qū)動(dòng))3.加入hibernate annotation支持包 * hibernate-annotations.jar* ejb3-persistence.jar* hibernate-commons-annotations.jar前三步驟參考http://blog.csdn.net/chenxiaochan/article/details/51417499,這篇博客中詳細(xì)講解了如何建立java項(xiàng)目,創(chuàng)建user library,以及加入包.4.提供hibernate.cfg.xml文件,完成基本的配置

<!DOCTYPE hibernate-configuration PUBLIC	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>	<session-factory>		<PRoperty name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_jpa_first</property>		<property name="hibernate.connection.username">root</property>		<property name="hibernate.connection.passWord">bjpowernode</property>		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>		<property name="hibernate.show_sql">true</property>		<!-- 		<property name="hibernate.format_sql">true</property>		 -->			</session-factory></hibernate-configuration>5、建立實(shí)體類User.java,采用注解完成映射

在這里需要說明,在使用@Entity注解的時(shí)候,引用javax.persistence.Entity,這個(gè)是jpa的jar包.@Id使用的注解是主鍵的注解.

package com.bjpowernode.hibernate;import java.util.Date;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class User {	private String id;		private String name;		private String password;		private Date createTime;		private Date expireTime;	@Id	public String getId() {		return id;	}	public void setId(String id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getPassword() {		return password;	}	public void setPassword(String password) {		this.password = password;	}	public Date getCreateTime() {		return createTime;	}	public void setCreateTime(Date createTime) {		this.createTime = createTime;	}	public Date getExpireTime() {		return expireTime;	}	public void setExpireTime(Date expireTime) {		this.expireTime = expireTime;	}}7.在hibernate.cfg.xml文件中添加user的映射.

<mapping class="com.bjpowernode.hibernate.User"/>8.編寫工具類ExoprtDB.java,注解生成ddl,必須采用AnnotationConfiguration類

package com.bjpowernode.hibernate;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * * @author Administrator * */public class ExportDB {	public static void main(String[] args) {				Configuration cfg = new AnnotationConfiguration().configure();				SchemaExport export = new SchemaExport(cfg);		export.create(true, true);	}}9.建立客戶端類Client,添加用戶數(shù)據(jù)到mysql
package com.bjpowernode.hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;public class Client {	public static void main(String[] args) {				//讀取hibernate.cfg.xml文件		Configuration cfg = new AnnotationConfiguration().configure();				//建立SessionFactory		SessionFactory factory = cfg.buildSessionFactory();				//取得session		Session session = null;		try {			session = factory.openSession();			//開啟事務(wù)			session.beginTransaction();			User user = new User();			user.setId("0001");			user.setName("張三");			user.setPassword("123");			user.setCreateTime(new Date());			user.setExpireTime(new Date());						//保存User對象			session.save(user);						//提交事務(wù)			session.getTransaction().commit();		}catch(Exception e) {			e.printStackTrace();			//回滾事務(wù)			session.getTransaction().rollback();		}finally {			if (session != null) {				if (session.isOpen()) {					//關(guān)閉session					session.close();				}			}		}	}}JPA不僅擁有ORM的特點(diǎn),還有自己獨(dú)有的特點(diǎn):

1 .標(biāo)準(zhǔn)化2 .對容器級(jí)特性的支持3 .簡單易用,集成方便4 .可媲美JDBC的查詢能力  JPA的查詢語言是面向?qū)ο蠖敲嫦驍?shù)據(jù)庫的,它以面向?qū)ο蟮淖匀徽Z法構(gòu)造查詢語句,可以看成是hibernate HQL的等價(jià)物。JPA定義了獨(dú)特的JPQL(Java Persistence Query Language),JPQL是EJB QL的一種擴(kuò)展,它是針對實(shí)體的一種查詢語言,操作對象是實(shí)體,而不是關(guān)系數(shù)據(jù)庫的表,而且能夠支持批量更新和修改、JOIN、GROUP BY、HAVING 等通常只有 SQL 才能夠提供的高級(jí)查詢特性,甚至還能夠支持子查詢。5. 支持面向?qū)ο蟮母呒?jí)特性


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 湘潭县| 固阳县| 吴旗县| 北海市| 福建省| 黔江区| 泸溪县| 平山县| 东乡族自治县| 正宁县| 舞钢市| 会昌县| 蛟河市| 禄丰县| 奉化市| 绵阳市| 礼泉县| 宣恩县| 辽阳县| 西安市| 二连浩特市| 肇州县| 文成县| 禹州市| 宝兴县| 两当县| 兴隆县| 永德县| 清水县| 高平市| 高淳县| 临沂市| 三穗县| 桑日县| 怀柔区| 科技| 洪洞县| 昌平区| 民乐县| 五峰| 蕉岭县|