mysql Blob hbn 操作

1.数据库
mysql >   desc  lotteryinformation ;
+ -- -----------+--------------+------+-----+---------+-------+
|  Field        |  Type          |   Null   |   Key   |   Default   |  Extra  |
+ -- -----------+--------------+------+-----+---------+-------+
|  id           |   int ( 11 )       |  NO    |  PRI  |   NULL      |         |
|  type         |   varchar ( 255 |  NO    |       |   NULL      |         |
|  title        |   varchar ( 255 |  YES   |       |   NULL      |         |
|  inputTime    |   datetime       |  YES   |       |   NULL      |         |
|  content      |  blob          |  YES   |       |   NULL      |         |
|  recommended  |   bit ( 1 )        |  YES   |       |   NULL      |         |
|  categories   |   varchar ( 255 |  YES   |       |   NULL      |         |
+ -- -----------+--------------+------+-----+---------+-------+

2.代码
        //插入
        Session sess 
=   HibernateSessionFactory.getSession();
        Transaction tr 
=  sess.beginTransaction();
        LotteryNew nn 
=   new  LotteryNew();
        nn.setInputTime(
new  Date());
        nn.setCategories(
" t " );
        nn.setTitle(
" new " );
        nn.setRecommended(
true );
        Blob bo 
=  Hibernate.createBlob( " ggggg " .getBytes());
        nn.setContent(bo);
        sess.save(nn);
        tr.commit();
        HibernateSessionFactory.closeSession();
       
        //修改
        sess 
=   HibernateSessionFactory.getSession();
        tr 
=  sess.beginTransaction();
        LotteryNew lo 
=  (LotteryNew) sess.get(LotteryNew. class , nn.getId());
        Blob bog 
=  Hibernate.createBlob( " xxxxx " .getBytes());
        lo.setContent(bog);
        tr.commit();
        HibernateSessionFactory.closeSession();
        
        //查找
        sess 
=   HibernateSessionFactory.getSession();
        LotteryNew lo2 
=  (LotteryNew) sess.get(LotteryNew. class , lo.getId());
        InputStream in 
=  lo2.getContent().getBinaryStream() ;
        
byte [] bbr  =   new   byte [in.available()];
        in.read(bbr);
        System.out.println(
new  String(bbr));
        HibernateSessionFactory.closeSession();

你可能感兴趣的:(mysql Blob hbn 操作)