zoukankan      html  css  js  c++  java
  • hibernate动态创建数据库表名几种方式

    数据库中数据量很大, 但又不可以删除时同时又要优化程序检索数据时间。

    答:方式有很多比如 创建数据库表分区,创建索引, 存储过程等; 我这里采用动态创建数据库表的方式。
    完全可以在不创建表分区情况下实行分表管理 例如 日志记录表 将日期(yyyy-MM)作为默认表后缀动态追加, 例如 文章发布表 将用户名作为后缀名进行动态追加 ;

      动态创建数据库表的方式要具体问题具体分析, 比如JDBC中直接使用create table 表名_dynamicStr(...); 还可以使用hibernate的sql查询,不使用hql就不用走.hbm.xml文件了.
    文章发布系统 dynamicStr 可以用已注册登陆的用户名, 日志记录表 dynamicStr 可以采用 'log_'+new SimpleDateFormat("yyyyMM").format(new Date());

    下面例子是使用hibernate动态创建数据库表的例子,缺点是每次都要创建sessionFactory对象如果不同用户在同一时间段内不停的发布信息那是相当好系统资源的事情不可取,用在局域网系统还算靠谱 如果有兴趣可以将下面的代码抽取成动态创建表的Utils.
    public
    class ReportDBApi { private SessionFactory sessionFactory = null; public ReportDBApi(){ createSession(); } public void createSession(){ Date date = new Date(); SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd"); String now_time = simpledateformat.format(date); String tablename = "TBL_REPORT_STATUS_20050707"; tablename = "TBL_REPORT_STATUS_" + now_time; try { Configuration cfg = new Configuration().addClass(cn.sports.vas.sms.unicom.TblReportStatus.class).configure(); Table table = cfg.getClassMapping(TblReportStatus.class).getTable(); table.setName(tablename); cfg.getClassMapping(TblReportStatus.class).setTable(table); sessionFactory = cfg.buildSessionFactory(); } catch (MappingException ex) { ex.printStackTrace(); }catch (HibernateException ex) { ex.printStackTrace(); } } public void insertPO(TblReportStatus po) throws HibernateException { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save(po); tx.commit(); session.close(); } public void closeSession() throws HibernateException { sessionFactory.close(); } }
  • 相关阅读:
    用Python打造一款文件搜索工具,所有功能自己定义!
    Python+Excel+Word一秒制作百份合同
    只需6行代码,Python将PPT转为Word!
    老板让我从几百个Excel中查找数据,我用Python一分钟搞定!
    爬虫遇到头疼的验证码?Python实战讲解弹窗处理和验证码识别
    SoftEther服务端配置
    SoftEther服务端安装
    nginx学习
    zookeeper安装
    prometheus监控之自动发现
  • 原文地址:https://www.cnblogs.com/YingYue/p/3855540.html
Copyright © 2011-2022 走看看