今天学到一点点hibernate优化的东西.先记录下来.
hibernate优化其实做到两点就行
第一点,配置文件,每次commit提交次数设置好
- public List hello()
- {
- List list=(List) this.getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session) throws HibernateException,
- SQLException {
- // TODO Auto-generated method stub
- String sql="select uid,uname,uport from userlist";
- List ls=session.createQuery(sql).list();
- return ls;
- }
- });
- return list;
- }
- public void doInsert()
- {
- //hibernate 优化
- this.getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session) throws HibernateException,
- SQLException {
- /*
- * 假设一次性插入10W行数据
- */
- int i=0,c=0;
- String exeSql="insert into userlist values(?,?,?)";
- Query query=null;
- long begin =System.currentTimeMillis();
- for(;i<100000;i++)
- {
- query=session.createQuery(exeSql);
- query.setParameter(1,i);
- query.setParameter(2,i);
- query.setParameter(3,new SimpleDateFormat("yyyy-MM-dd 24HH:mm:ss").format(new Date()));
- query.executeUpdate();
- c=c+1;
- if(c%50==0)
- {
- session.flush();
- session.clear();
- }
- }
- long end =System.currentTimeMillis();
- System.out.println("doing time:"+(end-begin));
- return null;
- }
- });
- }