Spring Boot初步学习04

通过以前整合mybatis框架以后,本文继续将springboot与springmvc进行整合.html

MVC设计模式

MVC设计模式将系统分为了三部分:
控制器(Controller)- 负责获取请求,处理请求,响应结果;
模型(Model) - 实现业务逻辑,数据逻辑实现;
视图(View) - UI设计人员进行图形界面设计,负责实现与用户交互;前端

MVC的首要职责:是让每一个对象各司其职,各尽所能;
其次是:基于"高内聚,低耦合"的面向接口思想实现相关层与层对象之间的交互.spring

整合基本配置

添加依赖

首先须要添加springmvc依赖:
右键pom.xml-->spring-->Edit Starters-->搜索spring WEB以及Thymeleaf,并将这两个依赖添加.后端

Web依赖提供了Spring MVC核心API,同时会嵌入一个Tomcat服务器;
Thymeleaf依赖提供了一个视图解析器对象以及数据绑定机制.
Thymeleaf是一个html模板引擎,提供了与Spring MVC进行整合的API,可做为MVC架构中Web应用的View层(已经逐步替代JSP进行使用).设计模式

配置核心

须要在application.properties配置文件中添加视图解析器配置(假如没有配置也会默认配置,在默认配置中prefix默认值为classpath:/templates/,后缀默认为.html)浏览器

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

在这里咱们配置如上图所示.tomcat

注意:根据配置,咱们须要在src/main/resources目录下建立templates/pages目录.springboot

thymeleaf目录下的html文件(模板文件)没法直接访问,若要直接访问须要在src/main/resources目录下建立static目录(用于存储静态资源).服务器

访问实现

编写GoodsController类

代码以下:mybatis

@Controller
@RequestMapping("/goods/")
public class GoodsController {
    @RequestMapping("doGoodsUI")
    public String doGoodsUI() {
        return "goods";
    }
}

经过添加@Controller注解将其交给spring管理,虽然是@Controller,但其实他并不属于MVC设计模式中的"C",而是Handler(处理器),可理解为一个后端控制器,但属于"M".

该方法是由前端控制器DispatcherServlet调用,返回值也是返回给前端控制器;返回值会被前端控制器调用视图解析器添加先后缀.

建立goods.html

因为GoodsController类最后会return "goods";因此咱们须要在/templates/pages/目录下建立goods.html文件.

那么当GoodsController类中return时,视图解析器会经过咱们所配置的先后缀,将其指向/templates/pages/目录下的goods.html文件,在网页中进行显示.

访问测试

打开服务器(内嵌的tomcat),打开浏览器输入路径进行访问测试

#server
server.port=80

因为我将端口号改成了80,因此只需在浏览器输入:
localhost/goods/doGoodsUI便可成功访问.

业务实现

既然已经能够成功访问后,能够与以前的Dao层的数据操做一同使用一下,看一下效果,这里以查询全部商品为实例.

@Controller
@RequestMapping("/goods/")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("doGoodsUI")
    public String doGoodsUI(Model model) {
        List<Goods> list = goodsService.findObjects();
        model.addAttribute("list", list);
        return "goods";
    }
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>the goods pages</h1>
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>name</th>
                <th>remark</th>
                <th>createdTime</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="g:${list}">
                <td th:text="${g.id}">1</td>
                <td th:text="${g.name}">pathon</td>
                <td th:text="${g.remark}">framwork</td>
                <td th:text="${#dates.format(g.createdTime, 'yyyy/MM/dd HH:mm:ss')}">2020/8/3 17:33</td>
            </tr>
        </tbody>
    </table>
</body>
</html>
@Mapper
public interface GoodsDao {
    @Select("select * from tb_goods")
    public List<Goods> findObjects();
}
public interface GoodsService {
    public List<Goods> findObjects();
}
@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private GoodsDao goodsDao;
    @Override
    public List<Goods> findObjects() {
        long t1=System.currentTimeMillis();
        List<Goods> list = goodsDao.findObjects();
        long t2=System.currentTimeMillis();
        log.info("execute time:{}",(t2-t1));
        return list;
    }
}

以上是代码实现,能够获得正确显示:

1596455966(1).png