java 序列化数据到mysql数据库中的方法

发布时间:2021-01-22编辑:脚本学堂
java 序列化数据到mysql数据库中的方法,有需要的朋友可以参考下。注:使用hibernate跟数据库打交道。

java 序列化数据到mysql/ target=_blank class=infotextkey>mysql数据库中的方法,有需要的朋友可以参考下。
注:使用hibernate跟数据库打交道。

hibernate配置文件如下
 

复制代码 代码如下:
    <?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> 
            <!-- 指定连接数据库所用的驱动 --> 
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
            <!-- 指定连接数据库的url,hibernate连接的数据库名 --> 
            <property name="connection.url">jdbc:mysql://localhost:3306/test</property> 
            <property name="connection.useUnicode">true</property> 
            <property name="connection.characterEncoding">utf-8</property> 
            <!-- 指定连接数据库的用户名 --> 
            <property name="connection.username">root</property> 
            <!-- 指定连接数据库的密码 --> 
            <property name="connection.password">you_password</property> 
            <!-- C3P0连接池设定 --> 
            <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 
            <!-- 指定连接池里最大连接数 --> 
            <property name="hibernate.c3p0.max_size">20</property> 
            <!-- 指定连接池里最小连接数 --> 
            <property name="hibernate.c3p0.min_size">1</property> 
            <!-- 指定连接池里连接的超时时长 --> 
            <property name="hibernate.c3p0.timeout">1800</property> 
            <!-- 指定连接池里最大缓存多少个Statement对象 --> 
            <property name="hibernate.c3p0.max_statements">100</property> 
            <property name="hibernate.c3p0.idle_test_period">500</property> 
            <property name="hibernate.c3p0.acquire_increment">2</property> 
            <property name="hibernate.c3p0.validate">true</property> 
            <property name="hibernate.c3p0.preferredTestQuery ">select 1 </property> 
            <property name="hibernate.c3p0.idleConnectionTestPeriod ">18000</property> 
            <property name="hibernate.c3p0.maxIdleTime">25000</property> 
            <property name="hibernate.c3p0.testConnectionOnCheckout">true</property> 
     
            <!-- 指定数据库方言 --> 
            <property name="dialect">org.hibernate.dialect.MySQLinnodbDialect</property> 
            <!-- 根据需要自动创建数据库 --> 
            <property name="hbm2ddl.auto">update</property> 
            <!-- 显示Hibernate持久化操作所生成的SQL --> 
            <property name="show_sql">true</property> 
            <!-- 将SQL脚本进行格式化后再输出 --> 
            <property name="hibernate.format_sql">true</property>             
     
            <mapping resource="test/java/Test.hbm.xml"/>            
        </session-factory> 
    </hibernate-configuration> 
 

        
hibernate映射文件
 

复制代码 代码如下:
   <!DOCTYPE hibernate-mapping PUBLIC 
             "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
             "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
      
     <hibernate-mapping> 
         <class name="test.java.Test" table="test" > 
             <id name="id" > 
                 <generator class="native"></generator> 
             </id> 
            <property name="content" type="java.sql.Blob"> 
                <column name="content" sql-type="blob"></column> 
            </property> 
         </class> 
     </hibernate-mapping> 

hibernate实体类
 

复制代码 代码如下:
    package test.java; 
    import java.sql.Blob; 
     
    public class Test { 
        private int id; 
        private Blob content; 
         
        public Blob getContent() { 
            return content; 
        } 
        public void setContent(Blob content) { 
            this.content = content; 
        } 
        public int getId() { 
            return id; 
        } 
        public void setId(int id) { 
            this.id = id; 
        }
    } 

 

待序列化的类
 

复制代码 代码如下:
    package test.java; 
    import java.io.Serializable; 
    public class User implements Serializable{ 
        private int id; 
        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; 
        } 
        private String name; 
        private int age; 
     
    } 

 

测试类
   

复制代码 代码如下:

package test.java; 
     
    import java.io.ByteArrayInputStream; 
    import java.io.ByteArrayOutputStream; 
    import java.io.IOException; 
    import java.io.ObjectInputStream; 
    import java.io.ObjectOutputStream; 
    import java.sql.Blob; 
    import java.sql.SQLException; 
    import org.hibernate.Hibernate; 
    import org.hibernate.Session; 
    import org.hibernate.SessionFactory; 
    import org.hibernate.Transaction; 
    import org.hibernate.cfg.Configuration;
     
    public class MySerialization { 
        public static SessionFactory sessionFactory=null; 
        public static SessionFactory getSessionFactory(){ 
            if(sessionFactory==null) 
                sessionFactory=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); 
            return sessionFactory; 
        } 
     
        public static Blob getBlob(Object object) throws IOException{ 
            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(); 
            ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream); 
            objectOutputStream.writeObject(object); 
            ByteArrayInputStream bis=new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); 
            return Hibernate.createBlob(bis); 
        } 
         
        public static void writeObjectToDB(Object object) throws IOException{ 
            Test test=new Test(); 
            test.setContent(getBlob(object)); 
            Session session=getSessionFactory().openSession(); 
            Transaction tx =session.beginTransaction(); 
            session.save(test); 
            tx.commit(); 
            session.close(); 
        } 
         
        public static User readObjectFromDB(int id) throws IOException, SQLException, ClassNotFoundException{ 
            Session session=getSessionFactory().openSession(); 
            Test test=(Test) session.get(Test.class,id); 
            ObjectInputStream objectInputStream=new ObjectInputStream(test.getContent().getBinaryStream()); 
            return (User)objectInputStream.readObject(); 
        } 

        public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException { 
            User user=new User(); 
            user.setId(1); 
            user.setAge(22); 
            user.setName("zhangsan"); 
            writeObjectToDB(user); 
             
            User user1=readObjectFromDB(2); 
            System.out.println(user1.getId()); 
            System.out.println(user1.getName()); 
            System.out.println(user1.getAge());
        }     
    }