Hibernate初印象(helloworld)-1

这里写图片描述
参考文档:
http://docs.jboss.org/hibernate/orm/4.2/quickstart/en-US/html_single/#d5e57

JPA与Hibernate、ibatis等的关系有点类似于JDBC和mysqlDriver,oracleDriver的关系

JPA定义了一套标准的接口规范,Hibernate等框架去按照这套规范实现,Hibernate的作者也参与了JPA规范的定义。

mysql中创建表student

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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate?serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) 一般用jndi-->
        <!--<property name="connection.pool_size">1</property>-->

        <!-- SQL dialect 方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management 暂时注释掉  3.2以后-->
        <!--<property name="current_session_context_class">thread</property>-->

        <!-- Disable the second-level cache  禁用二级缓存-->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheRegionFactoryAvailableException</property>

        <!-- Echo all executed SQL to stdout 要不要把生成的sql打印出来-->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup hbm:hibernatemapping  ddl建表语句  要不要用hibernate自动生成建表语句  暂时注释-->
        <!--<property name="hbm2ddl.auto">update</property>-->
        <mapping resource="model/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

student.java

package model;

/**
 * Created by dongyu.liu on 2017-5-22.
 */
public class Student {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Student.hbm.xml

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

<hibernate-mapping package="model">
    <!--table 属性不配就是默认-->
    <class name="Student" >
        <!-- id指属性  name  代表类中的属性  column可省略代表name和数据库名字一致-->
        <id name="id" >
            <generator class="increment"/>
        </id>
        <property name="name" />
        <property name="age"/>
    </class>
</hibernate-mapping>

测试

package model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Created by dongyu.liu on 2017-5-22.
 */
public class TestStudent {
    public static void main(String[] args) {
       Student s = new Student();
        s.setId(1);
        s.setName("s1");
        s.setAge(1);

        Configuration cfg = new Configuration();
        SessionFactory sf = cfg.configure().buildSessionFactory();

        Session sessions = sf.openSession();
        sessions.beginTransaction();
        sessions.save(s);
        sessions.getTransaction().commit();
        sf.close();
    }
}

遇到的问题

1 .xml文件无法编译到class文件夹中,在pom.xml中增加配置

<build>
    <finalName>hibernatetest</finalName>
      <!-- xml文件正常编译到class目录中-->
      <resources>
          <resource>
              <directory>src/main/java</directory>
              <includes>
                  <include>**/*.xml</include>
              </includes>
              <filtering>true</filtering>
          </resource>
      </resources>
  </build>
  1. The server time zone value ‘XXXXXX’ is unrecognized or represents more than one time zone.这个问题是时区不同步造成的,可以在hibernate.cfg.xml文件中在url后面添加参数:serverTimezone=UTC
 <property name="connection.url">jdbc:mysql://localhost/hibernate?serverTimezone=UTC</property>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值