SSM framework building and learning (2) --- integration of MyBatis and SpringMVC,
After setting up the Mybatis environment, we can use Java Code to get the data we want from the database, but a complete project must have a front-end, so the communication between the front-end and the back-end Java will use the SpringMVC framework we are familiar with. With SpringMVC, we can submit the front-end data to Java Contoller, then, use Mybatis to enter the database to query the desired data and return to the front-end.
So there are both Mabatis and SpringMVC, we need to find a way to integrate the two, that is, the integration of configuration files.
1. Let's take a look at SpringMVC? The mvc framework provided by a product in the spring framework implements functions similar to struts. Main functions: Collect form information, process data, and redirect after processing.
2. Start.
1) first download the jar package of Spring.
Address: http://maven.springframework.org/release/org/springframework/spring/
After downloading the jar package, you can add the nine jar packages. If you want to add other jar packages later.
2) Prepare the springmvc environment and configure web. xml.
Spring mvc is based on servlet, so you need to configure DispatcherServlet in web. xml!
Add these two tags to web. xml
Pay special attention to servlet-name, which is used to create the springmvc configuration file.
3) create springmvc configuration file WEB-INF under spring-servlet.xml
The name of spring must be the same as that of servlet-name in web. xml!
Where:
A. annotation-driven <mvc: annotation-driven/>
B. Set the package name of the controller scan path. spring mvc will automatically instantiate the controller under the package. The controller is equivalent to the action in struts. It is used to process data and complete forwarding.
<Context: component-scan base-package = "javastudy"/>
C. Other configurations are the same as those of spring.
III. in this way. the springMVC configuration file is ready. After SpringMVC is added, the view is OK. however, this is not enough. To be able to run normally, the Spring framework is essential. In the next chapter, let's talk about how to integrate Spring to form the final SSM framework!