¡Revisar! ! SpringBoot integra el marco SpringMVC

Descripción general del marco SpringMVC:

SpringMVC es un marco web ligero impulsado por solicitudes que implementa el modelo de diseño MVC basado en Java. Utiliza un conjunto de anotaciones para convertir una clase Java simple en un controlador para procesar solicitudes sin implementar ninguna interfaz.

Preparación preliminar

1. Edite el archivo pom.xml, agregue la dependencia web Spring, la dependencia Thymeleaf

Dependencia web (se proporciona la API principal de Spring MVC y se integrará un servidor Tomcat al mismo tiempo)

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

Dependencia de Thymeleaf (proporciona un objeto de resolución de vista y un mecanismo de enlace de datos)

<!-- 添加Thymeleaf依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2. Agregue la configuración de resolución de vista en application.properties

# server port
server.port=80

# Spring thymeleaf
# 修改页面不需要重启服务器
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html

3. Spring MVC para la implementación de codificación

3.1 Crear un proyecto

SpringBoot integra el marco SpringMVC

3.2 Escribir archivo GoodsMapper.xml

<?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">
    <!-- 查询所有用户信息 -->
    <select id="findAll" resultType="com.cy.pj.goods.utils.User">
            select * from emp
    </select>
</mapper>

3.3 Escribir interfaz GoodsDao

package com.cy.pj.goods.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.cy.pj.goods.utils.User;
/**
 * @Mapper 用于描述(做标记)数据层访问接口,用于告诉mybatis框架
 *    使用此注解描述的接口要由底层为创建实现类,在实现类中基于mybatis
 *    API实现与数据库的交互,这个类的对象最后会交给Spring管理。
 */
@Mapper
public interface GoodsDao {
    /**
     * 查询所有用户信息
     * @return List集合
     */
    public List<User> findAll();
}

3.4 Escribir interfaz GoodsService

package com.cy.pj.goods.service;

import java.util.List;

import com.cy.pj.goods.utils.User;

/**
 * 用户模块的业务层接口,用于制定标准
 * @author BigData
 *
 */
public interface GoodsService {

    /**
     * 查询所有用户信息
     * @return 全部信息
     */
    public List<User> findAll();
}

3.5 Escribir clase GoodsServiceImpl

Esta clase implementa la interfaz GoodsService, que se utiliza para la realización específica del negocio del módulo de usuario.

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;
/**
 * 业务层对象,后续会在此对象中执行
 * @author BigData
 */
@Service
public class GoodsServiceImpl implements GoodsService{

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public List<User> findAll() {
        return goodsDao.findAll();
    }
}

3.6 Escribir clase CoodsController

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:如果当前类所在的包配置了Spring容器包扫描,具有
 *该注解的类,就会作为bean注册到spring容器中,由spring容器创建实例。 
 */
@Controller
@RequestMapping("/show/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    /*
     * @RequestMapping:为当前方法配置访问路径
     * 如果Controllor类上没有配置访问路径,当前项目中所有controller中方法上的访问路径都不能冲突
     */
    @RequestMapping("findAll")
    public String findAll(Model model) {
        List<User> lists = goodsService.findAll();
        model.addAttribute("list", lists);
        return "show";

    }
}

3.7 Crear página html

Cree show.html en el directorio templates / pages para mostrar datos

SpringBoot integra el marco SpringMVC

3.7 Editar página html

<!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.salary}">5000.0</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

Salida y visualización

SpringBoot integra el marco SpringMVC

En este punto, la integración del marco está completa, gracias por su referencia, hay deficiencias, ¡deje sus valiosos comentarios! ! !

Supongo que te gusta

Origin blog.csdn.net/qwe123147369/article/details/109097584
Recomendado
Clasificación