配置
<?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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student_complex_quality_evaluation_system?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<mapping resource="com/ynnu/scqes/hibernate/lecture.hibernate.hbm.xml"/>
<mapping resource="com/ynnu/scqes/hibernate/technology.hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Bean
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ynnu.scqes.bean;
/**
*
* @author Pan
*/
public class LectureBean {
protected int lec_id;
protected int stu_id;
protected String years;
protected String lec_name;
protected String lec_level;
protected int lec_fraction;
public int getLec_id() {
return lec_id;
}
public void setLec_id(int lec_id) {
this.lec_id = lec_id;
}
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getYears() {
return years;
}
public void setYears(String years) {
this.years = years;
}
public String getLec_name() {
return lec_name;
}
public void setLec_name(String lec_name) {
this.lec_name = lec_name;
}
public String getLec_level() {
return lec_level;
}
public void setLec_level(String lec_level) {
this.lec_level = lec_level;
}
public int getLec_fraction() {
return lec_fraction;
}
public void setLec_fraction(int lec_fraction) {
this.lec_fraction = lec_fraction;
}
}
映射
<?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="com.ynnu.scqes.bean.LectureBean" table="lecture">
<id name="lec_id">
<generator class="assigned" />
</id>
<property name="stu_id" />
<property name="years" />
<property name="lec_name" />
<property name="lec_level" />
<property name="lec_fraction" />
</class>
</hibernate-mapping>
HibernateUitl
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ynnu.scqes.hibernate;
import com.ynnu.scqes.bean.LectureBean;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author Pan
*/
public class LectureHibernateUtil {
private static final SessionFactory sessionFactory;
private static Session session = null;
private static Transaction transaction = null;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (HibernateException ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static boolean Lecture(LectureBean Database, int Type) {
boolean result = true;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
switch (Type) {
case 1:
session.save(Database);
break;
case 2:
session.update(Database);
break;
case 3:
session.delete(Database);
break;
default:
break;
}
transaction.commit();
} catch (HibernateException ex) {
transaction.rollback();
result = false;
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
} finally {
session.close();
}
return result;
}
public static List ShowLecture(LectureBean Database) {
List<LectureBean> result = null;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
result = session.createQuery("from LectureBean").list();
transaction.commit();
} catch (HibernateException ex) {
transaction.rollback();
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
} finally {
session.close();
}
return result;
}
}