SpringBoot与MyBatis的整合|配置 和 Thymeleaf 前台页面模板语言

6 篇文章 0 订阅
5 篇文章 0 订阅

1.springboot引入mybatis

1.引入mybatis依赖

<!-- 阿里巴巴的Druid数据源依赖启动器 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!-- MyBatis依赖启动器 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- MySQL数据库连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

2.在application.properties配置文件中配置

spring.datasource.url=jdbc:mysql://localhost:3306/szqy08?serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=30
spring.datasource.druid.max-active=50
spring.datasource.druid.min-idle=20

#开启驼峰写法
mybatis.configuration.map-underscore-to-camel-case=true
#如果配置文件  和 类名对应包名可以省略,否则 需要声明位置
#mybatis.mapper-locations=classpath:mapper/*.xml
#配置别名
mybatis.type-aliases-package=com.wgz.springboot.entity

3.dao层的实现

@Repository // 只是表明是一个dao,可以不写
@Mapper// 将当前类加入到容器 并表明当前类是一个mapper
public interface StudentDao {
/*    @Select("select * from student where id = #{id}")*/
    Student  findStudentById(int id);
}

4.entity.student实体

public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;
    private float height;
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", height=" + height +
                '}';
    }
    public int getId() {
        return id;}
    public void setId(int id) {
        this.id = id; }
    public String getName() {
        return name;}
    public void setName(String name) {
        this.name = name;}
    public String getSex() {
        return sex;}
    public void setSex(String sex) {
        this.sex = sex;}
    public int getAge() {
        return age;}
    public void setAge(int age) {
        this.age = age;}
    public float getHeight() {
        return height; }
    public void setHeight(float height) {
        this.height = height;}}

5. com/aaa/springboot/dao/StudentDao.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 对应的是字符串一般是 对应dao层 接口全限定名
在mybatis 使用二级缓存时会用到id==== namespace
-->
<mapper namespace="com.aaa.springboot.dao.StudentDao">
    <select id="findStudentById" resultType="com.aaa.springboot.entity.Student">

        select * from student where id = #{id}

    </select>
    
</mapper>

6.测试类 com/aaa/springboot/SelectTest.java

@RunWith(SpringRunner.class)
@SpringBootTest// 标明当前类是springboot测试类,   注意测试类一定要和启动类在同一个包下边
public class SelectTest {
    @Autowired
    private StudentDao studentDao;
    @Test
    public void selectTest(){
        System.out.println("studentDao"+studentDao.findStudentById(4));
    }
   
}
@RunWith(SpringRunner.class)
@SpringBootTest// 标明当前类是springboot测试类,   注意测试类一定要和启动类在同一个包下边
public class SelectTest {
    @Autowired
    private StudentDao studentDao;
    @Test
    public void selectTest(){
        System.out.println("studentDao"+studentDao.findStudentById(4));
    }
   
}

Thymeleaf 前台页面模板语言

开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了。SpringBoot支持如下页面模板语言

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • JSP

上面并没有列举所有SpringBoot支持的页面模板技术。其中Thymeleaf是SpringBoot官方所推荐使用的,下面来谈谈Thymeleaf一些常用的语法规则。

1.引入Thymeleaf依赖

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

**Spring Boot默认存放模板页面的路径在src/main/resources/templates,这个无论是使用什么模板语言都一样,当然默认路径是可以自定义的,不过一般不推荐这样做。另外Thymeleaf默认的页面文件后缀是 “.html” **

保证界面刷新

在配置文件application.properties配置


# 在测试阶段 关闭thymeleaf 缓存,有助于调试,上线阶段要打卡
spring.thymeleaf.cache=false

2.在resource下创建templates目录,创建student.html

引入依赖 xmlns:th="http://www.thymeleaf.org"
在这里插入图片描述

3. src/main/resources/templates/studentlist.html

