第一步:创建数据库链接
切换到Database 视图
第二步:打开mysql
备注:设置mysql为手动启动,使用时打开,不使用时关闭。
第三步:打开mysql的管理工具sqlyog
第四步:eclipse中创建mysql链接
第五步:添加hibernate属性
新建工程,添加hibernate属性
方法一
方法二
添加完毕后视图
第六步Hibernate 映射
切换到hibernate视图,启动先前创建的链接
映射后生成的代码
第七步:生成类解释
***.DAO用来对数据库进行操作
例如:
StudentDAO dao=new StudentDAO();通过dao对象来调用方法。
POJO类用来存储数据
Student student=new Student();
第八步:DAO测试
public class Main {
public static void main(String[] args) {
StudentDAO dao = new StudentDAO();
dao用来对数据库进行操作
Student student = new Student();
用来保存记录
Team team = new Team();
team.setName("teamnametest1");
student.setAddress("西安市");
student.setAge(new Integer(23));
student.setName("刘建建");
student.setPhone("1234435454");
student.setTeam(team);// 关联
Transaction t = HibernateSessionFactory.getSession().getTransaction();
获取事务
t.begin();
dao.save(student);
t.commit();
}
}
切换到Hibernate视图,然后做映射,注意映射的时候有几个表就一起映射过来!映射好之后一对多的时候要添加这个属性:caseade="save-update"(使用集联) lazy="ture"(当使用的时候才加载)inuserse="ture"(是否要关联),多对一的时候:cascade="all"。
注:多对一表的<set>(<one-to-many)中添加的"cascade="save-update" lazy="true""的意思分别是“级联=在更新的时候”、“延时加载”;而在一对多的....hbm.hml中也有相对应的修改“cascade="all"”每一个和这个相关联的表都要改写,在 “ <many-to-one”处添加,注意每个表的对应。