hibernate开发环境搭建

1.导入hibernate基本jar包,hibernate-3.2\lib或hibernate-release-4.2.2.Final\lib\required。

2.创建web项目

   我的目录结构。

   hibernate开发环境搭建_第1张图片

注意:

        1.实体类与关系映射文件在同一目录下

           Customer.java

           Customer.hbm.xml

        2.hibernate.cfg.xml在src目录下

3.实体对象和对象关系映射文件配置(Customer.hbm.xml配置)

  -------------- 实体对象

package com.lanhuigu.hibernate.entity;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;

public class Customer implements Serializable{
	private static final long serialVersionUID = -2934493050228154410L;
	
	private Long id;
	private String name;
	private String email;
	private String password;
	private int phone;
	private boolean married;
	private String address;
	private char sex;
	private String description;
	private byte[] image;
	private Date birthday;
	private Timestamp registeredTime;
	
	public Customer(){}

	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 boolean isMarried() {
		return married;
	}

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

	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 String getDescription() {
		return description;
	}

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

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

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

	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;
	}
}

  --------------对象关系映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 1.hibernate-mapping对象关系映射的根元素,其它元素必须嵌套在此元素内 -->
<hibernate-mapping  package="com.lanhuigu.hibernate.entity"><!-- 2.package为实体类在哪个包下 -->
		<!-- 
			3.class中name为持久化的实体类(Customer),table为映射到数据库中的表名(CUSTOMERS),
			如果没有设置table,默认类名作为表名
		-->
		<class name="Customer" table="CUSTOMERS">
			<!-- 
				4.以下配置属性解释: 
				  4.1 name为实体类属性
				  4.2 column为映射到数据库中的字段名,如果没有配置column,默认使用name属性作为映射后数据库表字段名
				  4.3 type为字段类型,如果没有设置type,映射时自动采用实体类的java类型,对应映射数据库字段类型
				  java类型映射到数据库类型参照表:
				  java类型>>>>>>>>>>数据库类型
				  string           VARCHAR
				  int              INT
				  character        CHAR
				  boolean          BIT
				  text             TEXT
				  binary           BLOB
				  date             DATE
				  timestamp        TIMESTAMP
				  4.4 not-null设置该字段是否允许为null,
				  4.5 length设置字段的长度 
		    -->
			<!-- 设置主键 -->
			<id name="id" column="ID" type="long">
				<!-- 主键生成方式-->
				<generator class="increment"/>
			</id>
			<!-- 基本属性,基本属性必须设置在主键之口-->
			<property name="name" column="NAME" type="string" length="25" not-null="true" />
			<property name="email" column="EMAIL" type="string" not-null="true" />
			<property name="password" column="PASSWORD" type="string" not-null="true" />
			<property name="phone" column="PHONE" type="int" />
			<property name="address" column="ADDRESS" type="string" />
			<property name="sex" column="SEX" type="character" />
			<property name="married" column="IS_MARRIED" type="boolean" />
			<property name="description" column="DESCRIPTION" type="text" />
			<property name="image" column="IMAGE" type="binary" />
			<property name="birthday" column="BIRTHDAY" type="date" />
			<property name="registeredTime" column="REGISTERED_TIME" type="timestamp" />
		</class>
	
</hibernate-mapping>

4.hibernate.cfg.xml配置

 

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- hibernate-configuration为hibernate配置头文件 -->
<hibernate-configuration>
	<!-- 配置session-factory,
	     SessionFactory是Hibernate的工厂类,
	              该类负责Hibernate的配置和Hiberante的Session接口中save(),update(),delete(),load(),find()的操作  
	-->
	<session-factory>
		<!-- 1.数据库连接配置 -->
		<!-- 数据库连接url -->
		<property name="connection.url">jdbc:mysql://192.168.200.12:3306/hbtest</property>
		<!-- 数据库连接用户名 -->
		<property name="connection.username">root</property>
		<!-- 数据库连接口令 -->
		<property name="connection.password">root123</property>
		<!-- 数据库连接驱动 -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 数据库方言 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 数据库连接池大小配置 -->
		<property name="connection.pool_size">1</property>
		
		<!-- 配置使用二级缓存的类 -->
		<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
		<!-- 一次读的数据库记录数 -->
		<property name="jdbc.fetch_size">50</property>
		<!-- 设定对数据库进行批量删除条数 -->
		<property name="jdbc.batch_size">30</property>
		
		<!-- 2.是否打印sql配置 -->
		<property name="show_sql">true</property>
		
		<!-- 3.对象-映射关系文件的位置 -->
		<mapping resource="com/lanhuigu/hibernate/entity/Customer.hbm.xml" />
	
	</session-factory>
</hibernate-configuration>

5.测试配置

package com.lanhuigu.hibernate.test;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
 * 基本简单测试配置是否成功
 */
public class TestHibernate {
	public static void main(String[] args) {
		//SessionFactory sessionFactory;
		Configuration configuration = new Configuration().configure();
		//sessionFactory = configuration.buildSessionFactory();
		SchemaExport export =  new SchemaExport(configuration);
		export.create(true, true);
	}
}

6.结果

1.控制台输出

   drop table if exists CUSTOMERS

    create table CUSTOMERS (
        ID bigint not null,
        NAME varchar(25) not null,
        EMAIL varchar(255) not null,
        PASSWORD varchar(255) not null,
        PHONE integer,
        ADDRESS varchar(255),
        SEX char(1),
        IS_MARRIED boolean,
        DESCRIPTION longtext,
        IMAGE tinyblob,
        BIRTHDAY date,
        REGISTERED_TIME datetime,
        primary key (ID)
    )

 2.去自己数据库看到一张表:CUSTOMERS

说明配置成功,hibernate开发环境基本搭建完成,可以进行接下来的hibernate操作


你可能感兴趣的:(configuration,Hiberante,XXX.hbm.xml)