2011-08-31 58 views
0

我做了一些小項目 還有隻是添加,刪除,顯示列表功能 添加和刪除功能不錯,但是獲取列表功能都不起作用。 (在我的項目memoservice.getAll()函數沒有很好地工作) ,所以我試圖讓列表這樣,關於控制器和道功能的問題

List <Memo> mem = getAll(); 

但目前還沒有返回值; 什麼是我的項目的問題。幫助我PLZ!

一些代碼在這裏。

MemoController.java

package springbook.sug.web; 

import javax.inject.Inject; 
import javax.inject.Provider; 
import javax.validation.Valid; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.support.SessionStatus; 

import springbook.sug.domain.Memo; 
import springbook.sug.service.MemoService; 
import springbook.sug.web.security.LoginInfo; 

@Controller 
@RequestMapping("/memo") 
public class MemoController { 
    @Autowired 
    private MemoService memoService; 

    private @Inject Provider<LoginInfo> loginInfoProvider; 

    @RequestMapping("/list") 
    public String list(ModelMap model) { 
     model.addAttribute(this.memoService.getAll()); 
     return "memo/list"; 
    } 

    @RequestMapping("/list/{userId}") 
    public String list(@PathVariable int userId, ModelMap model) { 
     model.addAttribute(this.memoService.get(userId)); 
     return "memo/list"; 
    } 

    @RequestMapping("/delete/{memoId}") 
    public String delete(@PathVariable int memoId) { 
     this.memoService.delete(memoId); 
     return "memo/deleted"; 
    } 

    @RequestMapping("/add/") 
    public String showform(ModelMap model) { 
     Memo memo = new Memo(); 
     model.addAttribute(memo); 
     return "memo/add"; 
    } 

    @RequestMapping(method=RequestMethod.POST) 
    public String add(@ModelAttribute @Valid Memo memo, BindingResult result, SessionStatus status) { 
     if (result.hasErrors()) { 
      return "list"; 
     } 
     else { 
      this.memoService.add(memo);    
      status.setComplete(); 
      return "redirect:list"; 
     } 
    } 
} 

MemoService.java運行代碼時

package springbook.sug.service; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 

import springbook.sug.dao.MemoDao; 
import springbook.sug.domain.Memo; 

@Service 
@Transactional 
public class MemoServiceImpl implements MemoService{ 
    private MemoDao memoDao; 

    @Autowired 
    public void setMemoDao(MemoDao memoDao) { 
     this.memoDao = memoDao; 
    } 

    public Memo add(Memo memo) { 
     memo.initDates(); 
     return this.memoDao.add(memo); 
    } 

    public void delete(int memoId) { 
     this.memoDao.delete(memoId); 
    } 

    public Memo get(int userId) { 
     return this.memoDao.get(userId); 
    } 

    public Memo update(Memo memo) { 
     return this.memoDao.update(memo); 
    } 

    public List<Memo> getAll() { 
     return this.memoDao.getAll(); 
    } 
} 

MemoDao.java

package springbook.sug.dao; 

import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.List; 

import javax.sql.DataSource; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.dao.EmptyResultDataAccessException; 
import org.springframework.jdbc.core.RowMapper; 
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; 
import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; 
import org.springframework.stereotype.Repository; 

import springbook.sug.domain.Memo; 

@Repository 
public class MemoDaoJdbc implements MemoDao { 
    private SimpleJdbcTemplate jdbcTemplate; 
    private SimpleJdbcInsert memoInsert; 

    private RowMapper<Memo> rowMapper = 
      new RowMapper<Memo>() { 
       public Memo mapRow(ResultSet rs, int rowNum) throws SQLException { 
       Memo memo = new Memo(); 
       memo.setMemoId(rs.getInt("memoId")); 
       memo.setMemoContents(rs.getString("memoContents")); 
       memo.setMemoRegister(rs.getString("memoRegister")); 
       memo.setCreated(rs.getDate("created")); 

       return memo; 
     } 
    }; 

    @Autowired 
    public void init(DataSource dataSource) { 
     this.jdbcTemplate = new SimpleJdbcTemplate(dataSource); 
     this.memoInsert = new SimpleJdbcInsert(dataSource) 
         .withTableName("memos") 
         .usingGeneratedKeyColumns("memoId"); 
    } 

    public Memo add(Memo memo) { 
     int generatedId = this.memoInsert.executeAndReturnKey(
       new BeanPropertySqlParameterSource(memo)).intValue(); 
     memo.setMemoId(generatedId); 
     return memo; 
    } 

    public Memo update(Memo memo) { 
     int affected = jdbcTemplate.update(
       "update memos set " + 
       "memoContents = :memoContents, " + 
       "memoRegister = :memoRegister, " + 
       "created = :created, " + 
       "where memoId = :memoId", 
       new BeanPropertySqlParameterSource(memo)); 
     return memo; 
    } 

    public void delete(int memoId) { 
     this.jdbcTemplate.update("delete from memos where memoId = ?", memoId); 
    } 

    public int deleteAll() { 
     return this.jdbcTemplate.update("delete from memos"); 
    } 

    public Memo get(int memoId) { 
     try { 
     return this.jdbcTemplate.queryForObject("select * from memos where memoId = ?", 
       this.rowMapper, memoId); 
     } 
     catch(EmptyResultDataAccessException e) { 
      return null; 
     } 
    } 

    public List<Memo> search(String memoRegister) { 
     return this.jdbcTemplate.query("select * from memos where memoRegister = ?", 
       this.rowMapper, "%" + memoRegister + "%"); 
    } 

    public List<Memo> getAll() { 
     return this.jdbcTemplate.query("select * from memos order by memoId desc", 
       this.rowMapper); 
    } 

    public long count() { 
     return this.jdbcTemplate.queryForLong("select count(0) from memos"); 
    } 
} 

回答

0

您是否獲得一個例外?如果是這樣,則需要堆棧跟蹤。如果不是,你的表中有數據可以返回嗎?你如何將數據源注入你的dao實現?你的應用程序上下文可能有幫

+0

感謝您的回答鮑比·菲捨爾。它運行良好,沒有任何例外。而且,表中有8個數據。在XML中,我從XML文件注入數據源。 – masa

+0

添加,刪除,得到(INT ID)機能的研究進展順利,但在memoService.java,列表 GETALL()函數的問題... – masa

+0

在像this.' <上下文XML文件:屬性佔位符的位置=「類路徑:/database.properties 「/><豆ID =」 數據源」類= 「org.apache.commons.dbcp.BasicDataSource」><屬性名= 「driverClassName」 值= 「$ {database.driverClass}」/><屬性名稱= 「URL」 值= 「$ {database.url}」/><屬性名= 「username」 的值= 「$ {database.username}」/><屬性名= 「密碼」 的值=「$ {數據庫。密碼}「/>' – masa