当前位置: 代码迷 >> Web前端 >> java web从0单排第二十四期《hibernate》常用数据类型(1)
  详细解决方案

java web从0单排第二十四期《hibernate》常用数据类型(1)

热度:165   发布时间:2013-10-08 16:55:16.0
java web从零单排第二十四期《hibernate》常用数据类型(1)

这一期主要介绍如何操作一些基本的数据类型,由于只涉及到hibernate本身,就不用web工程了,我们建立一个java工程就可以工程名为hibernate2:

1.

点击Add Hibernate Capablities

点击next:

点击next:

点击next:

finish完成创建。

把上个工程的hibernate.cfg.xml文件到新建的工程,选择yes to all:

 

添加sql2008jar包:

 

点击configure Build Path...:

点击Add External JARS找到自己的驱动包位置即可:

 

 

 

2.新建数据库customer:

首先对数据类型进行分析:

bigint:long类型。

varchar:字符串类型。

int:整型。

bit:bool类型。

date:时间类型。

timestamp:时间类型,精确度更高,可以精确到毫秒。

image:大数据类型,即二进制文件,如文件的存储。

3.新建包com.test.bean,添加类Customers.java:

package com.test.bean;

import java.sql.Timestamp;
import java.util.Date;

public class Customers
{

	private Long id;
	private String name;
	private String email;
	private String password;
	private int phone;
	private String address;
	
    private char sex;
	private boolean married;
	
	private String description;
	private Date birthday;
	
	private Timestamp registeredTime;
	
	private byte[] image;

	public Long getId()
	{
		return id;
	}

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

	public String getName()
	{
		return name;
	}

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

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public int getPhone()
	{
		return phone;
	}

	public void setPhone(int phone)
	{
		this.phone = phone;
	}

	public String getAddress()
	{
		return address;
	}

	public void setAddress(String address)
	{
		this.address = address;
	}

	public char getSex()
	{
		return sex;
	}

	public void setSex(char sex)
	{
		this.sex = sex;
	}

	public boolean isMarried()
	{
		return married;
	}

	public void setMarried(boolean married)
	{
		this.married = married;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public Date getBirthday()
	{
		return birthday;
	}

	public void setBirthday(Date birthday)
	{
		this.birthday = birthday;
	}

	public Timestamp getRegisteredTime()
	{
		return registeredTime;
	}

	public void setRegisteredTime(Timestamp registeredTime)
	{
		this.registeredTime = registeredTime;
	}

	public byte[] getImage()
	{
		return image;
	}

	public void setImage(byte[] image)
	{
		this.image = image;
	}
}


新建Customer.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>
  
  <class name="com.test.bean.Customers" table="customers">
    <id name="id" column="id" type="long">
    <generator class="increment"><!-- 主键id的生成方式 -->
    </generator>
    </id>
 <property name="name" column="name" type="string" not-null="true"></property>
 <property name="address" column="address" type="string" not-null="true"></property>
 <property name="birthday" column="birthday" type="date" not-null="true"></property>
 <property name="description" column="description" type="string"></property>
 <property name="password" column="password" type="string"></property>  
   <property name="phone" column="phone" type="int"></property>
    <property name="married" column="is_married" type="boolean"></property>
     <property name="image" column="image" type="binary"></property>
      <property name="registeredTime" column="registered_time" type="timestamp" insert="false" update="false"></property>
      <property name="sex" column="sex" type="character"></property>
      <property name="email" column="email" type="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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory> 
       <property name="show_sql" >true</property>   
       <property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;DatabaseName=hibernate2</property>
       <property name="connection.username">sa</property>
       <property name="connection.password">57829707</property>
       <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
       
       <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
       
       <mapping resource="Customers.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

下一期会进行一些测试,Thank you!!


 

  相关解决方案