映射文件
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 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);
}
}
<?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(); //释放资源,不一定是真正的关闭 } }
相关文章推荐
- JavaScript实现页面跳转的方式汇总
- SHELL 编程之语法基础
- android dex文件超出限制个数分包方案
- 使用Gson解析Json的一条数据和一组数据
- 友盟自动更新技术解决方案(安卓版)
- php学习网站
- Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
- ccpc 2016 省赛
- swift-UILabel
- vs中web配置可浏览json数据文件
- teiid入门
- 每天一个linux命令(15):tail 命令
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- 关于安卓调试桥adb.exe被金山wps软件导致冲突问题。
- 树形和仿树形下拉框
- Mac下修改Android Studio的默认JDK版本
- 时间类
- MySQL检索数据(过滤+通配符+正则表达式)
- swift-UIButton
- 雷达图