第一个hibernate程序

(1)创建一个实体类User
(2)将hibernate所需jar包构建项目中

(3)配置文件
hibernate.cfg.xml      
hibernate.properties
驱动、url、用户名、密码、方言、自动创建表格、将hibernate工作过程中产生的sql语句输出
以及映射文件的位置
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///demo</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/hbsi/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>


(4)映射文件---
User----   User.hbm.xml   
<hibernate-mapping
package="com.hbsi.domain">    -----指定Java实体类的包名
<class name="User" table="user">   -----说明实体类怎样映射成表
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="birthday"/>
</class>
</hibernate-mapping>


(5)写Java代码
Configuration cfg=new Configuration();
cfg.configure();//完成hibernate的初始化--读取配置文件
//SessionFactory对象----DriverManager
SessionFactory sf=cfg.buildSessionFactory();


//得Session对象
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
User user=new User();
user.setName("Tom");
user.setBirthday(new Date());

s.save(user);
tx.commit();
s.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值