springboot整合mybatis+thmeleaf

一、springboot支持jsp组件

1、注意:springboot默认不支持jsp作为试图组件;需要导入springboot支持jsp的依赖包

<!--支持jsp的依赖-->
<!--springboot tomcat jsp 支持开启 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>9.0.2</version>
</dependency>

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<!-- 添加jsp支持 结束 -->

2、配置视图解析器

#配置tomcat的端口
server.port=8081
#配置项目的名称
server.servlet.context-path=/myspringboot
#视图解析器
#视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

3、编写控制器返回视图组件

    @RequestMapping("/goIndex")
    public String goInedx() throws  Exception {
        return "index";
    }

 

二、springboot+ssm的使用(springboot整合mybatis)

1、组织项目的目录结构

2、添加框架的支持,添加mybatis相关的包;mybatis,mysql驱动包,springmvc的包

      <!--mysql驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
            <scope>runtime</scope>
        </dependency>

        <!--mybatis的包-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <!--添加mybatis分页插件支持-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>

3、springboot整合mybatis,创建mybatis-config.xml文件

#配置tomcat的端口
server.port=8081
#配置项目的名称
server.servlet.context-path=/myspringboot
#视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

#配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/lethouse
spring.datasource.username=root
spring.datasource.password=root

#mybatis的配置
#指定mybatis配置文件的位置
mybatis.config-location=classpath:mybatis-config.xml
#指定mybatis的类型别名  mybatis配置文件中配置类型别名,此配置可不写
#mybatis.type-aliases-package=com.springboot.demo.entity
#指定mybatis的sql映射文件  mybatis配置文件中加载了sql映射,此配置可不写
#mybatis.mapper-locations=classpath:com/springboot/demo/mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
     <!--配置别名-->
    <typeAliases>
        <!--类型的别名就是类名-->
        <package name="com.springboot.demo.entity"></package>
    </typeAliases>

    <!--添加分页的插件-->
    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>


    <!--加载sql映射文件-->
    <mappers>
        <mapper resource="com/springboot/demo/mapper/DistrictMapper.xml"></mapper>
    </mappers>

</configuration>

4、springboot中的static和templates文件夹作用

       static存放静态资源文件(css,js等等);

       templates用于存放模板文件;

三、使用springboot提供的thymeleaf作为视图组件

1、介绍Thymeleaf

      Thymeleaf在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以thymeleaf的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

Jsp作视图组件存在的问题:效率低(执行速度慢)

2、使用thymeleaf步骤

第一步:引入依赖包

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

第二步:在springboot配置文件中增加配置

#配置tomcat的端口
server.port=8081
#配置项目的名称
server.servlet.context-path=/myspringboot
#视图解析器
#spring.mvc.view.prefix=/
#spring.mvc.view.suffix=.jsp

# thymeleaf的配置  jsp的配置可以不要了
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.servlet.content-type=text/html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.cache=false

#配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/lethouse
spring.datasource.username=root
spring.datasource.password=root


#mybatis的配置
#指定mybatis配置文件的位置
mybatis.config-location=classpath:mybatis-config.xml
#指定mybatis的类型别名  mybatis配置文件中配置类型别名,此配置可不写
#mybatis.type-aliases-package=com.springboot.demo.entity
#指定mybatis的sql映射文件  mybatis配置文件中加载了sql映射,此配置可不写
#mybatis.mapper-locations=classpath:com/springboot/demo/mapper


第三步: 编写html页面,编写控制器返回index.html

切记:html页面必须加入thymeleaf的支持 <html lang="en" xmlns:th="http://www.thymeleaf.org">

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Title</title>
    <script language="JavaScript" src="js/jquery-1.8.3.js"></script>
    <script language="JavaScript">
      $(function(){
           $("#table1 tr:odd").css("background-color","red");
          $("#table1 tr:even").css("background-color","yellow");
      });
    </script>
</head>
<body>
我是模板:
<div th:text="${info}"></div>
<input type="text" th:value="${info}">
<table id="table1">
    <tr>
        <td>区域的编号</td>
        <td>区域名称</td>
    </tr>
    <tr th:each="d:${list}">
        <td th:text="${d.id}"></td>
        <td th:text="${d.name}"></td>
    </tr>
</table>
<select>
    <option th:each="d:${list}" th:value="${d.id}" th:text="${d.name}"></option>
</select>
</body>
</html>
   @RequestMapping("/showDistrict")
    public String showDistrict(Model model) throws Exception{
         List<District> list=districtService.getAllDistrict();
         model.addAttribute("info","我会使用thymeleaf啦!");
         model.addAttribute("list",list);
         return "index";
     }

 

如果css和js放在了resources/static下面,引入方式如下图

css:th:href="@{/js/jquery-easyui-1.8.6/themes/icon.css}"

js:th:src="@{/js/district/district.js}"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值