初入Hibernate框架<二>
在网页中利用Hibernate框架对数据库进行增删改查
基于昨天的数据库,在网页中进行对该数据库进行增删改查
自己写Servlet继承BaseServlet,然后在里面写函数:
//DemoServlet extends BaseServlet
public abstract class BaseServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//读参数之前设置编码,读完之后就乱了
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String cmd = request.getParameter("cmd");
if(cmd==null || cmd.toString().trim().equals("")){
cmd = "execute";
}
try {
//这里的this对象其实是子类的,因为Tomcat new servlet时创建的是子类对象(StudServet,JSP页面请求的地址在web.xml中配置的是该类)
Method m = this.getClass().getMethod(cmd, HttpServletRequest.class,HttpServletResponse.class);
m.invoke(this, request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
public abstract void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
}
关于缓存问题:
每个Session有缓存,单例有可能取到同一个,去过查询请求过来,有缓存,可能查询的是历史记录(删除前的记录),想忽略缓存,直接读取数据库内容(真实的),一级缓存:每一个session都有自己的缓存,二级缓存是每一个session共享的
事务开启:
Transaction:开事务,该接口可以实现JDBC的事务,JTA中的UserTransaction,可以是CORBA事务等跨容器的事务。使用一个统一的事务操作界面,使自己的项目可以在不同的环境和容器之间方便的移值。
默认函数为查询所有:
//DemoServlet
@Override
public void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Student> list = service.queryAllStudents();
request.getSession().setAttribute("list", list);
//这里是在web.xml中配置了
String showPage = getInitParameter("queryAll");
response.sendRedirect(getServletContext().getContextPath()+showPage);
}
删除:
public void delStud(Student stud) {
// 每次操作都要获取session,不建议共享sessio,安全
Session session = HibernateSessionFactory.getSession();
// 开启事务
Transaction tran = session.beginTransaction();
// 要求给个对象
session.delete(stud);
tran.commit();
// 清理缓存
session.clear();
// 如果弄一个不存在的ID,不会删不会挂
}
添加或者修改:
public void addStud(Student stud) {
// 每次操作都要获取session,不建议共享sessio,安全
Session session = HibernateSessionFactory.getSession();
// 开启事务
Transaction tran = session.beginTransaction();
/*
* //如果存在,则至修改姓名 ID不修改 Student stud2 = (Student)
* session.get(Student.class, stud.getStudId());//缓存模式
* stud.setAge(stud2.getAge()); stud.setDeptId(stud2.getDeptId());
* //要在缓存中手动清楚stud2对象,否则会出现2份ID相同的 不同的instance,因此要删除一个 //缓存中的对象 instance
* 是用ID来标识的 session.evict(stud2);
*/
// 要求给个对象
session.saveOrUpdate(stud);// 存在则是修改,不存在添加
tran.commit();
// 清理缓存
session.clear();
}
查询:
public List<Student> queryStud(Student stud) {
//判断是否传值,办法用的有点呆,框架说明书有更简单的
boolean f1 = false, f2 = false, f3 = false;
Session session = HibernateSessionFactory.getSession();
String hql = "from Student s where 1=1 ";
if (stud.getStudId() != null && stud.getStudId().trim().length() > 0) {
hql = hql + " and s.studId = :studId";
f1 = true;
}
if (stud.getStudName() != null
&& stud.getStudName().trim().length() > 0) {
hql = hql + " and s.studName like :studName ";
f2 = true;
}
if (stud.getDeptId() != null && stud.getDeptId().trim().length() > 0) {
hql = hql + " and s.deptId =:deptId";
f3 = true;
}
//System.out.println(hql);
Query query = session.createQuery(hql);
if (f1) {
query.setParameter("studId", stud.getStudId().trim());
}
if (f2) {
query.setParameter("studName", "%"+stud.getStudName().trim()+"%");
}
if(f3){
query.setParameter("deptId", stud.getDeptId().trim());
}
//session.clear();
return query.list();
}