SpringSide】dml簡單版

=========================================================【修改】=====================================================================

方法一:根據id改變他的狀態。就一個字段時。

首先循環去的id,然後在他的action裏遍歷的根據id(要修改的字段的id)查出實體對象,並且賦值setisnotice字段,最後save

即:find——》set——》save

String[] IdArray=ids.split(",");

                            for(String tempId:IdArray){                                 

                                     Article temp=articleService.getArticleById(Long.parseLong(tempId));

                                     temp.setIsnotice(1L);

                                     articleService.saveArticle(temp);

                            }

方法二:

action裏面:

         public String changeIsnotice(){                 

                   try {

                            String[] IdArray=ids.split(",");

                            for(String tempId:IdArray){                                 

                                     articleService.updateIsnoticeState(tempId);

                            }

                   } catch (Exception e) {

                            logger.error("顯示出錯!",e);

                   }

                   return RELOAD;

         }

service層裏:

public void updateIsnoticeState(String isnoticeIds)

    {

                   System.out.println("updateIsnoticeState........ik");

       String hql = "update Article a set a.isnotice = 1 where a.articleid in ("+isnoticeIds+")";

       Query query = this.articleDao.createQuery(hql);

       query.executeUpdate();[d1]             

      

    }

 

方式二:【根據id修改所有的字段,並且彈出一個新頁面】

頁面傳入的值:

<form id="mainForm" action="user.action" method="get">

                            <s:iterator value="page.result">

                                               <tr>

                                                        <td>${loginName}&nbsp;</td>

                                                        <td>${name}&nbsp;</td>

                                                        <td>${email}&nbsp;</td>

                                                        <td>${roleNames}&nbsp;</td>

                                                        <td>&nbsp;

                                                                           <a href="studentAction!input.action?id=${id}">修改</a>&nbsp;[d2] 

                                                                           <a href="studentAction!delete.action?id=${id}">刪除</a>

                                                                

                                                        </td>

                                               </tr>

                                     </s:iterator>   

</form>

==========================================================【刪除】====================================================================

頁面傳入的值

action內的值:

@Override

         public String delete() throws Exception {

                   try {

                            accountManager.deleteUser(id);

                            addActionMessage("刪除用戶成功");[d3] 

                   } catch (Exception e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                            addActionMessage("刪除用戶失敗");

                   }

                   return RELOAD;

         }

// 添加刪除操作要用這個封裝數據

         @Override

         protected void prepareModel[d4] () throws Exception {

                   if (id != null) {

                            entity = accountManager.getUser(id);

                   } else {

                            entity = new User();

                   }

                  

         }

 

service內的值:

/**

          * 刪除用戶,如果嘗試刪除超級管理員將拋出異常.

          */

         public void deleteUser(Long id) {

                   if (isSupervisor(id)) {

                            logger.warn("操作員{}嘗試刪除超級管理員用戶", SpringSecurityUtils.getCurrentUserName());

                            throw new ServiceException("不能刪除超級管理員用戶");

                   }

                   userDao.delete(id);[d5] 

         }

 

 

dao內的值:

 

==========================================================【添加】====================================================================

頁面傳入的值:

<a href="${ctx}/account/studentAction!input.action">增加新用戶</a>

 

action內的值:

@Override

         public String save() throws Exception {

                   accountManager.saveUser(entity);

                   return RELOAD;

         }

 

 

service內的值:

public void saveUser(User entity) {

                   userDao.save(entity);

         }

 

dao內的值:

 

==========================================================【查詢】====================================================================

頁面傳入的值

 

action內的值:

 

 

service內的值:

//第一次頁面加載所有數據

         public List<Node> getAllNode(){     

                   return nodeDao.getAll();

         }

 

/**

          * 使用屬性過濾條件查詢用戶.

          */

         @Transactional(readOnly = true)

         public Page<User> searchUser(final Page<User> page, final List<PropertyFilter> filters) {

                   return userDao.findPage(page, filters);

         }

 

         @Transactional(readOnly = true)

         public User findUserByLoginName(String loginName) {

                   return userDao.findUniqueBy("loginName", loginName);

         }

 

 

dao內的值:

 

==========================================================【動態加載頁面】============================================================

頁面傳入的值

 

action內的值:

         private Page<User> page = new Page<User>(5);//每頁5條記錄

         page = accountManager.searchUser(page, filters);//保存到page裏面

 

service內的值:

         public Page<User> searchUser(final Page<User> page, final List<PropertyFilter> filters) {

                   return userDao.findPage(page, filters);

         }

dao內的值:

         public class UserDao extends HibernateDao<User, Long>

jsp頁面的值:

         <s:iterator value="page.result">//默認的是result屬性。表示集合裏的值

 

===========================================================【底層dao============================================================

1)返回page分頁:     busiGoodsDAO.findPage(page,hql);

2)返回list集合:         busiGoodsDAO.createQuery(hql).list();[d6] 

3)返回String不重複:   this.busiGoodsDAO.createQuery(hql).uniqueResult().toString();[d7] 

4)返回void                 this.busiGoodsDAO.createQuery(hql);

                            query.executeUpdate();

 

 

 

 

 

 

 

 

 

 


 [d1]更新操作。Sql語句

 [d2]超鏈接,傳id

 [d3]

 [d4]初始化數據,就實現了根據id獲得整個實體類

 [d5]調用daodelete方法,只需要傳入id即可,不需要寫語句

 [d6]

 [d7]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章