hibernate 学习笔记1

  今天开始学习hibernate,碰到的几个问题在这里记录一下。

1>导入hibernate5.2.10的hibernate-release-5.2.10.Final\hibernate-release-5.2.10.Final\lib\required 文件夹下的jar包,及mysql数据库驱动。

 

2>配置hibernate.cfg.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
 9         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&amp;characterEncoding=UTF8</property>
10         <property name="connection.username">root</property>
11         <property name="connection.password"></property>
12         <!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch 
13             Size越小,读数据库的次数越多,速度越慢 -->
14         <property name="jdbc.fetch_size">50 </property>
15         <!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大 -->
16         <property name="jdbc.batch_size">23 </property>
17         <!-- SQL 方言-->
18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 
19         <!-- Enable Hibernate's automatic session context management -->
20         <property name="current_session_context_class">thread</property>
21         <!-- 在控制台输出sql语句 -->
22         <property name="show_sql">true</property>
23         <!-- 在启动时根据配置更新数据库 -->
24         <property name="hbm2ddl.auto">update</property>
25         <mapping resource="com/cnblogs/hibernate_first/User.hbm.xml" /><!-- 注册我们的实体映射类 -->
26     </session-factory>
27 </hibernate-configuration>

以上配置需要注意几个问题:

a建立数据库的时候一定要指定数据库编码,如:create database hibernate_first DEFAULT CHARACTER SET UTF8及数据库url:

<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&amp;characterEncoding=UTF8</property> 否则可能产生持久化的时候出现的乱码情况
如:

b <mapping resource="com/cnblogs/hibernate_first/User.hbm.xml" /><!-- 注册我们的实体映射类 --> 注册实体映射类注意带包名,其中“.”用“/”代替。

c如果报错前言中不允许有内容,可能是在xml文件的开头有不必要的空格,记得检查。

备注:因为是刚刚学习hibernate 配置中的很多property 可以查阅相关资料,以上配置没有过多列出。

3>建立实体类及配置映射文件,因为是刚刚学习,所以配置的很简单,后续进一步学习将进行整改。

package com.cnblogs.hibernate_first;

import java.util.Date;

public class User {
    private String id;
    private String name;
    private String password;
    private Date createDate;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    

}
<?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" >

<hibernate-mapping package="com.cnblogs.hibernate_first">
    <class name="User" table="user">
        <id name="id" column="id">
            <generator class="uuid"></generator>
        </id>
        <property name="name" column="name" />
        <property name="password" column="password" />
        <property name="createDate" column="createDate" />
    </class>
</hibernate-mapping>

以上配置需要注意以下问题:

a <hibernate-mapping package="com.cnblogs.hibernate_first"> 记得指定包名。要不报错找不到映射文件。

4>测试

 1 public class Test1 {
 2     public static void main(String[] args) {
 3         //读取配置文件
 4         Configuration cfg = new Configuration().configure();
 5         //建立SessionFactory
 6         SessionFactory factory = cfg.buildSessionFactory();
 7         Session session = null;
 8         Transaction transation = null;
 9         try {
10         //打开Session
11         session = factory.openSession();
12         //开启事务
13         transation = session.beginTransaction();
14         User user = new User();
15         user.setName("王五");
16         user.setPassword("123");
17         user.setCreateDate(new Date());
18         session.save(user);
19         //提交事务
20         transation.commit();
21         } catch (Exception e) {
22             e.printStackTrace();
23             //回滚
24             transation.rollback();
25         }finally{
26             //关闭Session
27             if(session != null){
28                 if(session.isOpen()){
29                     session.close();
30                 }
31             }
32         }
33     }
34 }

提交结果如下图:

 

 

 

 

 

 
 

 

转载于:https://www.cnblogs.com/Juli/p/7685573.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值