simple.java~7~

来自「hibernate+spring的相片上传项目」· JAVA~7~ 代码 · 共 637 行 · 第 1/2 页

JAVA~7~
637
字号
                        List list=query.list();
                        Integer in=(Integer)list.get(0);
                        if(in.intValue()>0)
                        {
                                hql="select max(a."+idName+") from "+entityName+" a";
                                query=this.getHibernateTemplate().createQuery(this.getSession(),hql);
                                //query=this.getSession().createQuery(hql);//使用Session对象一定要记得关闭session
                                list=query.list();
                                in=(Integer)list.get(0);
                                maxValue=in.intValue()+1;
                                log.info("maxID:"+maxValue);
                        }

                }
                catch(Exception e)
                {
                        log.info("查找最大ID失败,出现异常");
                        e.printStackTrace();
                }
                return maxValue;
        }
        /**
         * 判断用户是否已订购包月业务
         */
        public int findBaoYue(SCSP scsp)
        {
                int res=0;
                try
                {
                        String hql="from SCSP as sc where sc.itemName=? and sc.userId=? and sc.stat=0 order by sc.sp_time desc";
                        Query query=this.getHibernateTemplate().createQuery(this.getSession(), hql);
                        query.setString(0, scsp.getItemName());
                        query.setString(1, scsp.getUserId()+"");
                        List list=query.list();
                        Iterator it=list.iterator();
                        if(it.hasNext())
                                res=1;
                        else
                                res=0;
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
                return res;
        }
        /**
         * 修改批定交易号的包月记录
         */
        public void updateBaoYue(SCSP scsp)
        {
                Session session=null;
                Connection con=null;
                try
                {
                        String sql="update add_scsp set starttime=to_char(sysdate,'yyyy-mm-dd HH24:mm:ss'),endtime=to_char(sysdate + 30,'yyyy-mm-dd HH24:mm:ss'),ifextend=? where transactionid=?";
                        session=this.getSession();
                        con=session.connection();
                        PreparedStatement ps=con.prepareStatement(sql);
                        ps.setInt(1, scsp.getIfextEnd());
                        ps.setString(2, scsp.getTransactionId());
                        ps.executeUpdate();
                        con.close();
                        session.close();
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
        /**
         * 认证用户订购订单订购成功时修改相应订单记录(包月)
         * @param scsp
         */
        public void updateBaoYueTwo(SCSP scsp)
        {
                try
                {
                        String hql="update add_scsp set stat=0 where transactionid=?";
                        //Session session=this.getHibernateTemplate().getSessionFactory().openSession();
                        Session session=this.getSession();
                        Connection con=session.connection();
                        PreparedStatement ps=con.prepareStatement(hql);
                        ps.setString(1, scsp.getTransactionId());
                        ps.executeUpdate();
                        ps.close();
                        con.close();
                        session.close();
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
        /**
         *
         * @return
         */
        public java.util.List userFindTwo()
        {
                String hql="from SCSP sc where sc.userId=? and sc.stat=? order by sc.sp_time desc";
                List list=this.getHibernateTemplate().find(hql, new Object[]{new Integer(8888),new Integer(4)});
                return list;
        }
        /**
         * 用createQuery查询实体最大ID
         */
        public void	createQuery()
                throws Exception
        {
                String hql="select max(sc.id) from SCSP as sc";
                //Query query=this.getHibernateTemplate().createQuery(this.getSession(), hql);
                //log.info("最大ID:"+query.list().get(0));
                List list=this.getHibernateTemplate().find(hql);
                log.info("最大ID:"+list.get(0));
        }
        /**
         * 使用find方法
         *
         */
        public void useFind()
        {
                //List list=this.getHibernateTemplate().find("from SCSP sc where sc.id=?", new Integer(1),Hibernate.INTEGER);
                String hql="from SCSP sc where sc.id=? and sc.transactionId=?";
                List list=this.getHibernateTemplate().find(hql, new Object[]{new Integer(8888),"230800070000000200000000000000000005"});
                hql="from SCSP sc where sc.id=?";
                list=this.getHibernateTemplate().find(hql, "3333");
                Iterator it=list.iterator();
                while(it.hasNext())
                {
                        SCSP sc=(SCSP)it.next();
                        log.info(sc.getUserId());
                        log.info(sc.getItemName());
                        log.info(sc.getTransactionId());
                }
        }

        /**
         *
         * @throws Exception
         */
        public void exeUP()
                throws Exception
        {
                this.getHibernateTemplate().update("update SCSP as s set s.stat=4");
                /*String hql="update `````````";
                Query query=this.getHibernateTemplate().createQuery(this.getSession(), hql);

                this.getHibernateTemplate().execute(new HibernateCallback()
                {
                        public Object doInHibernate(Session s) throws HibernateException,SQLException
                        {
                                String hql="update SCSP ";
                                Query query=s.createQuery(hql);
                                return null;
                        }
                });*/
        }
        /**
         * 批量更新与批量操作推荐由JDBC API直接操作效果更好.
         * 通过创建hql得到对象然后再修改对象属性
         * 修改.先查找出id对应的对象,然后于修改对属性,最然再update
         */
        public void updateThe(SCSP scsp)
        {
                String hql="from SCSP sc where sc.id=?";
                List list=this.getHibernateTemplate().find(hql, scsp.getId());
                scsp=(SCSP)list.get(0);
                scsp.setItemName("刘向");
                scsp.setFee(new Integer(10));
                this.getHibernateTemplate().update(scsp);
        }
        /**
         * 通过得到单个对象修改
         * @param scsp
         */
        public void updateTheTT(SCSP scsp)
        {
            //scsp=(SCSP)this.getHibernateTemplate().load(SCSP.class, scsp.getId());
                scsp=(SCSP)this.getHibernateTemplate().get(SCSP.class, scsp.getId());
                scsp.setItemName("修改哈哈");
                scsp.setFee(0);
                scsp.setStat(new Integer(10));
                this.getHibernateTemplate().update(scsp);
        }
        /**
         * 此种修改不合法
         */
        public void updateTheT(SCSP scsp)
        {
                this.getHibernateTemplate().update(scsp);
        }
        /**
         * 删除单个用户只需要设置用户的标识既可删除用户
         * @param scsp
         */
        public void delete(SCSP scsp)
        {
                this.getHibernateTemplate().delete(scsp);
                log.info("删除成功");
                /*
                 * 批量修改(不成功)
                 */
                Session session=this.getSession();
                String hql="update SCSP sc set sc.itemName=? where sc.id=?";
                try
                {
                        net.sf.hibernate.Transaction tx=session.beginTransaction();
                        List list=session.createQuery(hql).setInteger(0, new Integer(8888)).list();
                        Iterator it=list.iterator();
                        while(it.hasNext())
                        {
                                SCSP scspT=(SCSP)it.next();
                                scspT.setItemName("向辉");
                                session.update(scspT);
                                tx.commit();
                        }
                        session.close();
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
        /**
         * 通过查询批量语句删除
         * @param scsp
         */
        public void deleteTwo(SCSP scsp)
        {
                this.getHibernateTemplate().delete("from SCSP sc where sc.id="+scsp.getId());
                log.info("Two删除成功");
        }
        /**
         *
         * @param num
         * @param hql
         * @return
         */
        public List getDishList(final String hql,final int currPage,final int pageSize,final int recordnum)
        {
                return getHibernateTemplate().executeFind(new HibernateCallback()
                {
                        public Object doInHibernate(Session s) throws HibernateException,
                                        SQLException
                        {
                                List list=null;
                                try
                                {
                                        Query query = s.createQuery(hql);
                                        query.setFirstResult(0); //从第0条记录开始取出10条记录
                                        query.setMaxResults(10);
                                        list = query.list();
                                }
                                catch(Exception e){e.printStackTrace();}
                                return list;
                        }
                });
        }
        public List getList()
                throws Exception
        {
                List list=new java.util.ArrayList();
                java.sql.Connection con=this.getSession().connection();
                java.sql.ResultSet rs=con.prepareStatement("").executeQuery();
                while(rs.next())
                {
                        SCSP scsp=new SCSP();
                        scsp.setId(new Integer(scsp.getInt("id")));
                        scsp.setItemName(rs.getString("itemName"));
                        scsp.setStartTime(rs.getString("starttime"));
                        list.add(scsp);
                }
                return list;
        }
        /**
         * 使用期JdbcTemplate执行sql
         */
        public void userJdbcTemplate()
        {
                JdbcTemplate jt=new JdbcTemplate();
                jt.setDataSource(this.getDataSource());
                List list=jt.queryForList("select * from add_scsp");
                Iterator it=list.iterator();
                int i=0;
                while(it.hasNext())
                {
                        i++;
                        System.out.println(it.next());
                }
        }
        /**
         *
         * @param args
         * @throws Exception
         */
        public static void main(String[] args)
                throws Exception
        {

                /*SCSP scsp=new SCSP();
                scsp.setId(new Integer(8888));
                List list=dao.getDishList("from SCSP",0,0,0);
                Iterator it=list.iterator();
                int i=0;
                while(it.hasNext())
                {
                        i++;
                        SCSP scsp2=(SCSP)it.next();
                        System.out.println(scsp2.getUserId());
                        System.out.println(scsp2.getItemName());
                        System.out.println(scsp2.getTransactionId());
                }
                System.out.println("读出了"+i+"数据");*/
                dao.userJdbcTemplate();
        }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?