为了账号安全,请及时绑定邮箱和手机立即绑定

hibernate-001 源代码

标签:
Java
  • 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>'

点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消