hibernate------->第一个程序
今天学习hibernate 。
创建User类:
package com.hibernate; import java.util.Date; public class User { private Integer id; private String name; private Date birth; public User(){} public User(Integer id, String name, Date birth) { super(); this.id = id; this.name = name; this.birth = birth; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]"; } }
写User类的hiebernate
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hibernate"> <!-- 映射表和实体 --> <class name="User" table="t_user"> <!-- 字段(column)和属性(name) 属性名和字段名一致的时候,可以省略--> <id name="id"> <!-- 主键生成器 --> <generator class="increment"></generator> </id> <property name="name"></property> <property name="birth"></property> </class> </hibernate-mapping>
配置hibernate的映射:
<?xml version="1.0" encoding="utf-8"?> <!-- dtd:document type definition 标签,标签中的属性,标签的层级,标签的先后顺序 --> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置信息 --> <!-- 数据库连接参数 --> <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.1.105:1521:orcl</property> <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hibernate.connection.username">ems</property> <property name="hibernate.connection.password">123456</property> <!-- 自身配置信息 dialect:方言 指示数据库的厂商. :hibernate在进行底层的数据库操作时,会针对不同的数据库特点,做出相应的适应. --> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 显示 并 格式化 hibernate底层执行的sql语句 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property>
<!--在数据库中自动创建表--> <property name="hbm2ddl.auto">update</property> <!-- 映射文件的注册 --> <mapping resource="com/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
写测试类:
package com.hibernate; import java.util.Date; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; public class Test { public static void main(String[] args) { //加载配置 Configuration config=new Configuration().configure(); //用加载配置连接session工厂 SessionFactory factory =config.buildSessionFactory(); //利用session工厂创建session Session session=factory.openSession(); //通过session获取事务 Transaction transaction=session.beginTransaction(); User u=new User(null,"heluwei",new Date()); session.save(u); transaction.commit(); session.close(); factory.close(); } }
小知识:
getCurrentSession()和openSession()
用getCurrentSession()需要在xml文件中配置
<!-- 即可通过getCurrentSession 获取线程唯一的session --> <property name="current_session_context_class">thread</property> <!-- 禁用了javaEE6的bean-validate --> <property name="javax.persistence.validation.mode">none</property>
openSession每次调用都获取新的session对象。但是getCurrentSession在同一线程中多次调用,获取的是同一个对象。
利用注解的方式:
在xml配置文件中需要加上需要注解的 类:
<mapping class="com.hibernate.User"/>
实体:
package com.hibernate; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.GenericGenerator; @Entity//这是一个参与ORM映射的实体 //参与ORM的表,及表名,如果表名和实体名一致的话,则此注解可以省略. //则hibernate会用实体名作为表名 @Table(name="t_user") public class User { //@Id必需注解 @Id//此属性要映射主键列 //如果属性名和列名一致,则@Column可以省略 @Column(name="id")//此属性映射的列名为id @GenericGenerator(name="inc47",strategy="increment") @GeneratedValue(generator="inc47") private Integer id; //@Column(name="name") private String name; //@Column(name="birth") //Date==只保存日期 //Time==只保存时间 //TimeStamp==保存日期和时间 (默认) @Temporal(TemporalType.DATE) private Date birth; public User(){} public User(Integer id, String name, Date birth) { super(); this.id = id; this.name = name; this.birth = birth; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]"; } }