Java 如何在dao类中使用从表单获得的值作为变量?

Java 如何在dao类中使用从表单获得的值作为变量?,java,spring-boot,spring-mvc,jdbc,Java,Spring Boot,Spring Mvc,Jdbc,我正在尝试创建一个页面,在该页面上键入类似java的兴趣,然后从数据库中获取所有与java相关的行,因此我创建了这个表单元素 <input class="form-control" type="text" placeholder="Search..." name="interest"><br> 这是我在控制器中的映射 @ModelAttribute("courses"

我正在尝试创建一个页面,在该页面上键入类似java的兴趣,然后从数据库中获取所有与java相关的行,因此我创建了这个表单元素

<input class="form-control" type="text" placeholder="Search..." name="interest"><br>

这是我在控制器中的映射

@ModelAttribute("courses")
    public Map<String, String> getCourseList() throws SQLException, IOException {

        return (new ServiceImpl()).getCourseList();
    }
    
    @RequestMapping(value="showCourses", method=RequestMethod.POST)
    public ModelAndView showCourses(@RequestParam("interest") String interest) {
        ModelAndView mv=new ModelAndView();
        mv.addObject("interest",interest);
        mv.setViewName("showCourses");
        return mv;
    }
@modeldattribute(“课程”)
公共映射getCourseList()引发SQLException,IOException{
return(newServiceImpl()).getCourseList();
}
@RequestMapping(value=“showCourses”,method=RequestMethod.POST)
公共模型和视图展示课程(@RequestParam(“兴趣”)字符串兴趣){
ModelAndView mv=新ModelAndView();
mv.addObject(“利息”,利息);
mv.setViewName(“showCourses”);
返回mv;
}
我正在从数据库中获取所有课程,但在我的daoimpl类中

public Map<String, String> courseList() throws SQLException, IOException {
        connect();
        Map<String, String> courseList = new HashMap<String, String>();
        String sql = "SELECT * from course;";
        resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            courseList.put(resultSet.getString("CourseName"), resultSet.getString("CourseName"));
        }
        return courseList;
    }
公共映射courseList()抛出SQLException,IOException{ connect(); Map courseList=新建HashMap(); String sql=“从课程中选择*”; resultSet=statement.executeQuery(sql); while(resultSet.next()){ courseList.put(resultSet.getString(“CourseName”)、resultSet.getString(“CourseName”); } 返回课程列表; }
在这个courseList方法中,我想使用兴趣,这样我只能获取特定兴趣的课程,并在我的jsp上显示它们。如何在我的daoImpl类中使用兴趣?

请查看下面的代码片段

您已经实现了dao,只需对该方法稍加修改:-

public Map<String, String> getCourseList(String subjectInterest) {
     connect();
     Map<String, String> courseList = new HashMap<String, String>();
     String sql = "SELECT * from course where subInterest = ?";
     statement.setString(1,subjectInterest);
     resultSet = statement.executeQuery(sql);
     while (resultSet.next()) {
        courseList.put(resultSet.getString("CourseName"), resultSet.getString("CourseName"));
     }
     return courseList;
}
publicmap getCourseList(字符串subjectInterest){
connect();
Map courseList=新建HashMap();
String sql=“从课程中选择*,其中子兴趣=?”;
语句.设置字符串(1,主观性测试);
resultSet=statement.executeQuery(sql);
while(resultSet.next()){
courseList.put(resultSet.getString(“CourseName”)、resultSet.getString(“CourseName”);
}
返回课程列表;
}
除此之外,您还需要实现一个服务类,从中可以调用dao方法获取课程详细信息,如下所示:-

@Service
public class CourseService {
    @Autowired
    private CourseDAO courseDao;

    public Map<String, String> getCourseList(String subjectInterest) throws SQLException, IOException {
        return courseDao.getCourseList(subjectInterest);
    }
}
@Controller
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/showCourses", method = RequestMethod.POST)
    public String showCourses(@RequestParam("interest") String interest, Model model) throws SQLException, IOException {
        model.addAttribute("interest", interest);
        Map<String, String> courseList = courseService.getCourseList(interest);
        model.addAttribute("courseList", courseList);
        return "showCourses";
    }

}
@服务
公共课程服务{
@自动连线
私人球场;
公共映射getCourseList(String subjectInterest)抛出SQLException、IOException{
返回courseDao.getCourseList(主客观测试);
}
}
在您的控制器中发布此信息,需要进行一些更改,如下所示:-

@Service
public class CourseService {
    @Autowired
    private CourseDAO courseDao;

    public Map<String, String> getCourseList(String subjectInterest) throws SQLException, IOException {
        return courseDao.getCourseList(subjectInterest);
    }
}
@Controller
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/showCourses", method = RequestMethod.POST)
    public String showCourses(@RequestParam("interest") String interest, Model model) throws SQLException, IOException {
        model.addAttribute("interest", interest);
        Map<String, String> courseList = courseService.getCourseList(interest);
        model.addAttribute("courseList", courseList);
        return "showCourses";
    }

}
@控制器
公共类课程控制器{
@自动连线
私人课程服务;
@RequestMapping(value=“/showcources”,method=RequestMethod.POST)
公共字符串showcources(@RequestParam(“interest”)字符串interest,Model Model)抛出SQLException,IOException{
model.addAttribute(“利息”,利息);
Map courseList=courseService.getCourseList(兴趣);
model.addAttribute(“课程列表”,课程列表);
返回“展示课程”;
}
}

最后,您需要在showcources.jsp中循环courseList属性,以便能够根据兴趣显示课程。

不明白问题出在哪里。但首先,我假设
serviceinpl
是一个Springbean,不应该直接实例化。而是自动连接bean。第二,要在您的dao中使用查询参数,只需提供方法参数OK我将把ServiceImpl作为一个Bean并自动连接它,但是如何将我在控制器中请求的兴趣传递给dao?就像方法参数一样