5.5 th:each 标签

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>

    <thead>
        <tr>
            <td>
                id
            </td>
            <td>
                name
            </td>
            <td>
                age
            </td>
            <td>
                sex
            </td>
            <td>
                height
            </td>
            <td>
                更新
            </td>
        </tr>
    </thead>
    <!--  th:each="student:${studentList}"  获取studentList中的每一个元素 并赋值给睡student-->
    <tr th:each="student:${studentList}">

        <td th:text="${student.id}"></td>
        <td th:text="${student.name}"></td>
        <td th:text="${student.age}"></td>
        <td th:text="${student.sex}"></td>
        <td th:text="${student.height}"></td>
        <td > <a th:href="@{/updateStudent(id=${student.id})}">更新</a> </td>
    </tr>
</table>
</body>
</html>

4.向studentList界面返回数据

   @RequestMapping("/findAllStudent")// 返回 thymeleaf 视图
    public String findAllStudent(Model model){

        model.addAttribute("studentList",studentService.findAllStudent());

        return "studentlist";

    }

5.知识点

5.1.1通过 th:text,th:value显示数据

src/main/resources/templates/thymeleaf.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<!--  th:text 引用用 request 中的属性 并展示-->
msg: <span th:text="${msg}"></span>

student.id:<span th:text="${student.id}"></span>
student.name:<span th:text="${student.name}"></span>
student.sex:<span th:text="${student.sex}"></span>

<!-- 设置输入框的默认值  -->
input:---student.name<input th:value="${student.name}">

<!--${#request.getRequestURI()}  ${#request} 获取内置对象 -->
当前路径url:<p th:text="${#request.getRequestURI()}"></p>

<!-- 通过a 标签访问路径 -->

<a href="https://wwww.baidu.com"> 百度 </a>

th 方法 <a th:href="@{https://wwww.baidu.com}">百度</a>

th:if
<!-- 如果 th:if 符合调教  但钱 p 标签显示  -->
<p th:if="${student.age}>18 ">
    大学生
</p>
th:switch
<span th:switch="${student.sex}">

  <p th:case="F"></p>
  <p th:case="M"></p>

</span>
</body>
</html>

5.1.2向thymeleaf.html界面返回数据

 @RequestMapping("/thymeleafTest")// 返回 thymeleaf 视图
    public String thymeleafTest(Model model){

        model.addAttribute("msg","hello world");
        model.addAttribute("student",studentService.finStudentById(1));

        // 返回 thymeleaf.html
        return "thymeleaf";
    }

5.2访问内置对象

<!--${#request.getRequestURI()}  ${#request} 获取内置对象 -->
当前路径url:<p th:text="${#request.getRequestURI()}"></p>
<p th:text="${#httpServletRequest.getRequestURI()}"></p>

5.3.通过@{}访问资源


<a href="https://wwww.baidu.com"> 百度 </a>

/!--差不多--/
 <a th:href="@{https://www.baidu.com}">百度</a>
    <a th:href="@{/hi}">hi</a>

5.4. th:if 判断 th:switch 选择判断

<!-- 如果 th:if 符合调教  但钱 p 标签显示  -->
<p th:if="${student.age}>18 ">
    大学生
</p>
th:switch
<span th:switch="${student.sex}">

  <p th:case="F"></p>
  <p th:case="M"></p>

</span>

src/main/resources/templates/updateStudent.html

<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>更新学生</h2>
<form action="" method="post">
    id:<input name="id" th:value="${student.id}"><br>
    name:<input name="name" th:value="${student.name}"><br>
    age:<input name="age" th:value="${student.age}"><br>
    sex:<input name="sex" th:value="${student.sex}"><br>
    height:<input name="height" th:value="${student.height}"><br>

    <input type="submit" value="更新">
</form>
</body>
</html

controller 层


    @RequestMapping("/updateStudent")
    public String updateStudent(int id,Model model){

        System.out.println("id:"+id);

        model.addAttribute("student",studentService.finStudentById(id));

        return "updateStudent";

    }

3.springboot中静态资源目录

springboot的静态资源目录如下

/public–>/static–>/resources–>/META-INF/resources

访问资源的优先级 从左往右 优先级越来越高

classpath:/META-INF/resources/` > `classpath:/resources/` > `classpath:/static/` > `classpath:/public/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值