/**
* 创建Classes实体类
* @author jianglei
*
*/
public class Classes implements Serializable{
private int classes_id;
private String classes_name;
private String teacher;
private String create_date;
public int getClasses_id() {
return classes_id;
}
public void setClasses_id(int classesId) {
classes_id = classesId;
}
public String getClasses_name() {
return classes_name;
}
public void setClasses_name(String classesName) {
classes_name = classesName;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getCreate_date() {
return create_date;
}
public void setCreate_date(String createDate) {
create_date = createDate;
}
}
//hibernate.cfg.xml配置文件
<!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="show_sql">true</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databasename=Hibernate
</property>
<property name="connection.username">sa</property>
<property name="connection.password">19920521</property>
<mapping resource="Classes.hbm.xml" />
</session-factory>
</hibernate-configuration>
Classes.hbm.xml配置文件:
<?xml version="1.0"?>
<!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.xtkj.hibernate.entity.Classes" table="classes">
<id name="classes_id" type="java.lang.Integer">
<column name="classes_id"></column>
<generator class="native"></generator>
</id>
<property name="classes_name" type="java.lang.String">
<column name="classes_name" length="20"></column>
</property>
<property name="teacher" type="java.lang.String">
<column name="teacher" length="20"></column>
</property>
<property name="create_date" type="java.lang.String">
<column name="create_date" length="20"></column>
</property>
</class>
</hibernate-mapping>
private Session session = null;
@Before
public void setUp() throws Exception{
//获得hibernate配置管理接口
//根据classpath中默认配置文件hibernate.cfg.xml初始化Configuration实例
Configuration config = new Configuration().configure();
//根据Configuration实例创建session的工厂类sessionfactory
SessionFactory factory = config.buildSessionFactory();
//通过session工厂获得实例
session = factory.openSession();
}
//调用save方法增加班级
public void saveClasses() throws Exception{
Transaction tr = session.beginTransaction();
Classes classes = new Classes();
classes.setClasses_name("G302");
classes.setTeacher("李老师");
classes.setCreate_date("1998-09-12");
session.save(classes);
tr.commit();
}
@Test
//修改班级信息
public void updateClasses() throws Exception{
Classes classes = (Classes) session.get(Classes.class, new Integer(1));
System.out.println("classesId:"+classes.getClasses_id());
System.out.println("classesName:"+classes.getClasses_name());
System.out.println("teacher:"+classes.getTeacher());
System.out.println("createDate:"+classes.getCreate_date());
Transaction tr = session.beginTransaction();
classes.setCreate_date("1998-09-13");
tr.commit();
}
@After
public void tearDown() throws Exception{
//关闭session
session.close();
}