SpringBoot整合SpringMVC框架

前言:
該文章是基於 SpringBoot整合MyBatis框架快速入門 文章的基礎上實現的下列功能。html

SpringMVC框架概述:

SpringMVC 是一種基於 Java 實現 MVC 設計模型的請求驅動類型的輕量級 Web 框架。它經過一套註解,讓一個簡單的 Java 類成爲處理請求的控制器,而無須實現任何接口。java

前期準備

1.編輯pom.xml文件,添加Spring web依賴,Thymeleaf依賴

Web依賴(提供了Spring MVC核心API,同時會嵌入一個Tomcat服務器)web

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

Thymeleaf依賴(提供了一個視圖解析器對象以及數據綁定機制)spring

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

2.在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進行編碼實現

3.1 建立工程

建立工程SpringBoot整合SpringMVC.png

3.2 編寫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 編寫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 編寫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 編寫GoodsServiceImpl類
該類實現了GoodsService接口,用於用於用戶模塊的業務的具體實現數據庫

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 編寫CoodsController類apache

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 建立html頁面

在templates/pages目錄下建立show.html,用於展現數據
建立網頁.jpgsegmentfault

3.7 編輯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>
輸出和展現

urlAnd結果.jpg

到這裏,框架的整合就完了,感謝各位的參考,有不足的地方,請留下您寶貴的意見!!!服務器

相關文章
相關標籤/搜索