这周在学习Hibernate。用的的Hibernate3.2的Jar包。配置挺繁琐的,在这里把配置贴出来。
一、倒入Hibernate中的Jar包
二、在src下拷入log4j.properties //这是日志文件
在src下创建并配置hibernate.cfg.xml文件
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:2222/test</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> //设置数据库方也,就是使用的数据库类型
<property name="show_sql">true</property>//在Console中显示sql语句
<property name="format_sql">false</property>
<mapping resource="com/lubby/pojo/Student.hbm.xml"/> //这里是【pojo】.hbm.xml的路径
<mapping resource="com/lubby/pojo/ClassBean.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三、新建pojo并配置【pojo】.hbm.xml文件
<hibernate-mapping>
<class name="com.lubby.pojo.Student" table="t_student">
<id name="sid" >
<generator class="assigned"></generator>
</id>
<property name="sname"></property>
<property name="address"></property>
</class>
</hibernate-mapping>
四、使用Hibernate对数据库进行操作
4.1新建表
Configuration cfg = new Configuration().configure(); //加载pojo的配置文件
SchemaExport se = new SchemaExport(cfg);//这里是新建数据库表 如果有就删除后新建
se.create(true, true);
4.2保存数据库对象
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
Student stu1 = new Student(1, "小红", "合肥");
session.beginTransaction().begin();
session.save(stu1);
session.beginTransaction().commit(); //默认是事务的的处理方式 ,所以一定要提交
4.3删除数据库对象
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
session.beginTransaction().begin();
Student stu1 = (Student )session.load(Student.class, 1); //通过类名和主键得到数据库中的值
session.del(stu1);
session.beginTransaction().commit(); //默认是事务的的处理方式 ,所以一定要提交
4.4得到数据库所有对象的List
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
session.beginTransaction().begin();
List<Student> list = session.createQuery("from Student").list(); //这里返回的是list
session.beginTransaction().commit(); //默认是事务的的处理方式 ,所以一定要提交
4.5 get和load的区别 get是立即就到数据库中执行,而load是需要的时候再执行,老师提了一下代理机制,反正我是不懂
五、Hibernate中的o2o(一对一的关系)
六、Hibernate中的o2m(一对多的关系)
七、Hibernate中的m2m2(多对多的关系)
一、倒入Hibernate中的Jar包
二、在src下拷入log4j.properties //这是日志文件
在src下创建并配置hibernate.cfg.xml文件
hibernate.cfg.xml 的基本配置
<hibernate-configuration><session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:2222/test</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> //设置数据库方也,就是使用的数据库类型
<property name="show_sql">true</property>//在Console中显示sql语句
<property name="format_sql">false</property>
<mapping resource="com/lubby/pojo/Student.hbm.xml"/> //这里是【pojo】.hbm.xml的路径
<mapping resource="com/lubby/pojo/ClassBean.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三、新建pojo并配置【pojo】.hbm.xml文件
<hibernate-mapping>
<class name="com.lubby.pojo.Student" table="t_student">
<id name="sid" >
<generator class="assigned"></generator>
</id>
<property name="sname"></property>
<property name="address"></property>
</class>
</hibernate-mapping>
四、使用Hibernate对数据库进行操作
4.1新建表
Configuration cfg = new Configuration().configure(); //加载pojo的配置文件
SchemaExport se = new SchemaExport(cfg);//这里是新建数据库表 如果有就删除后新建
se.create(true, true);
4.2保存数据库对象
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
Student stu1 = new Student(1, "小红", "合肥");
session.beginTransaction().begin();
session.save(stu1);
session.beginTransaction().commit(); //默认是事务的的处理方式 ,所以一定要提交
4.3删除数据库对象
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
session.beginTransaction().begin();
Student stu1 = (Student )session.load(Student.class, 1); //通过类名和主键得到数据库中的值
session.del(stu1);
session.beginTransaction().commit(); //默认是事务的的处理方式 ,所以一定要提交
4.4得到数据库所有对象的List
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
session = sf.openSession();
session.beginTransaction().begin();
List<Student> list = session.createQuery("from Student").list(); //这里返回的是list
session.beginTransaction().commit(); //默认是事务的的处理方式 ,所以一定要提交
4.5 get和load的区别 get是立即就到数据库中执行,而load是需要的时候再执行,老师提了一下代理机制,反正我是不懂
五、Hibernate中的o2o(一对一的关系)
六、Hibernate中的o2m(一对多的关系)
七、Hibernate中的m2m2(多对多的关系)