温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

hibernate第一个例子-保存对象

发布时间:2020-07-29 01:07:42 来源:网络 阅读:293 作者:yayaAA 栏目:数据库

3.hibernate第一个例子-保存对象

使用hibernate框架需要导入的jar包:

antlr-2.7.6

backport-util-concurrent

c3p0-0.9.1

commons-collections-3.1    apache集合帮助的包

commons-logging-1.1.1日志

dom4j-1.6.1解析XML

ehcache-1.5.0缓存框架

hibernate3hibernate核心包

javassist-3.9.0.GA代理模式工具包,解决懒加载问题

jta-1.1

log4j日志

mysql-connector-java-5.1.10-bin数据库连接

slf4j-api-1.5.8

slf4j-log4j12


Person-持久化类

/**
 * 对象的序列化的作用:让对象在网络上传输,以二进制的形式传输
 * Serializable标示接口
 */
public class Person implements Serializable{
    private Long pid;
    private String pname;
    private String psex;
    set,get方法
}


映射文件 Person.hbm.xml

<?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>
    <!-- 
        用来描述一个持久化类
        name  类的全名
         table 对应的表名,可以不写  默认值和类名一样 
         catalog  数据库的名称  一般不写
     -->
    <class name="cn.itcat.domain.Person">
        <!-- 
            标示属性  和数据库中的主键对应
            name  属性的名称
            column 列的名称
            length 表中字段长度
         -->
        <id name="pid" column="pid" length="200" type="java.lang.Long">
            <!-- 
                主键的产生器
              就该告诉hibernate容器用什么样的方式产生主键-->
            <generator class="increment"></generator>
        </id>
        <!-- 描述一般属性 -->
        <property name="pname" column="pname" length="20" type="string"></property>
        <property name="psex" column="psex" length="10" type="java.lang.String"></property>
    </class>
</hibernate-mapping>


配置文件 hibernate.cfg.xml

<?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只能连接一个数据库-->
    <session-factory>
        <!--数据库的用户名-->
        <property name="connection.username">root</property>
        <!--密码-->
        <property name="connection.password">root</property>
        <!-- url-->
        <property name="connection.url">
            jdbc:mysql://localhost:3306/hibernateDay01
        </property>
            <!-- 
                "hbm2ddl.auto"作用:根据持久化类和映射文件生成表
                validate 只校验
                create-drop启动hibernate创建,关闭hibernate销毁表
                create 启动hibernate容器生成表
                update 启动hibernate容器时检查持久化类和表对不对应,如果不对应,创建表,对应的话,验证。
            -->
        <property name="hbm2ddl.auto">update</property>
            <!-- 
                显示hibernate内部生成的sql语句
            -->
        <property name="show_sql">true</property>
        <mapping resource="cn/itcat/domain/Person.hbm.xml" />
 
    </session-factory>
</hibernate-configuration>

测试

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
 
public class PersonTest {
    @Test
    public void testCreateTable(){ //测试数据库中会不会根据持久化类和映射文件生成表
        Configuration configuration = new Configuration();
        configuration.configure();  //加载配置文件1、该配置文件必须放在classpath下2、名称必须为hibernate.cfg.xml
        configuration.buildSessionFactory();
        }
    @Test
    public void testSavePerson(){
        Configuration configuration = new Configuration();
        configuration.configure();  
        SessionFactory factory = configuration.buildSessionFactory();
        
        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();
        
        Person person = new Person();
        person.setPname("班长2");
        person.setPsex("女");
        
        /**
         * 参数必须持久化对象
         */
        session.save(person);
        
        transaction.commit();
        session.close();
    }
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI