hibernate实体类型映射文件

1.通过写hibernate映射文件。实体类型转换为数据库中的表

据实体类型而写的。

实体类型User.java

package cn.wwh.www.hibernate.dd.property;

import java.util.Arrays;
import java.util.Date;

/**
 *类的作用:
 *
 *
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-17   下午08:05:30
 */
public class User {
	private Integer id;
	private String name; // 姓名
	private boolean gender; // true表示男。false表示女
	private Date birthday; // 生日
	private String desc; // 一大段说明,最多为5000字
	private byte[] photo; // 照片
	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the gender
	 */
	public boolean isGender() {
		return gender;
	}
	/**
	 * @param gender the gender to set
	 */
	public void setGender(boolean gender) {
		this.gender = gender;
	}
	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}
	/**
	 * @param birthday the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	/**
	 * @return the desc
	 */
	public String getDesc() {
		return desc;
	}
	/**
	 * @param desc the desc to set
	 */
	public void setDesc(String desc) {
		this.desc = desc;
	}
	/**
	 * @return the photo
	 */
	public byte[] getPhoto() {
		return photo;
	}
	/**
	 * @param photo the photo to set
	 */
	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}
	@Override
	public String toString() {
		return "User [birthday=" + birthday + "\n desc=" + desc + "\n gender="
				+ gender + "\n id=" + id + "\n name=" + name + "\n photo="
				+ Arrays.toString(photo) + "]";
	}
	
	

	

}

2.映射文件User.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="cn.wwh.www.hibernate.dd.property.User" table="user">
	<id name="id" type="int" column="id" >
		<generator class="native"></generator>
	</id>
		<!-- 
			name:对象中的属性名。必需要有
			type:数据的类型。不写时会自己主动检測
			column:相应的列名。不写时默觉得属性的名称
			not-null:true/false。是否有非空约束,默觉得false
			length:长度,默觉得255
		-->
		<!--
		<property name="name" type="string" column="name" not-null="true" length="35"/>
		<property name="name" type="string">
			<column name="name_" not-null="true" length="55"></column>
		</property>		
		 -->
		 <property name="name" type="string" column="name" not-null="true"></property>
		<property name="gender"></property>
		<!-- 对于日期要指定类型 -->
		<property name="birthday" type="date"></property>
		<!-- 大文本数据要指定长度,因为desc是数据库中的keyword。要用反单引號 -->
		<property name="desc" type="text" column="`desc`" length="5000"></property>
		<!-- 对于二进制数据类型。要指定长度 -->
		<property name="photo" type="binary" column="photo" length="888888"></property>
	</class>

</hibernate-mapping>

3.主配置文件:
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> <!-- 1、数据库信息:数据库方言(是一个类的全名)与数据库连接信息 --> <!-- 配置连接数据库所需的驱动 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 连接数据库的数据库url --> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <!-- 数据库的账号 --> <property name="connection.username">root</property> <!-- 数据库的密码 --> <property name="connection.password">wwh</property> <!-- 指定数据库的方言(本人用的是MySql) --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 2.配置其他 --> <!-- create: 先删表。再建表。 

                        create-drop: 启动时建表,退出前删表。

<span style="white-space:pre">			</span> update: 假设表结构不一致,就创建或更新。
			validate: 启动时验证表结构,假设不致就抛异常。
	       	-->
		<!-- 自己主动创建数据表 -->
		<property name="hbm2ddl.auto">update</property>
		<!-- 显示数据操作的sql语句 -->
		<property name="show_sql">true</property>
		<!-- 格式化的显示sql语句 -->
		<property name="format_sql">true</property>
		<!-- 3.导入映射配置文件 -->
	<mapping resource="cn/wwh/www/hibernate/dd/property/User.hbm.xml"/>

	</session-factory>

</hibernate-configuration>

4.測试数据库中的数据:

package cn.wwh.www.hibernate.dd.property;

import java.io.FileInputStream;
import java.util.Date;

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

/**
 *类的作用:
 *
 *
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-17   下午08:10:38
 */
public class TestSqlByXml {

	private static SessionFactory sessionFactory = new Configuration()
			.configure()
			.buildSessionFactory();

	// 保存
	@Test
	public void testSave() throws Exception {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		// ---------------------------------------

		// 从硬盘中读取图片。然后存储数据库中
		FileInputStream in = new FileInputStream("F:/psb.jpg");
		byte[] photo = new byte[in.available()];
		in.read(photo);
		in.close();

		// 准备对象
		User user = new User();
		user.setName("一叶扁舟");
		user.setGender(true);
		user.setBirthday(new Date());
		user.setDesc("一叶扁舟是一个积极向上的孩子,为梦想而努力奋斗着…………");
		user.setPhoto(photo);

		// 保存
		session.save(user);

		// ---------------------------------------
		session.getTransaction().commit();
		session.close();
	}

	// 获取
	@Test
	public void testGet() throws Exception {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		// ---------------------------------------

		User user = (User) session.get(User.class, 1);
		System.out.println(user.getName());
		System.out.println(user.isGender());
		System.out.println(user.getBirthday());
		System.out.println(user.getDesc());
		System.out.println(user.getPhoto());
//		System.out.println(user);

		// ---------------------------------------
		session.getTransaction().commit();
		session.close();
	}

	
	

}



版权声明:本文博客原创文章,博客,未经同意,不得转载。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值