hibernate.cfg.xml 配置及注意问题详解

一 :导入的包

hibernate3.jar 核心包

hibernate-3.6.0 Final\hibernate\lib\required 目录下面的所有的包

c3p0-0.9.1.jar  采用c3p0数据源连接的包

hibernate-jpa-2.0-api-1.0.0.Final.jar 

二:创建 一个JOPO对象(一个实体类对象)

public class News {

    private Integer id;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    private String title;
    private String content;
    
}

三:生成News.hbm.xml 映射文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- Generated 2010-11-9 18:15:04 by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="hibernate.News" table="news_table">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="title" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="content" />
        </property>
    </class>
</hibernate-mapping>

注意此处 为了避免和mysql原生态字段同名 需要手动修改部分字段名称

四:配置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>
        <!-- 数据库驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <!--   数据库密码 -->
        <property name="hibernate.connection.password">123456</property>
    <!--     数据库地址 -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
     <!--    数据库用户名 -->
        <property name="hibernate.connection.username">root</property>
<!--         数据库方言 --  注意mysql数据库需要改此处 否则报错 >
         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   <!--       字段显示sql语句 -->
        <property name="show_sql">true</property>  
      <!--   sql语句格式化 -->
         <property name="hibernate.format_sql">true</property>
     <!--     自动创建数据表 -->
        <property name="hbm2ddl.auto">update</property>
    <!--     映射的xml  注意此处生成的News.hbm.xml和实体类在一起 注意此处的路径 包名/类名.hbm.xml-->
        <mapping resource="hibernate/News.hbm.xml"/>
            
      
    </session-factory>
</hibernate-configuration>

 

五:利用框架插入一条数据demo

public static void main(String[] args)
        throws Exception {
        //实例化Configuration,
        Configuration conf = new Configuration()
        //下面方法默认加载hibernate.cfg.xml文件
            .configure();
        //以Configuration创建SessionFactory
        SessionFactory sf = conf.buildSessionFactory();
        //创建Session
        Session sess = sf.openSession();
        //开始事务
        Transaction tx = sess.beginTransaction();
        //创建消息实例
        News n = new News();
        //设置消息标题和消息内容
        n.setTitle("疯狂Java联盟成立了");
        n.setContent("疯狂Java联盟成立了,"
            + "网站地址http://www.crazyit.org");
        //保存消息
        sess.save(n);
        //提交事务
        tx.commit();
        //关闭Session
        sess.close();
        sf.close();
    }


 

转载于:https://my.oschina.net/mclongyi/blog/718740

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值