您的位置:首页 > 其它

总结Hibernate(一)

2016-05-16 19:54 218 查看
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方法

映射普通属性

映射主键
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: