基于Hibernate实现CRUD

这次实验,我是在一个新闻系统上做的实验。

此系统主要基础java类如图所示。

例如:

在实体类映射的文件中,配置了许多关联映射:

  以BigClass.hbm.xml和Article.hbm.xml两个配置文件为例:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

  <class name="com.xh369.dto.BigClass" table="bigclasses" dynamic-update="true" dynamic-insert="true">

    <id name="id" column="id" type="int">

      <generator class="native"/>

    </id>

    <property name="bigclassname" type="string">

      <column name="bigclassname"/>

    </property>

    <set name="article" cascade="delete" inverse="true">

      <key column="bigclassid"/>

      <one-to-many class="com.xh369.dto.Article"/>

    </set>

    <set name="smallclass" cascade="delete" inverse="true">

      <key column="bigclass_id"/>

      <one-to-many class="com.xh369.dto.SmallClass"/>

    </set>

  </class>

</hibernate-mapping>-----------------------------------------------------------------------------------------------

<hibernate-mapping>

  <class name="com.xh369.dto.Article" table="articles" dynamic-update="true" dynamic-insert="true">

     <id name="id" column="id" type="int">

      <generator class="native"/>

    </id>

    <many-to-one

      class="com.xh369.dto.BigClass"

      name="bigclass"

      column="bigclassid"

      not-null="false"

    />

    <property name="content" type="text">

      <column name="content"/>

    </property>

    <many-to-one

      class="com.xh369.dto.SmallClass"

      name="smallclass"

      column="smallclassid"

      not-null="false"

    />

    <property name="title" type="string">

      <column name="title"/>

    </property>

    <property name="deploytime" type="timestamp">

      <column name="deploytime"/>

    </property>

    <property name="picpath" type="text">

      <column name="picpath"/>

    </property>

  </class>

</hibernate-mapping>

其中,包括了双向的多对一关联映射(一个一级分类对应多个文章)和单向的一对多(一个文章对应多个二级分类)。一级分类就是大的分类,二级分类就是小的分类咯
比如人物是一级分类,作家,将军等就是二级分类

 

 

service层(以ManagerFactory.java为例):

 

import com.xh369.dao.ArticleDAO;

import com.xh369.daoimpl.ArticleDAOimpl;

import com.xh369.dao.BigClassDAO;

import com.xh369.daoimpl.BigClassDAOimpl;

import com.xh369.dao.SmallClassDAO;

import com.xh369.daoimpl.SmallClassDAOimpl;

 

public class ManagerFactory {

 

    //创建文章对象

    public static ArticleDAO createArticleDAO(){

        return new ArticleDAOimpl();

    }

 

    //创建一级分类对象

    public static BigClassDAO createBigClassDAO(){

        return new BigClassDAOimpl();

    }

 

    //创建二级分类对象

    public static SmallClassDAO createSmallClassDAO(){

        return new SmallClassDAOimpl();

    }

}

 

action层(以admin-action.java为例):

package com.xh369.actions;

 

import javax.servlet.http.*;

import com.xh369.dao.*;

import com.xh369.dto.*;

import com.xh369.service.*;

import org.apache.struts.action.*;

import org.apache.struts.actions.*;

import java.util.*;

 

public class Admin_Action extends DispatchAction{

 

    private AdminDAO adminDAO;

 

    public AdminDAO getAdminDAO(){

        return adminDAO;

    }

 

    public void setAdminDAO(AdminDAO adminDAO){

        this.adminDAO = adminDAO;

    }

 

    //登录

    public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

 

        Admin_ActionForm admin_ActionForm = (Admin_ActionForm) form;

        Admin admin = (Admin) UtilForm.populate(admin_ActionForm, "com.xh369.dto.Admin", new String[] {"username", "pwd"});

        int count = adminDAO.getLogin(admin);

        if(count > 0){

            request.getSession().setAttribute("username", admin_ActionForm.getUsername());

            return mapping.findForward("success");

        }else{

            return mapping.findForward("faile");

        }

    }

 

    //查询管理人员列表

    public ActionForward searchAdminList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

 

        List aList = adminDAO.getAdminList();

        request.setAttribute("aList", aList);

        return mapping.findForward("searchAdminList");

    }

 

    //添加管理人员

    public ActionForward addAdmin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

 

        Admin_ActionForm admin_ActionForm = (Admin_ActionForm) form;

        System.out.println("username:" + admin_ActionForm.getUsername());

        System.out.println("pwd:" + admin_ActionForm.getPwd());

        System.out.println("email:" + admin_ActionForm.getEmail());

        String[] level = admin_ActionForm.getLevel();

        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < level.length; i++){

         sb.append(level[i] + ",");

        }

        sb = sb.deleteCharAt(sb.length() - 1);

        Admin admin = (Admin) UtilForm.populate(admin_ActionForm, "com.xh369.dto.Admin", new String[] {"username", "pwd", "email"});

        admin.setLevel(sb.toString());

        if(adminDAO.insertAdmin(admin)){

            return mapping.findForward("addAdmin");

        }else{

            return mapping.findForward("faile");

        }

    }

    

    //查询用户权限

    public ActionForward searchPopedom(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

    

     Integer id = Integer.valueOf(request.getParameter("id"));

     Admin admin = adminDAO.getPopedom(id);

     List aList = adminDAO.getAllPopedom();

     request.setAttribute("admin", admin);

     request.setAttribute("id", id + "");

     request.setAttribute("aList", aList);

     return mapping.findForward("searchPopedom");

    }

    

    //修改用户权限

    public ActionForward updatePopedom(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

    

     Integer id = Integer.valueOf(request.getParameter("id"));

     StringBuffer sb = new StringBuffer();

     for(int i = 0; i < adminDAO.getAllPopedom().size(); i++){

     String level = request.getParameter("level"+i);

     if(level != null){

     int j = Integer.parseInt(level);

     sb.append( (j + 1) + ",");

     }

     }

     sb.deleteCharAt(sb.length() - 1);

     if(adminDAO.updatePopedom(id, sb)){

     return mapping.findForward("addAdmin");

     }else{

     return mapping.findForward("faile");

     }

    }

    

    //删除用户

    public ActionForward delAdmin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

    

     Admin_ActionForm admin_ActionForm = (Admin_ActionForm) form;

     String[] ids = request.getParameterValues("id");

     boolean flag = false;

     for(int i = 0; i < ids.length; i++){

     flag = false;

     Integer id = Integer.valueOf(ids[i]);

     if(adminDAO.delAdmin(id)){

     flag = true;

     }

     }

     if(flag){

     return mapping.findForward("addAdmin");

     }else{

     return mapping.findForward("faile");

     }

    }

    

    //导航到添加用户页面

    public ActionForward toAdmin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

    

     Admin_ActionForm admin_ActionForm = (Admin_ActionForm) form;

     List aList = adminDAO.getAllPopedom();

     if(aList.size() > 0){

     request.setAttribute("aList", aList);

     return mapping.findForward("toAdmin");

     }else{

     return mapping.findForward("faile");

     }

    }

}

猜你喜欢

转载自blog.csdn.net/m18846420582/article/details/80978537