2016-01-19 22 views
0

我正在學習DAO和服務,並試圖爲一些測試(JSP)製作一個非常簡單的Web應用程序。我創建了DAO和服務類,但我怎樣才能讓它們彼此合作?我希望爲了在textfield中輸入的值將被保存在我的數據庫中。下面是一個代碼:Spring MVC - 如何將控制器與服務類和DAO連接起來?

StudentDAO:

public void create(Student student); 

StudentDAOImpl:

private JdbcTemplate jdbcTemplate; 

DataSource dataSource; 

public JdbcTemplate getJdbcTemplate() { 
    return jdbcTemplate; 
} 

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 
    this.jdbcTemplate = jdbcTemplate; 
} 

public void setDataSource(DataSource dataSource) { 
    this.dataSource = dataSource; 
} 

public void create(Student student) { 
    String query = "insert into studentdb.student (`name`, `age`) values (?,?)"; 
    Connection con = null; 
    PreparedStatement ps = null; 
    try{ 
     con = dataSource.getConnection(); 
     ps = con.prepareStatement(query); 
     ps.setString(1, student.getName()); 
     ps.setString(2, student.getAge()); 
     int out = ps.executeUpdate(); 

    }catch(SQLException e){ 
     e.printStackTrace(); 
    }finally{ 
     try { 
      ps.close(); 
      con.close(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

StudentService長相酷似StudentDAO一樣,這裏是StudentServiceImpl:

StudentDAO studentDAO; 


public void createStudent(Student student) { 

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("studentconfig.xml"); 

    studentDAO = ctx.getBean("studentDAO", StudentDAO.class); 

    student.setName(//value entered in webapp); 
    student.setAge(//value entered in webapp); 

    studentDAO.create(student); 
} 

,這裏是MainController :

@RequestMapping(value="/home.html", method = RequestMethod.POST) 
public ModelAndView homePagePost(Model model, @ModelAttribute("student") Student student1){ 

    model.addAttribute("student", student1); 
    studentService.createStudent(student1); 

    ModelAndView home = new ModelAndView("index"); 
    return home; 
} 

的index.jsp:

<html> 
<body> 
<h2>Hello World!</h2> 
<form action="/webapplication/home.html" method="post"> 
<input type="text" name="${student.name}"/> 
<input type="text" name="${student.age} }"/> 
<input type="submit" value="confirm!"/> 
</form> 
</body> 
</html> 

回答

0

您需要的DI(依賴注入),或者更精確地執行這 - 控制反轉。

閱讀關於此Spring DISpring IoC containers

然後你需要指明要自動裝配類到另一種註解,如:

@RestController 
public class LoginController { 
    @Autowired 
    private LoginService loginService; 

之後,你需要聲明應用程序上下文掃描初始化所有類和注入這樣所有依賴哪些類:

<context:component-scan base-package="controller" /> 
<context:component-scan base-package="service" /> 
<context:component-scan base-package="dao" /> 

所以,當你將部署應用程序部署描述符將與應用程序上下文來解析,然後用掃描類包及其依賴Spring bean容器將初始化所有這CLASSE由IoC原則從最後到開始注入這些到另一個。

一般來說,你需要這樣做,也許我已經跳過了一些東西,但這是原則。

+0

它對你有幫助嗎? –

0

如上所述,一個好的解決方案是彈簧依賴注入。

下面是一些流線型的信息: https://www.youtube.com/watch?v=6F3Cv1a7G0w

這是你StudentDao與實施:

public interface StudentDao { 

} 

@Component 
public class StudentDaoImpl implements StudentDao { 
    //TODO Your Code 
} 

這是你StudentService與實施:

@Service 
public interface StudentService { 

} 

@Component 
public class StudentServiceImpl implements StudentService { 

    @Autowired 
    private StudentDao studentDao; 

} 

這是你StudentController與實現:

@Controller 
public interface StudentController { 

} 

@Component 
public class StudentControllerImpl implements StudentController { 

    @Autowired 
    private StudentService studentService; 

} 

這是你的應用程序類(如果你使用SpringBoot):

@SpringBootApplication 
@ComponentScan("com.your.packages.controller", "com.your.packages.service", "com.your.packages.dao") 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

@ComponentScan會告訴指示春天來搜索與@Component,@Service,@Controller和其他註解類指定的包。

相關問題