- StudentTest.java
`
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.*;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//创建配置对象
Configuration config =new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会化工厂
sessionFactory = config.buildSessionFactory(serviceRegistry);
//绘画对象
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
}
@After
public void destory(){
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testSaveStudents(){
//生成学生对象
Students s =new Students(1,"pp","男", new Date(),"11A403");
session.save(s);//保存对象进入数据库
}
}
`
-
hibernate.cfg.xml
'<?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">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8
</property><property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- hibernate数据表表前缀 --> <property name="hibernate.default_schema">hibernate</property> <!-- 创建数据库生成策略 create 表示重新生成表结构 update保留原有的表结构--> <property name="hbm2ddl.auto">create</property> <property name="hibernate.current_session_context_class">thread</property> <!-- jta事务 全局事务 <property name="hibernate.current_session_context_class">jta</property> --> <mapping resource="Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
' - Students.hbm.xml
'<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Students" table="STUDENTS">
<id name="sid" type="int">
<column name="SID"/>
<generator class="assigned"/>
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME"/>
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER"/>
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY"/>
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS"/>
</property>
</class>
</hibernate-mapping>'
共同学习,写下你的评论
评论加载中...
作者其他优质文章