SpringBoot integrates MVC

2021-03-27

Contents:
One SpringBoot integration SpringMVC
two SpringBoot simple case based on SpringMVC and Mybatis

A SpringBoot integrates SpringMVC

The first step: add MVC dependency

添加Spring Web依赖
<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Add Thymeleaf dependency (provides a view resolver object and data binding mechanism)
Web dependency (provides Spring MVC core API, and will also embed a Tomcate server)

Among them: Spring Web Starter provides Soring MVC dependency support and Spring MVC core API. A tomcate dependency will be automatically added for use as an embedded web server.
Thymeleaf is an html template engine that provides an API integrated with SpringMVC and can be used as the View layer of web applications in the MVC framework.

Note: SpringBoot no longer supports jsp. If you want to use jsp, you need to configure it in xml.

Step 2: Configure Spring MVC core objects

1: Now create the templates/pages directory under the src/main/resources directory

2: Add the view parser configuration to the application.properties file (it will also be configured if there is no default. In the default configuration, the prefix defaults to classpath:/templates/, and the suffix defaults to .html)

spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html

Insert picture description here

Step 3: Create the Controller class

Write the XxxController (GoodsController) class and give it to spring to manage. Such a Controller is usually called Handler (Processor-Model) in the SpringMVC specification, and we sometimes understand this object as a back-end controller in the enterprise (in terms of rigor, it is not standardized enough)
Insert picture description here

package com.cy.pj.goods.controller;
@Controller
	
@RequestMapping("/goods/")
public class GoodsController {
    
    
     @RequestMapping("doGoodsUI")
public String doGoodsUI() {
    
    
	   return "goods";
}
}

Step 4: Need to create goods.html in the /tmplates/pages/ directory

Step 5: Start the server (the default project is embedded in the default server provided by Spring web Starter), open the browser for access testing

SpringBoot integrates Mybatis+MVC and uses Thymeleaf template engine to query related information from the database and display it on the page. Simple project case

Take STS software as an example

Step 1: Import Import the sql file into the database

Open the dos command window to import and open MySQL to import the sql file. The
Insert picture description here
code is as follows

1: Log in to mysql

mysql –uroot –proot

2: Set the client console encoding (MySql client) mode

set names utf8;

3: Execute the goods.sql file

source d:/goods.sql

The sql file is as follows

drop database if exists dbgoods;
create database dbgoods default character set utf8;
use dbgoods;
create table tb_goods(
     id bigint primary key auto_increment,
     name varchar(100) not null,
     remark text,
     createdTime datetime not null
)engine=InnoDB;
insert into tb_goods values (null,'java','very good',now());
insert into tb_goods values (null,'mysql','RDBMS',now());
insert into tb_goods values (null,'Oracle','RDBMS',now());
insert into tb_goods values (null,'java','very good',now());
insert into tb_goods values (null,'mysql','RDBMS',now());
insert into tb_goods values (null,'Oracle','RDBMS',now());
insert into tb_goods values (null,'java','very good',now());
insert into tb_goods values (null,'mysql','RDBMS',now());
insert into tb_goods values (null,'Oracle','RDBMS',now());
insert into tb_goods values (null,'java','very good',now());
insert into tb_goods values (null,'mysql','RDBMS',now());
insert into tb_goods values (null,'Oracle','RDBMS',now());



Step 2: Create the project

Insert picture description here

Step 3: Add related dependencies

Import Mybatis related dependencies
1 mysql driver package

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>

2 spring jdbc dependency

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

3 mybatis starter dependency

<dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.1</version>
</dependency>
注意:mybatis starter 依赖的版本可能不会自动给,需要手动添加!!!

Import MVC related dependencies
1 Web dependencies

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
</dependency>

2 Thymeleaf dependency

<dependency>
	   <groupId>org.springframework.boot</groupId>
	   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Step 4: Add a simple configuration to the application.propertis configuration file

Connection pool configuration

// serverTimezone=GMT%2B8 为设置时区为东八区
// characterEncoding=utf8设置编码格式为utf-8
spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8
// 自己数据库的用户名
spring.datasource.username=root
// 自己数据库的密码
spring.datasource.password=root

Add the view resolver configuration in the application.properties file (if there is no configuration, it will be configured by default. In the default configuration, the prefix default value is classpath:/templates/, and the suffix defaults to .html)

spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html

the fifth step

Create the Pojo class The
Insert picture description here
code is as follows

