Hibernate映射普通属性、Hibernate中的各种类型

xiaoxiao2021-02-28  72

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 导入包 --> <hibernate-mapping package="cn.itcast.c_hbm_property"> <!-- name属性:哪个类 table属性:对应那个表,如果不写,默认的表名就是类的简单名称 --> <class name="User" table="user"> <!-- id int类型 --> <id name="id" type="int" column="id"> <!-- 自增长 --> <generator class="native" /> </id> <!-- 普通的属性 (数据库中基本类型,如果字符串、数字、日期等) name属性:对象中的属性名,必须要有的。 type属性:类型,如果不写,Hibernate会自动检测 可以写Java中类的全名。 或是写Hibernate类型。 column属性:对应表中的列名,如果没有,默认为属性名 length属性:长度,不是所有的属性都有长度属性的,比如varchar有,但int没有,如果不写默认为255 not-null属性:非空约束,默认为false --> <property name="name" type="string" column="name" length="20" not-null="true"/> <property name="age" type="int" column="age_" /> <property name="birthday" type="date" column="birthday_" /> <!-- 当列名与关键字冲突时,可以通过column属性指定一个其它列名。 或是使用反引号包围起来。 指定使用text类型时,最好再指定length,以确定生成SQL类型是能够存放指定数量的字符的。 <property name="desc"> <column name="desc_" length="5000" sql-type="text"/> </property> --> <property name="desc" type="text" length="5000" column="desc_"/> <!-- 头像,二进制类型,最好指定长度 --> <property name="photo" type="binary" length="1024" column="photo_"/> </class> </hibernate-mapping>

package cn.itcast.c_hbm_property; import java.util.Arrays; import java.util.Date; /** * 实体 * * @author 风清杨 * @version V1.0 */ public class User { private int id;// 编号 private String name;// 姓名 private int age;// 年龄 private Date birthday;// 生日 private String desc;// 一大段说明 private byte[] photo;// 头象图片 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public byte[] getPhoto() { return photo; } public void setPhoto(byte[] photo) { this.photo = photo; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", birthday=" + birthday + ", desc=" + desc + ", photo=" + Arrays.toString(photo) + "]"; } } package cn.itcast.c_hbm_property; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; 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; /** * 操作类 * * @author 风清杨 * @version V1.0 */ public class App { private static SessionFactory sessionFactory; // 初始化配置文件 static { sessionFactory = new Configuration()// .configure()// 读取默认主配置文件 .addClass(User.class)// .buildSessionFactory();// 根据配置生成session工厂 } /** * 保存 * * @throws Exception */ @Test public void testSave() throws Exception { // 读取图片文件 BufferedInputStream bis = new BufferedInputStream(new FileInputStream( "test.png")); byte[] photo = new byte[bis.available()]; bis.read(photo); bis.close(); // 创建对象 User user = new User(); user.setName("张三"); user.setAge(18); user.setBirthday(new Date()); user.setDesc("2016年4月,布鲁克斯在接受韩国媒体“民众之声”采访,被问及“继引进‘爱国者’导弹、‘萨德’系统和宙斯盾系统后,韩国安保防御还需要什么”时,他表示,为加强对朝鲜发射弹道导弹的预警和追踪,同时迅速、全方位地共享相关信息,必须加强韩美两国在弹道导弹防御方面的“合作运营能力”。布鲁克斯还强调,韩方应该加大反导武器的购买力度,提升自身防御能力的同时,也应为韩美同盟反导体系做贡献,这也符合韩美战时作战指挥权转换的条件。"); user.setPhoto(photo); // 保存? Session session = sessionFactory.openSession();// 打开一个新的Session Transaction tx = session.beginTransaction();// 开始事务 session.save(user); tx.commit();// 提交事务 session.close();// 关闭session 释放资源 } /** * 获取 * * @throws Exception */ @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.getId()); System.out.println(user.getName()); System.out.println(user.getAge()); System.out.println(user.getBirthday()); System.out.println(user.getDesc()); System.out.println(user.getPhoto()); OutputStream os = new FileOutputStream("copy.jpg"); os.write(user.getPhoto()); os.close(); tx.commit(); session.close(); } }

转载请注明原文地址: https://www.6miu.com/read-30789.html

最新回复(0)