The preparatory work

Resource file

  • Eclipse: Extract password: PPVG
  • Apache-tomcat-9.0.1: Extract the password: yvm2
  • Apache-maven-3.5.0: Extract password: 8haj
  • Mysql 5.7.20 mysql 5.7.20 mysql 5.7.20 mysql 5.7.20
  • Navicat+for+MySQL

Install and configure the relevant environment

  • Install database: Two ways to install mysql on a MAC
  • Maven installation and configuration in Mac OS X
  • mybatis

Create your first project

  • Maven build SpringMVC+Spring+MyBatis framework by hand
  • Create a Spring +springMVC+Mybatis project in Eclipse

Part of that

  • When creating a new project, select a Maven project. It is very convenient to manage project dependencies through Maven

  • Configuration for the new project

  • After the project is built, the structure is generally like the following figure. Of course, it can be adjusted according to their own needs.

    • “Com.xxq2dream” is the package name and “search” is the project name
    • Under the Controller package is where specific network requests are handled
    • The model is real
    • A service accesses a database and so on
    • Mapper is a specific interface to access a database. Database statements are written in the Mapper folder under Resources

  • When the project is set up and running, select “Run on Server” to access our project directly from the browser. The address is http://localhost:8080/ + project name. As I figure in the project name for “search”, then access the address is: http://localhost:8080/search/

  • In particular, there may be a problem with the name of the project if you follow the link at the beginning of this article. At the very end of the POM.xml file is the filename node, where the project name is set. Make sure you check the name Settings here and don’t just copy them.


How to write your own business logic

Add your own path

  • Settings page file paths, such as to set up the access path for http://localhost:8080/search/index

    • In the spring-mvC. XML file, set the path for storing the file
    • The controller package then writes the method to handle the request, as shown in the figure below. After retrieving the data from the database, it sets the data to the request and forwards it to the index.jsp file for processing
  • Write your own JSP file, as shown in the following code, with Person being the person added to the request above

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"% > <! DOCTYPE html PUBLIC"- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
</head>  
<body>  
    <h1>say Hello</h1>  
    <h2>${say}</h2>  
<c:if test="${person! =null}"> <h1>${person.name}</h1>  
</c:if>  
  
<c:if test="${person==null}"</h1> </c:if>  
  
</body>  
</html>  
Copy the code

Add your own database processing

  • The first step is to add the database configuration to the spring.xml file, either directly or as a separate database configuration file

  • Accessing the database

  • Interface to access the database

public interface PersonDao {	
    Person getPersonById(int id);
}
Copy the code
  • Add the business process logic interface PersonService and the concrete implementation class PersonServiceImpl
public interface PersonService {
	public Person getPersonById(int id);
}
Copy the code
  • The controller in the call
@Controller
@RequestMapping("/")
public class SearchController {
	
	@Autowired
	private PersonService personService;
	
	@RequestMapping(value="/index", produces="text/html; charset=UTF-8")
	public String getPerson(HttpServletRequest request, HttpServletResponse response){  
        Person person = personService.getPersonById(0);
        request.setAttribute("person", person);
        request.setAttribute("say"."test Say Hello");
        return "index"; }}Copy the code

The above is the first time to build SpringMVC+Spring+MyBatis framework in some processes


Welcome to follow my wechat official number, and learn and grow together with me!