/Hibernate_0100_HelloWorld
/Hibernate_0100_HelloWorld:
一、src下面两个package:
1、default package里面:创建了并初始化Student对象;通过Configuration、SessionFactory、openSession方法来获得session,从而来调用session的方法。而让我疑惑的是为何在使用session方法时,调用了sesion.beginTransaction。
1 Student s = new Student(); 2 3 s.setId(1); 4 s.setName("s1"); 5 s.setAge(24); 6 7 Configuration cfg = new Configuration(); 8 SessionFactory sf = cfg.configure().buildSessionFactory(); 9 Session session = sf.openSession(); 10 session.beginTransaction(); 11 session.save(s); 12 session.getTransaction().commit(); 13 session.close(); 14 sf.close();
2、com.xxc.hibernate.model里面:
编写了Student.java和Student.hbm.xml。Student.java实现domain model;Student.hbm.xml实现hibernate-mapping,既然是mapping,那么肯定会同时有类属性和table表项
1 <hibernate-mapping package="com.xxc.hibernate.model"> 2 3 <class name="Student" table="student"> 4 5 <id name="id"></id> 6 <property name = "name"></property> 7 <property name = "age"></property> 8 9 </class> 10 11 </hibernate-mapping>
3、hibernate.cfg.xml
1 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 2 <property name="connection.url">jdbc:mysql://localhost/hibernate</property> 3 <property name="connection.username">root</property> 4 <property name="connection.password">32147</property> 5 6 7 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 8 9 <mapping resource="com/xxc/hibernate/model/Student.hbm.xml"/>
注意dialect中MySQLDialect,而不是MYSQLDialect