Hibernate笔记总结.doc
文本预览下载声明
show_sql:true
hibernate.cfg.xml(aa.bb.cc移到?src
HibernateSessionFactory:location…
3)主键生成策略:sequence
assigned:应用程序指定
dao(UserInfo)
添加,删除,更新
public void saveUser(UserInfo user)throws Exception{
//1得到session
Session session=HibernateSessionFactory.getSession();
//2.声明事务
Transaction tx =session.beginTransaction();
//3.保存
session.save(user);//delete,update
//4.提交
mit();
//5.关闭
HibernateSessionFactory.closeSession();
}
根据userId得到UserInfo
public UserInfo getUserById(userId)throws Exception{
UserInfo user = null;
user =(UserInfo)session.get(UserInfo.class,userId);
return user;
}
/**
* 根据 id得到商品信息
*
* @return
*/
public String getGoodsById() {
HttpServletRequest resquest=ServletActionContext.getRequest();
String[] ids=resquest.getParameterValues(id);
for (int i = 0; i ids.length; i++)
{
try {
goods = gb.getGoodsById(Long.parseLong(ids[i]));
} catch (Exception e) {
e.printStackTrace();
}
goodslist.add(goods);
System.out.println(goodslist.size());
}
if (goodslist != null) {
return SUCCESS;
} else {
return INPUT;
}
}
/**
* 根据id得到订单信息
* @return
*/
public String getOrdersByid() {
HttpServletRequest resquest=ServletActionContext.getRequest();
String id=resquest.getParameter(id);
try {
orders = ob.getOrdersByid(Long.parseLong(id));
} catch (Exception ex) {
ex.printStackTrace();
return INPUT;
}
if(orders!=null)
{
return SUCCESS;
}else
{
return INPUT;
}
}
根据userName实现模糊查询
public ListUserInfo getUserByName(uname){
Criteria c = session.createCriteria(Userinfo.class);
c.add(Restrictions.like(“userName”,”%”+uname +”%”));
ListUserInfo userList =c.list();
return userList;
}
public ListUserInfo getUserByName(uname){
String hql=”from UserInfo u where u.userName like ?”;
Query q=session.createQuery(hql);
q.setString(0,”%”+uname+”%”);
ListUserInfo userList =q.list();return userList;
}
登陆:
public UserInfo getUserByLoginId(String loginId)
throws Exception{
UserInf
显示全部