SpringMVC Framework Overview:
SpringMVC is a lightweight Web framework based on the Request-driven type of Java implementation MVC design model. It uses a set of annotations to make a simple Java class a controller for handling requests without implementing any interfaces.
preparation
1. Edit the pom. XML file to add Spring Web dependencies and Thymeleaf dependencies
Web dependencies (provides the Spring MVC core API and is embedded with a Tomcat server)
<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>Copy the code
Thymeleaf dependency (provides a view resolver object and data binding mechanism)
<! > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>Copy the code
2. Add the view resolver configuration in application.properties
Port =80 # Spring thymeleaf # Change the page without restarting the server Spring. Thymeleaf. Cache =false spring.thymeleaf.prefix=classpath:/templates/pages/ spring.thymeleaf.suffix=.htmlCopy the code
3.Spring MVC coding implementation
3.1 Creating a Project
3.2 Compiling the Goodsmapper. XML file
<? The XML version = "1.0" encoding = "utf-8"? > <! DOCTYPE mapper PUBLIC "- / / mybatis.org//DTD mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < mapper namespace="com.cy.pj.goods.dao.GoodsDao"> <! - query all users information - > < select id = "the.findall resultType" = "com. Cy. Pj. Goods. Utils. User" > select * from emp < / select > < / mapper >Copy the code
3.3 Writing the GoodsDao interface
package com.cy.pj.goods.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.cy.pj.goods.utils.User; /** * @mapper is used to describe (mark) the data layer access interface. It is used to tell the Mybatis framework * that the interface described using this annotation needs to be created by the underlying implementation class, which implements the interaction with the database based on the Mybatis API. The objects of this class will eventually be handed over to Spring. */ @mapper public interface GoodsDao {/** * return List */ public List<User> findAll(); }Copy the code
3.4 Writing GoodsService interface
package com.cy.pj.goods.service; import java.util.List; import com.cy.pj.goods.utils.User; /** * User module business layer interface, * @author BigData ** / public interface GoodsService {/** * public List<User> findAll(); }Copy the code
3.5 Writing the GoodsServiceImpl class
This class implements the GoodsService interface for the concrete implementation of the business for the user module
package com.cy.pj.goods.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.cy.pj.goods.dao.GoodsDao; import com.cy.pj.goods.service.GoodsService; import com.cy.pj.goods.utils.User; /** * Business layer object, * @author BigData */ @service public class GoodsServiceImpl implements GoodsService{@autoWired Private GoodsDao goodsDao; @Override public List<User> findAll() { return goodsDao.findAll(); }}Copy the code
3.6 Writing the CoodsController class
package com.cy.pj.goods.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.cy.pj.goods.service.GoodsService; import com.cy.pj.goods.utils.User; /* * @controller: If the current class is in a package that is configured with Spring package scanning, the class with the * annotation will be registered with the Spring container as a bean, and the Spring container will create the instance. */ @Controller @RequestMapping("/show/") public class GoodsController { @Autowired private GoodsService goodsService; / * * @ RequestMapping: Configure an access path for the current method * If the Controllor class has no access path configured, */ @requestMapping ("findAll") public String findAll(Model Model) {List<User> lists = goodsService.findAll(); model.addAttribute("list", lists); return "show"; }}Copy the code
3.7 Creating AN HTML Page
Create show.html in the templates/ Pages directory to display the data
3.7 Editing AN HTML Page
<! DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>The Show Page</h1> <table> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>salary</th> </tr> </thead> <tbody> <tr th:each="l:${list}"> <td th:text="${l.id}">1</td> <td th:text="${l.name}">Tom</td> <td th:text="${l.age}">18</td> <td Th: text = "${l.s alary}" > 5000.0 < / td > < / tr > < / tbody > < / table > < / body > < / HTML >Copy the code
Output and display
Here, the integration of the framework is over, thank you for your reference, there are insufficient places, please leave your valuable opinions!!
Source: www.tuicool.com/articles/U7…