第一个hibernate程序(xml版本)

1. 在使用hibernate3.3 的时候使用的包的配置情况:
a.[b] [color=orange]hibernate3.jar[/color][/b] ---这是hibernate的核心包。
b.在lib文件夹中的required包中所用的jar包:
[img]http://dl.iteye.com/upload/attachment/0080/7659/9007385e-a97f-3b17-82bf-bc9181d908a2.png[/img]

c. 关于日志的包,我们需要slf4j的日志包,需要注意的是需要匹配上面[color=orange][b]slf4j-api-1.5.8.jar[/b][/color]中后面的版本包1.5.8,以及slf4j-nop-1.5.8.jar这个包。

最后,使用xml方式的hibernate的jar包就完成了,如下图:
[img]http://dl.iteye.com/upload/attachment/0080/7661/4364aa30-bde8-3ef7-985f-79a6d69f54ae.png[/img]

2. 在对hibernate连接数据库时,我们需要给他配置一个关于数据库的配置文件(hibernate.cfg.xml,此文件的文件名为默认的,如果需要用其他的文件名,需要在程序里面添加相应的文件名参数,在这里就不详细些了。要注意的是位置要放在src目录下):
<?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/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">chihonglu</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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>

<!--xml版本 导入实体类下面的实体类配置中将会提到Student.hbm.xml-->
<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
</session-factory>

</hibernate-configuration>



3. 在配置完数据库连接过后,我们将会配置实体类,如下(一般我们都会把实体类的配置文件放在实体类那个包下面),如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student">
<id name="id" />
<property name="name" />
<property name="age" />
</class>

</hibernate-mapping>


4. 这一步我们就需要开始进行编码了,我们在编码的时候就需要保存一个student对象到数据库中, 请看下面的代码:
public class Test {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("zhangsan");
s.setAge(8);

SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}


这样,我们就成功的完成了第一个hibernate程序。以后的大部分都是一样的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值