maven开发ssh项目(struts2spring3hibernate3)【第三篇hibernate3之ssh整合开发具体操作(增删改查)】

1、查找数据库

1-1、根据用户名查找数据库表中的内容

String hql="from po.UserBean(与表对应的bean类) where username=?";
     List<UserBean> list=this.getHibernateTemplate().find(hql,username);
     if(!list.isEmpty()&&list.size()>0){//判断数据库中是否存在这个数据
     return list.get(0);
     }

return null;

1-2、查找表中所有数据

String hql="from po.Xsb ";
     List<Xsb> list=this.getHibernateTemplate().find(hql);
    

return list;

1-3、查找数据库中有多少行(也就是有多少条数据)

String hql="select count(*) from po.Xsb";
     List<Long> list=this.getHibernateTemplate().find(hql);
     if(!list.isEmpty()&&list.size()>0){
     return list.get(0);
     }

return (long) 0;

1-4、查找数据库中列值

String hql="select zym,id from po.Zyb ";
     List list=this.getHibernateTemplate().find(hql);
    

return list;

1-5、对于有联合主键的表的查询

上面这种就是联合主键

1、String hql="from po.Cjb ";
    List<Cjb> list=this.getHibernateTemplate().find(hql);
   
return list;

2、String hql="from po.Cjb a where a.id.kch=?";
     List<Cjb> list=this.getHibernateTemplate().find(hql,kch);
     if(!list.isEmpty()&&list.size()>0){
     return list.get(0);
     }

return null;

(这里说明下cjb这张表的联合主键为xh和kch而联合主键在hibernate中是要将联合主键放在一个新类里的然后cjb表中存放这个类的对象来表示)【所以这里的a.id.kch表示cjb中的id(联合主键的对象)中的kch】

-----------------------------------------------------------------------------------

联合主键的多表查询:

String hql="select a.xm,b.kcm,c.id.xh,c.cj,c.xf from po.Xsb a"
+ ",po.Kcb b,po.Cjb c where a.xh=c.id.xh and b.kch=c.id.kch and c.id.xh=?";
   
List list=this.getHibernateTemplate().find(hql,xh);
   
    return list;

1-6、分页查询

这个是我之前就发表过的

https://blog.csdn.net/qq_34299694/article/details/81066561

2、插入数据(如果不需要插入全部数据那就user【这个与库表对应的类对象的值不要去set就行了】主键不能不要哦)

3、删除数据

1、String hql="from po.Cjb a where a.id.xh=?";
        
        this.getHibernateTemplate().deleteAll(this.getHibernateTemplate().find(hql,xh));

2、Xsb xsb=new Xsb();
        xsb.setXh(xh);
        this.getHibernateTemplate().delete(xsb);

4、修改数据

1、this.getHibernateTemplate().update(cj);//cj是对应的表对象

2、Xsb xsb=this.getHibernateTemplate().get(Xsb.class, student.getXh());
        xsb.setBz(student.getBz());
        xsb.setCssj(student.getCssj());
        xsb.setXb(student.getXb());
        xsb.setXm(student.getXm());
        xsb.setZxf(student.getZxf());
        xsb.setZyb(student.getZyb());
        xsb.setXh(student.getXh());
        this.getHibernateTemplate().update(xsb);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值