SQL——Hibernate SQL增删改查
1.查询list数据
实例:user login
public String userLogin(){ Session session = HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); String sqlString = "select * from userinfo where username='"+user.getUsername()+"' and password='"+user.getPassword()+"'"; ulist = session.createSQLQuery(sqlString).addEntity(UserInfo.class).list();//通过sql语句查询列表 session.close(); //使用迭代器遍历每个用户 Iterator it = ulist.iterator(); while(it.hasNext()){ user = (UserInfo) it.next(); } //控制台输出 System.out.println(ulist.size()+"--"+user.getUsername()+user.getPassword()+"-- id:"+user.getUserid()); if(ulist.size()==0){ return "ERROR"; } return "SUCCESS"; }
实例:查询所有数据
/** * persons list 显示person列表信息 */ @SuppressWarnings("unchecked") public String personList(){ Session session = HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); String hqlString = "select * from person"; plist = session.createSQLQuery(hqlString).addEntity(Person.class).list(); //获取session,存储list列表 ActionContext actionContext = ActionContext.getContext(); Map<String, Object> s = actionContext.getSession(); s.put("personList", plist); session.close(); return "SUCCESS"; }
2.通过sql添加数据
public String addPerson(){ Session session = HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); String sql = "INSERT INTO person (SELECT MAX(personid+1)," +//找出最大的id+1 赋给新的id "'"+person.getPersonname()+"'," + "'"+person.getMobilephone1()+"'," + "'"+person.getMobilephone2()+"'," + "'"+person.getSex()+"'," + "'"+person.getBirthday()+"'," + "'"+person.getPnative()+"'," + "'"+person.getAddress()+"'," + "'"+person.getEmail()+"'," + "'"+person.getDescribe()+"'," + +person.getGroupid()+ " FROM person)"; session.createSQLQuery(sql).executeUpdate();//执行更新操作 tran.commit(); System.out.println(person.getPersonid()+"--"+person.getPersonname()); session.close(); return "SUCCESS"; }
3.根据id查找数据
/** * 根据id找到person * @param id * @return */ public String findPersonById(){ int personid = Integer.parseInt(ServletActionContext.getRequest().getParameter("id")); Session session = HibernateSessionFactory.getSession(); session.load(person, personid);//根据id查询一条记录 //获取session,存储list列表 ActionContext actionContext = ActionContext.getContext(); Map<String, Object> s = actionContext.getSession(); s.put("person", person); session.close(); return "SUCCESS"; }
4.删除一条记录
/** * 根据id删除person * @return */ public String deletePerson(){ int personid = Integer.parseInt(ServletActionContext.getRequest().getParameter("id")); person.setPersonid(personid); Session session = HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); session.delete(person); tran.commit(); return "SUCCESS"; }
5.更新一条记录
/** * 根据id更新person * @return */ public String updatePerson(){ Session session = HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); String sql = "UPDATE person SET " + "personname='"+person.getPersonname()+"'," + "mobilephone1='"+person.getMobilephone1()+"'," + "mobilephone2='"+person.getMobilephone2()+"'," + "sex='"+person.getSex()+"'," + "birthday='"+person.getBirthday()+"'," + "native='"+person.getPnative()+"'," + "address='"+person.getAddress()+"'," + "email='"+person.getEmail()+"'," + "pdescribe='"+person.getDescribe()+"',"+ "groupid="+person.getGroupid()+ " WHERE personid=" +person.getPersonid(); session.createSQLQuery(sql).executeUpdate(); tran.commit(); session.close(); return "SUCCESS"; }
逃避不一定躲得过,面对不一定最难过