总结Hibernate(一)

来源:互联网 发布:花千骨御剑进阶数据 编辑:程序博客网 时间:2024/05/24 06:18

Hibernate:持久层框架

添加环境:

jar包

配置文件:

主配置:hibernate.cfg.xml

映射配置:xxx.hbm.xml

使用Hibernate实现CRUD操作:

//准备:

Configuration cfg=new Configuration().configure();

SessionFactory sessionFactory=cfg.buildSessionFactory();

//模板代码:

Session session=sessionFactory.openSession();

Transaction tx=null;

try{

tx=session.openTransaction();

//操作

tx.commit();

}catch(Exception e){

tx.rollback();

}finally{

session.close();

}

//操作

Session中的方法:

save(Object)

update(Object)

delete(Object)

get(Class,id)

createQuery(hql)

主配置文件:

数据库信息

方言、驱动、url、username、password

导入配置文件

<mapping resource=" "/>

其他配置

show_sql=true

hbm2ddl.auto=update

映射配置:

映射基础:

1.要有无参的构造方法

2.要有主键

3.要有get、set方法

映射普通属性

映射主键


0 0
原创粉丝点击