简述:
使用Hibernate实现一个学生注册功能(仍然使用部分Spring mvc)
步骤:
0. 配置Hibernates数据库连接
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="dialect"><!--Database Dialect--> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"><!-- URL of the database--> jdbc:mysql://localhost:3306/smw </property> <property name="connection.username">root</property><!-- user name --> <property name="connection.password">sql</property><!-- password --> <property name="connection.driver_class"><!--connect driver --> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property><!-- show sql statement --> <mapping resource="smw/model/Student.hbm.xml"/><!-- mapping file --> </session-factory> </hibernate-configuration>
1. 新建Student model, 用于数据库表的映射
package smw.model; public class Student { private int sid; private String name; private String password; private String college; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getCollege() { return college; } public void setCollege(String college) { this.college = college; } }
2. 配置hibernate对于某个bean对象(表单元素)的映射
Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="smw.model.Student" table="tb_student" catalog="smw"> <id name="sid" column="sid" type="int"> <generator class="increment"/> </id> <property name="name" type="java.lang.String"> <column name="name" not-null="true" length="20"> <comment>student's name</comment> </column> </property> <property name="password" type="java.lang.String"> <column name="password" not-null="false" length="10"> <comment>student's password</comment> </column> </property> <property name="college" type="java.lang.String"> <column name="college" not-null="false" length="20"> <comment>student's college</comment> </column> </property> </class> </hibernate-mapping>
3. HibernateUtil类用于管理事务会话
HibernateUtils.java
package smw.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory factory; static{ try{ Configuration cfg = new Configuration().configure(); factory = cfg.buildSessionFactory(); //build Session Factory }catch(Exception e){ System.out.println("static of HibernateUtil: " + e.getMessage()); } } /** * @return SessionFactory */ public static SessionFactory getSessionFactory(){ return factory; } /** * get Session * @return Session */ public static Session getSession(){ return factory.openSession(); } /** * close Session * @param session */ public static void closeSession(Session session){ if(session != null){ if(session.isOpen()){ session.close(); } } } }
4. StudentDAO用于数据库访问接口的实现
StudentDAO.java
package smw.dao; import org.hibernate.Session; import org.hibernate.Transaction; import smw.model.Student; public class StudentDAO { private Session session = null; private Transaction transaction = null; public void saveStudent(Student student){ try { session = HibernateUtils.getSession(); //get Session transaction = session.beginTransaction(); //begin Session session.save(student); //save to DATABASE smw transaction.commit(); //commit transaction } catch (Exception e) { System.out.println("StudentDAO failure !"); transaction.rollback(); //roll back transaction }finally{ HibernateUtils.closeSession(session); //close session } } }
5. 此外还需要实现(spring mvc的跳转)
我使用了一个StudentRegistrationController.java来管理学生表单注册后的跳转,spring mvc的方法不再列出(查spring mvc试用)
最后整个文件夹目录
效果:
注册界面,
注册成功后跳转界面,
数据库呈现,