1. Importing the JAR Package
2. Write the appropriate configuration file Hibernate.cfg.xml
(Database driver, password, user name, dialect, SQL statement, etc.)
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Connection.url" >jdbc:mysql://localhost:3306/company
?useunicode=true&characterencoding=utf-8</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >root</property>
<property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "dialect" >org.hibernate.dialect.MySQLDialect</property>
<property name= "Show_sql" >true</property>
<property name= "Format_sql" >true</property>
<mapping resource= "Cn/hibernate/bean/detp.hbm.xml"/>
</session-factory>
3. Writing the Detp.hbm.xml configuration file
(Name is a property of the entity class, column is a field of the database table, type is the database and entity class types, and the database field type must be the same as the entity type)
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<class name= "Cn.hibernate.bean.Detp" table= "DETP" >
<id name= "DeptNo" column= "DeptNo" >
<!--primary key generation policy identity: Identity column assigned: program native: Local
-
<generator class= "Identity" ></generator>
</id>
<property name= "Deptname" column= "Deptname" type= "java.lang.String" ></property>
<property name= "deptlocation" column= "deptlocation" type= "java.lang.String" ></property>
</class>
4. Writing the interface Detpdao
Package Cn.hibernate.dao;
Import java.util.List;
Import CN.HIBERNATE.BEAN.DETP;
Public interface Detpdao {
/** * Add DETP table information * @param DETP * @return * *
public int Adddetp (DETP detp);
/** * Update information for DETP table * @param DETP * @return * *
public int Updatedetp (DETP detp);
/** * Delete Information for DETP table * @param DETP * @return * *
public int Deteledetp (DETP detp);
/** * Search All information * @return * *
Public list<detp> Findetps ();
Implementation class Detpdaoimpl of 5.DetpDao
Import java.util.ArrayList; Import java.util.List;
Import Org.hibernate.Query; Import org.hibernate.Session; Import org.hibernate.Transaction;
Import CN.HIBERNATE.BEAN.DETP; Import Cn.hibernate.dao.DetpDao; Import Cn.hibernate.dao.HibernateUtils;
public class Detpdaoimpl implements Detpdao {private session session; Private Transaction Transaction;
@Override public int adddetp (Detp detp) { int num=0; try { session=hibernateutils.getsession (); transaction = Session.begintransaction (); session.save (DETP); transaction.commit (); num=1; } catch (Exception e) { e.printstacktrace (); Transaction.rollback (); Num=-1; } finally{ Hibernateutils.closesession (); } return num; }
@Override public int updatedetp (Detp detp) { int num = 0; try { s Ession = Hibernateutils.getsession (); transaction = Session.begintransaction (); session.update (DETP); transaction.commit (); num = 1; } catch (Exception e) { e.printstacktrace (); transaction.rollback (); num =-1; }finally{ hibernateutils.closesession (); } return num; }
@Override public int deteledetp (Detp detp) { int num = 0; try { s Ession = Hibernateutils.getsession (); transaction = Session.begintransaction (); session.delete (DETP); transaction.commit (); num = 1; } catch (Exception e) { e.printstacktrace (); transaction.rollback (); num =-1; }finally{ hibernateutils.closesession (); } return num; }
@Override public list<detp> Findetps () {list<detp> dlist=new arraylist<detp> (); String hql= "from Detp"; try {session=hibernateutils.getsession (); Query query=session.createquery (HQL); Dlist =query.list (); } catch (Exception e) {e.printstacktrace (); } finally{hibernateutils.closesession (); } for (Detp detp:dlist) {System.out.println ("Number:" +detp.getdeptno () + "\t\t Department:" +detp.getdeptname () + "\t\t Department location:" +DETP.G Etdeptlocation ()); } return dlist; }
6. Writing Test Classes
public class Testdeom {Detpdao DAO = new Detpdaoimpl ();
@Test public void Tupdatedeom () {DETP detp = new Detp (); Detp.setdeptno (6); Detp.setdeptname ("Ministry of Manpower 001"); Detp.setdeptlocation ("100"); int num = DAO.UPDATEDETP (DETP); SYSTEM.OUT.PRINTLN (num); } @Test public void Tfinddeom () {list<detp> detps= Dao.findetps (); System.out.println (Detps.size ()); } @Test public void Tadddeom () {DETP detp=new detp (); Detp.setdeptno (11); Detp.setdeptname ("Ministry of Foreign Affairs 01"); Detp.setdeptlocation ("102"); DAO.ADDDETP (DETP); } @Test public void Tdeldeom () {DETP detp=new detp (); Detp.setdeptno (5); int num= DAO.DETELEDETP (DETP); SYSTEM.OUT.PRINTLN (num); }
Hibernate additions and deletions change