hibernate用注解的方式实现orm
hibernate 有两种方式实现把一张表映射成一个对象,一种是配置文件的方式,一种是注解的方式。这里用hibernate提供的注解的方式实现一个对象和一张表之间的对应。
思路:
首先在hibernate.cfg.xml文件中配置如下内容:数据库,方言,是否显示sql,加载映射类:注意这个hibernate.cfg.xml位置在src下。
因为new Configuration().configure()。这个configure()函数打开源码默认的hiberante.cfg.xml就在src下。
hibernate.cfg.xml的配置的代码如下所示:
1 <?xml version='1.0' encoding='utf-8'?> 2 <!-- 3 ~ Hibernate, Relational Persistence for Idiomatic Java 4 ~ 5 ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 6 ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 7 --> 8 <!DOCTYPE hibernate-configuration PUBLIC 9 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 10 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 11 12 <hibernate-configuration> 13 14 <session-factory> 15 16 <!-- Database connection settings --> 17 <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> 18 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 19 <property name="connection.username">scott</property> 20 <property name="connection.password">a123456</property> 21 22 <!-- JDBC connection pool (use the built-in) --> 23 <property name="connection.pool_size">10</property> 24 25 <!-- SQL dialect --> 26 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 27 <!-- Echo all executed SQL to stdout --> 28 <property name="show_sql">true</property> 29 30 <!-- Drop and re-create the database schema on startup --> 31 <!-- <property name="hbm2ddl.auto">create</property>--> 32 33 <!-- Names the annotated entity class --> 34 <mapping class="com.qls.domain.DiaoChan"/> 35 </session-factory> 36 37 </hibernate-configuration>
然后再oracle数据库中创建diaoChan这张表:
sql语句如下:
1 create table diaoChan( 2 id number(2) primary key not null, 3 name varchar2(30) , 4 height number(2) 5 );
然后再oracle数据库创建一个序列,用于主键生成策略用的。
sql语句如下:
1 create sequence sixi start with 1 increment by 1;
写一个hibernate的domain对象:
代码如下:
package com.qls.domain; import javax.persistence.*; /** * Created by 秦林森 on 2017/5/20. */ @Entity @Table(name = "diaoChan") public class DiaoChan { private int id; private String name; private int height; @Id @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "people") //其中上行的generator中的内容必须和下面这行的name表示一致。 @SequenceGenerator(name = "people",sequenceName = "sixi",initialValue = 1,allocationSize = 1) public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name ="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name = "height") public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } }
然后写一个Test类主要是向表中插入一条数据:
代码如下:
1 package com.qls.test; 2 3 import com.qls.domain.DiaoChan; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.Transaction; 7 import org.hibernate.cfg.Configuration; 8 9 /** 10 * Created by 秦林森 on 2017/5/20. 11 */ 12 public class Test2 { 13 public static void main(String[] args) { 14 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 15 Session session = sessionFactory.openSession(); 16 DiaoChan diaoChan = new DiaoChan(); 17 diaoChan.setName("崇祯"); 18 diaoChan.setHeight(10); 19 Transaction tx = session.beginTransaction();//开启事务。 20 session.save(diaoChan); 21 tx.commit();//提交事务。 22 session.close();//关闭会话。 23 } 24 }
运行Test2之后就会在diaoChan这张表中插入一条数据:
查询结果如下所示:
1 SQL> select *from diaochan; 2 3 ID NAME HEIGHT 4 --- ------------------------------ ------ 5 3 貂蝉 12 6 4 崇祯 10
上面的结果是因为我运行了两次的原因。