package com.cy.goods.Pojo;                                                                                                                                    
                                                                                                                                                              
import java.sql.Date;                                                                                                                                         
                                                                                                                                                              
public class Goods {
    
                                                                                                                                              
	private Integer id;                                                                                                                                       
	private String name;                                                                                                                                      
	private String remark;                                                                                                                                    
	private Date createdTime;                                                                                                                                 
	                                                                                                                                                          
	public Goods() {
    
                                                                                                                                              
		super();                                                                                                                                              
	}                                                                                                                                                         
	                                                                                                                                                          
	public Goods(Integer id, String name, String remark, Date createdTime) {
    
                                                                                      
		super();                                                                                                                                              
		this.id = id;                                                                                                                                         
		this.name = name;                                                                                                                                     
		this.remark = remark;                                                                                                                                 
		this.createdTime = createdTime;                                                                                                                       
	}                                                                                                                                                         
                                                                                                                                                              
	public Integer getId() {
    
                                                                                                                                      
		return id;                                                                                                                                            
	}                                                                                                                                                         
                                                                                                                                                              
	public void setId(Integer id) {
    
                                                                                                                               
		this.id = id;                                                                                                                                         
	}                                                                                                                                                         
                                                                                                                                                              
	public String getName() {
    
                                                                                                                                     
		return name;                                                                                                                                          
	}                                                                                                                                                         
                                                                                                                                                              
	public void setName(String name) {
    
                                                                                                                            
		this.name = name;                                                                                                                                     
	}                                                                                                                                                         
                                                                                                                                                              
	public String getRemark() {
    
                                                                                                                                   
		return remark;                                                                                                                                        
	}                                                                                                                                                         
                                                                                                                                                              
	public void setRemark(String remark) {
    
                                                                                                                        
		this.remark = remark;                                                                                                                                 
	}                                                                                                                                                         
                                                                                                                                                              
	public Date getCreatedTime() {
    
                                                                                                                                
		return createdTime;                                                                                                                                   
	}                                                                                                                                                         
                                                                                                                                                              
	public void setCreatedTime(Date createdTime) {
    
                                                                                                                
		this.createdTime = createdTime;                                                                                                                       
	}                                                                                                                                                         
	                                                                                                                                                          
}                                                                                                                                                             
                                                                                                                                                              

Sixth step

Create Dao
Insert picture description here

code show as below

package com.cy.goods.Dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.cy.goods.Pojo.Goods;

@Mapper
public interface GoodsDao {
    
    
	@Select("select * from tb_goods")
	List<Goods> findAll();
}

Seventh step

Create Service
Insert picture description here

code show as below

package com.cy.goods.Servlet;

import java.util.List;

import com.cy.goods.Pojo.Goods;

public interface GoodsService {
    
    
	List<Goods> findGoods();
}

Eighth step

Create ServiceImpl
Insert picture description here

code show as below

package com.cy.goods.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.goods.Dao.GoodsDao;
import com.cy.goods.Pojo.Goods;
import com.cy.goods.Servlet.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService{
    
    

	@Autowired
	private GoodsDao goosDao;
	
	@Override
	public List<Goods> findGoods() {
    
    
		// TODO Auto-generated method stub
		List<Goods> list = goosDao.findAll();
		return list;
	}
	
	
}

Step 9

Create Controller
Insert picture description here

code show as below

package com.cy.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.goods.Pojo.Goods;
import com.cy.goods.Servlet.GoodsService;

@Controller
@RequestMapping("/goods/")
public class GoodsController {
    
    
	@Autowired
	private GoodsService goodsService;
	
	@RequestMapping("doGoodsUI")
	public String doGoodsUI(Model model) {
    
    
		List<Goods> list= goodsService.findGoods();
		model.addAttribute("goods", list);
		return "goods";
		
	
	}
	
}

Tenth step

Create html
Insert picture description here

code show as below

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
  <thead>
    <tr>
      <th >id</th>
      <th >name</th>
    </tr>
  </thead>
  <tbody>
    <tr th:each="g: ${goods}">
      <td th:text="${g.id}">Oranges</td>
      <td th:text="${g.name}">Oranges</td>
    </tr>
  </tbody>
</table>
</body>
</html>

Eleventh step

Enter the URL test in the browser
Insert picture description here

The daily record of the relevant learning content is not very good, please advise

Gaojing College of Shaanxi University of Science and Technology: Attacking Lions Club

Guess you like

Origin blog.csdn.net/weixin_46422151/article/details/115260862