hibenrate(二)HelloWorld

[b]1、建立一个名为hibernate的数据库,一个student表,有id,name,age字段. [/b]
[b]2、Student.java[/b]
private int id;
private String name;
private int age;
public 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


[b]3、Student.hbm.xml映射文件(和Student类放在同一个文件下)[/b]
<?xml version="1.0"?>   
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.fengpeng.model">
<class name="Student">
<id name="id"/>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>

4、hibernate.cfg.xml(核心配置文件)
<?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">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">rootroot</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect --> //mysql数据语言
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- <property name="current_session_context_class"> org.hibernate.context.ManagedSessionContext </property>Enable Hibernate's current session context -->
<!-- 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> <!-- <property name="hbm2ddl.auto">create</property>Drop and re-create the database schema on startup -->
<mapping resource="com/fengpeng/model/Student.hbm.xml"/>
//xml配置文件方式
<mapping class="com.bjsxt.hibernate.Teacher"/>
//JPA(Annotation版本)
</session-factory>
</hibernate-configuration>

[b]5、Teacher.java[/b]
@Entity 		//映射成表
public class Teacher {
private int id;
private String name;
private String title;
@Id //映射成主键,也可以定义在定义的时候
public 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 Title getTitle() {
return title;
}

public void setTitle(Title title) {
this.title = title;
}

[b]6、StudentTest.java[/b]
public static void main(String[] args) {
Student student = new Student();
student.setId(1);
student.setName("你好");
student.setAge(15);
[color=red]Configuration cfg = new Configuration().configure();[/color] //XML方式取得
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
session.close();
sf.close();

}

[b]7、TeacherTest.java[/b]
public static void main(String[] args) {
Teacher t = new Teacher();
t.setId(2);
t.setName("MMMM");
t.setTitle("本科");
[color=red] Configuration cfg = new AnnotationConfiguration().configure(); //Annotation注解取得[/color]
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}


[color=red]
*注:输入@时不提示时:windows-preference-content assist设置即可[/color]

[img]http://dl.iteye.com/upload/attachment/210065/de998b3d-a3f5-349d-bb7c-e705c0eb79c3.bmp[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值