hibernate注解初探
来源:互联网 发布:淘宝补单怎么收费 编辑:程序博客网 时间:2024/06/05 18:40
1.对项目引入注解所需的jar包,本例使用的是hibernate-annotations-3.4.0.,包括注解核心包以及注解必需的包(其它的hibernate所需的jar包已经引入项目,详见hibernate初探,使用的是hibernate-3.3.2)
2.在数据库新建测试表teacher(项目使用的是oracle数据库)
-- Create tablecreate table TEACHER( id NUMBER not null, name VARCHAR2(20), title VARCHAR2(20))tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited );-- Create/Recreate primary, unique and foreign key constraints alter table TEACHER add constraint PPP primary key (ID) using index tablespace USERS pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited );3.新建实体类Teacher,并加入注解@Entity和@Id,分别位于类名上面和主键的get方法上面
package com.baosight.model;import javax.persistence.Entity;import javax.persistence.Id;/** * <p>Title: </p> * <p>Description:Student </p> * <p>Company: </p> * @author yuan * @date 2016-4-10 下午12:32:46*/@Entitypublic class Teacher {private int id;private String name;private String title;@Idpublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}}4.修改hibernate配置文件,加入Teacher的配置信息,本例中为:<mapping class="com.baosight.model.Teacher"/>
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/baosight/model/Student.hbm.xml"/><mapping class="com.baosight.model.Teacher"/> </session-factory></hibernate-configuration>5.新建测试类TestTeacher测试新增数据
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;import com.baosight.model.Student;import com.baosight.model.Teacher;/** * <p>Title: </p> * <p>Description:TestStu </p> * <p>Company: </p> * @author yuan * @date 2016-4-10 下午3:16:01*/public class TestTecher {/** * @Title: main * @Description: TODO * @param args * @return void * @throws */public static void main(String[] args) {// TODO Auto-generated method stub//教师测试类Teacher t = new Teacher();t.setId(1);t.setName("t1");t.setTitle("中级");//读取配置文件Configuration cfg = new AnnotationConfiguration();//得到session工厂SessionFactory sf = cfg.configure().buildSessionFactory();//得到sessionSession session = sf.openSession();//开启事务session.beginTransaction();//session执行savesession.save(t);//事务提交session.getTransaction().commit();//关闭sessionsession.close();//关闭session工厂sf.close();}}6.运行TestTeacher,查询数据库,记录成功添加
结果:1 1 t1 中级
综上所述,注解与常规的方法相比,代码要简洁许多,注解存在于实体类中,不需要再有实体类和对应数据库表的映射文件了,只需在hibernate配置文件中声明一下实体类的映射就行了,另外,注意使用注解读取hibernate配置文件时使用的是AnnotationConfiguration类,而不是普通的Configuration类。
1 0
- hibernate注解初探
- Hibernate初探
- Hibernate初探
- Hibernate初探
- 初探Hibernate
- hibernate初探
- 初探Hibernate
- hibernate初探
- 初探hibernate
- Hibernate初探
- Hibernate-hibernate初探
- java注解初探
- hibernate 注解
- hibernate注解
- hibernate注解
- hibernate注解
- Hibernate注解
- Hibernate 注解
- [PS] 没有图层匹配此滤镜 && 我的图层消失了
- string类的使用
- LeetCode *** 303. Range Sum Query - Immutable
- 【bzoj4430】[Nwerc2015]Guessing Camels赌骆驼
- SDAU 搜索专题 23 Another Eight Puzzle
- hibernate注解初探
- iOS开发 ☞ Quartz2D详解
- 树结构
- 通过第三方平台shareSDK分享链接
- 关于Adapter的notifyDataSetChanged是否生效
- ScrollView与GridView滚动发生冲突
- WSDL UDDI SOAP与在webserver标准中的作用
- [python]使用txt保存和读取列表变量
- 响应式布局初设(二)