• hibernate课程 初探单表映射3-2 基本类型


    本节内容:(介绍基本类型)

    1    数据类型 简介

    2    时间类型 简介

    3    时间类型 demo

    1    hibernate类型    java类型

          integer/int      java.lang.Integer/int

          string        java.lang.String

    2      hibernate类型    java类型 

         date        java.util.Date或java.sql.Date      年月日

        time         java.util.Date或java.sql.Time        时分秒

        timestamp       java.util.Date或java.sql.Timestamp    年月日时分秒

    3     demo

    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="connection.driver_class">com.mysql.jdbc.Driver</property>
         <property name="connection.url">jdbc:mysql://localhost:3306/bendi</property>
         <property name="connection.username">root</property>
         <property name="connection.password"> </property>
         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
         
         <property name="show_sql">true</property>
         <property name="format_sql">true</property>
         <property name="hbm2ddl.auto">create</property>
      
      <mapping resource = "Student.hbm.xml"/>     
        </session-factory>
    </hibernate-configuration>

    Student.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 2017-12-20 0:42:12 by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
        <class name="com.ddwei.student.Student" table="STUDENT">
            <id name="pid" type="int">
                <column name="PID" />
                <generator class="increment" />
            </id>
            <property name="name" type="java.lang.String">
                <column name="NAME" />
            </property>
            <property name="sex" type="java.lang.String">
                <column name="SEX" />
            </property>
            <property name="birthday" type="date">
    <!--         <property name="birthday" type="time"> -->
    <!--         <property name="birthday" type="timestamp"> -->
                <column name="BIRTHDAY" />
            </property>
            <property name="address" type="java.lang.String">
                <column name="ADDRESS" />
            </property>
        </class>
    </hibernate-mapping>

    StudentTest.java

    package hibernate_001;

    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.After; import org.junit.Before; import org.junit.Test;

    import com.ddwei.student.Student;

    public class StudentTest {    private SessionFactory sessionFactory;  private Session session;  private Transaction trasaction;    @Test  public void testSaveStudent(){ //  Student student =new Student(1,"周恩来","男",new Date(),"绍兴");//创建学生对象   Student student = new Student(); //  student.setPid(100);   student.setName("秦始皇");   student.setSex("男");   student.setBirthday(new Date());   student.setAddress("阿房宫");   session.save(student);//会话保存学生对象进入数据库  }    @Before  public void init(){   //1  创建配置对象   Configuration config = new Configuration().configure();   //2  创建服务对象   ServiceRegistry serviceRe = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();      //3  创建会话工厂   sessionFactory = config.buildSessionFactory(serviceRe);      //4  打开会话   session = sessionFactory.openSession();   //5  创建事务   trasaction = session.beginTransaction();  }      @After  public void destroy(){   trasaction.commit();   session.close();   sessionFactory.close();  }  

    }

  • 相关阅读:
    获取某个文件夹下面的子文件夹(要求是第一级)
    操作手册
    GWT与GXT
    eclipse中出现:The project cannot be built until build path errors are resolved
    eclipse部署项目要做的工作及配置
    如何测试tomcat安装成功
    tomcat的安装及eclipse配置
    配置jdk
    oracle数据库的安装、完全卸载与plsql的安装以及与oracle的连接
    UVA
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/8261004.html
Copyright © 2020-2023  润新知