zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Hibernate使用@Lob、@Basic修饰大数类型的属性

    <?xml version="1.0" encoding="GBK"?>
    <project name="hibernate" basedir="." default="">
        <property name="src" value="src"/>
        <property name="dest" value="classes"/>
    
        <path id="classpath">
            <fileset dir="../../lib">
                <include name="**/*.jar"/>
            </fileset>
            <pathelement path="${dest}"/>
        </path>
    
        <target name="compile" description="Compile all source code">
            <delete dir="${dest}"/>
            <mkdir dir="${dest}"/>
            <copy todir="${dest}">
                <fileset dir="${src}">
                    <exclude name="**/*.java"/>
                </fileset>        
            </copy>
            <javac destdir="${dest}" debug="true" includeantruntime="yes"
                deprecation="false" optimize="false" failonerror="true">
                <src path="${src}"/>
                <classpath refid="classpath"/>
                <compilerarg value="-Xlint:deprecation"/>
            </javac>
        </target>
    
        <target name="run" description="Run the main class" depends="compile">
            <java classname="lee.PersonManager" fork="yes" failonerror="true">
                <classpath refid="classpath"/>
            </java>
        </target>
    
    </project>
    package lee;
    
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    import org.hibernate.service.*;
    import org.hibernate.boot.registry.*;
    import org.crazyit.app.domain.*;
    import java.io.*;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class PersonManager
    {
        public static void main(String[] args)
            throws Exception
        {
            // 实例化Configuration,
            Configuration conf = new Configuration()
                .configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(conf.getProperties()).build();
            // 以Configuration实例创建SessionFactory实例
            SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
            // 创建Session
            Session sess = sf.openSession();
            // 开始事务
            Transaction tx = sess.beginTransaction();
            // 创建Person对象
            Person person = new Person();
            // 为Person对象的属性设置值
            person.setName("crazyit.org");
            File file = new File("logo.jpg");
            byte[] content = new byte[(int)file.length()];
            new FileInputStream(file).read(content);
            person.setPic(content);
            // 保存Person对象
            sess.save(person);
            // 提交事务
            tx.commit();
            // 关闭Session
            sess.close();
            sf.close();
        }
    }
    package org.crazyit.app.domain;
    
    import javax.persistence.*;
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    @Entity
    @Table(name="person_inf")
    public class Person
    {
        @Id // 用于修饰标识属性
        // 指定该主键列的主键生成策略
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer id;
        // @Column指定该属性映射的列信息,此处指定了列名、长度
        @Column(name="person_name" , length=50)
        private String name;
        @Lob
        @Basic(fetch=FetchType.LAZY)
        @Column(nullable=true)
        private byte[] pic;
    
        // id的setter和getter方法
        public void setId(Integer id)
        {
            this.id = id;
        }
        public Integer getId()
        {
            return this.id;
        }
    
        // name的setter和getter方法
        public void setName(String name)
        {
            this.name = name;
        }
        public String getName()
        {
            return this.name;
        }
    
        // pic的setter和getter方法
        public void setPic(byte[] pic)
        {
            this.pic = pic;
        }
        public byte[] getPic()
        {
            return this.pic;
        }
    }
  • 相关阅读:
    python基础之字符串和字节的转换
    python学习笔记(三)字符串方法、读写文件、json处理以及函数
    python学习笔记(二):list,字典,字符串,元组,文件
    python学习笔记(一):python入门
    接口测试:jmeter学习笔记:数据库操作和压测
    接口测试:postman和jmeter随记
    设计模式之建造者模式
    设计模式之外观模式
    设计模式之模板模式
    设计模式之原型模式
  • 原文地址:https://www.cnblogs.com/tszr/p/12367231.html
Copyright © 2011-2022 走看看