getHibernateTemplate()

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38703170/article/details/79960281

getHibernateTemplate()提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作。
它的常用方法:
saveOrUpdate():更新或者插入,有主键就执行更新,如果没有主键就执行插入。https://blog.csdn.net/huzhigenlaohu/article/details/50054249这个博主写的很好,链接留着以后用。
find(String queryString):根据HQL查询字符串来返回实例集合。

例子:

public Person queryPersonById(String id) {//根据id查询人员信息
        Person person= null;
        String hql = "from Person p where p.id = ?";
        List list = getHibernateTemplate().find(hql, id);//根据hql查询字符串来返回实例集合
        if ((list != null) && (list.size() > 0)){
            person = (Person)list.get(0);//获得List中的第一条数据
        }
        return person;
    }

补充:list.get(int i)方法,返回list中第i项。

getHibernateTemplate().

05-23

public List selectAll(String hql,String TName) rnrntry rnreturn getHibernateTemplate().find(hql, TName);rnrn catch (RuntimeException re) rnrnthrow re;rnrnrnrnrnrnrnrnpublic List selectAll(String TName) rnrn System.out.println("21212121212121212121");rnrnreturn this.selectAll("From ?",TName);rnrnrnrnrnrnrnrnrn这样写法有问题吗?rn反正我这样写报错了 , 可以输出那些21212121 应该是查询出错了rnrnrnrnrnjava.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)Vrnorg.hibernate.hql.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1590)rnorg.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1408)rnorg.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)rnorg.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)rnorg.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)rnorg.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)rnorg.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:274)rnorg.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)rnorg.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)rnorg.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)rnorg.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)rnorg.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)rnorg.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)rnorg.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)rnorg.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)rnorg.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)rnorg.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)rnorg.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)rnorg.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)rnorg.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)rnorg.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:908)rncom.pro.dao.BaseDao.selectAll(BaseDao.java:65)rncom.pro.dao.OneDAO.selectAll(OneDAO.java:25)rncom.pro.serivce.OneSerivce.selectAll(OneSerivce.java:36)rncom.pro.action.FindAction.findty(FindAction.java:29) 论坛

没有更多推荐了,返回首页