您的位置:首页 > 其它

映射文件

2016-05-16 15:44 302 查看
映射配置:hbm.xml

<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!--

This mapping demonstrates

(1) a table-per-subclass mapping strategy

(2) a simple component mapping

(3) recursive associations withing an inheritance tree

-->

<hibernate-mapping package="demo">
<!--
name:类名
table:表名
-->
<class name="User" table="t_user">

<id name="id" type="int" column="id" >
<generator class="native"/> <!--表示自动增长-->
</id>
<!--
普通属性:(数据库中的基本类型,如字符串,日期、数字等)
name:对象中的属性名,必须有
type:对象中属性的类型,如果不写,hibernate会自动检测
可以写Java中类的全名 比如java.lang.String
或者写hibernate类型
column:数据库中表的列,默认为属性名
length:长度,不是所有的属性都有长度,比如varchar有,int没有,如果不写默认为255
not-null:非空约束,默认为false
-->
<property name="name" type="string" column="name" />
<property name="age" type="int" column="age_" />
<property name="birth" type="timestamp" column="birth_" />
<property name="description" type="text" length="5000" column="description_" />
<!--二进制类型,最好指定长度-->
<property name="photo" type="binary" length="102400" column="photo_"/>
</class>

</hibernate-mapping>
package demo;

import java.util.Date;

public class User {
private int id;
private String name;
private int age;
private Date birth;
private String description; //说明
private byte[] photo;//照片

public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
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;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "id="+id+" name="+name+" age="+age+" birth="+birth+
" description="+description;
}
}


生成表
package demo;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

public class CreateSchema {
@Test
public void test(){
Configuration cfg=new Configuration().configure();
SchemaExport schemaExport=new SchemaExport(cfg);
//第一个参数的作用:print the DDL to the console
//第二个参数的作用:export export the script to the database
schemaExport.create(true, true);

}
}

package demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

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

public class App {
private static SessionFactory sessionFactory;
//初始化
static{
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//读取指定的主配置文件
sessionFactory=cfg.buildSessionFactory();//生成session工厂
}
@Test
public void testSave() throws Exception{
//读取文件图片
InputStream is=new FileInputStream("d:/dog.jpg");
byte [] photo=new byte[is.available()];
is.read(photo);
is.close();

User  user=new User();
user.setName("张三");
user.setAge(23);
user.setBirth(new Date());
user.setDescription("hahahha");
user.setPhoto(photo);
//保存
Session session=sessionFactory.openSession();
Transaction  tx=session.beginTransaction(); //开启事务
session.save(user);
tx.commit();//提交事务
session.close();  //释放资源,不一定是真正的关闭
}

@Test
public void testGet()throws Exception{
Session session=sessionFactory.openSession();
Transaction  tx=session.beginTransaction(); //开启事务

User user=(User)session.get(User.class,1);
System.out.println(user);

OutputStream os=new FileOutputStream("d:/copy.jpg");
os.write(user.getPhoto());
os.close();

tx.commit();//提交事务
session.close();  //释放资源,不一定是真正的关闭
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: