关于controller和dao函数的问题

发布于 2024-12-02 06:54:18 字数 6513 浏览 1 评论 0原文

我做了一些小项目 只有添加、删除、显示列表功能 添加和删​​除功能很好用,但获取列表功能不起作用。 (在我的项目中 memoservice.getAll() 函数不能很好地工作) 所以我尝试获取这样的列表,

List <Memo> mem = getAll();

但没有返回值; 我的项目有什么问题。请帮助我!

一些代码在这里。

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");
    }
}

i made some small project
there are just add, delete, showlist functions
add and delete functions good work but get list functions are didn't work.
(in my project memoservice.getAll() function didn't work well)
so i try to get lists like this,

List <Memo> mem = getAll();

but there are no return values;
what's the problem in my project. help me plz!

some codes are here.

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");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小梨窩很甜 2024-12-09 06:54:18

运行代码时是否出现异常?如果是这样,则需要堆栈跟踪。如果没有,你的表中有数据要返回吗?你如何将数据源注入到你的 dao 实现中?您的应用程序上下文可能会有所帮助。

Are you getting an exception when running the code? If so, stack trace is needed. If not, do you have data in your table to return? How are you injecting datasource into your dao implementation? Your application context might help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文