Springboot学习<三>

125 阅读1分钟

Springboot整合Mybatis

整合MyBatis同时整合SpringMVC+Thymeleaf完成CRUD操作

1.添加相关依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.5</version>
</dependency>

2.在application.properties文件配置相关JDBC、数据源、数据库连接池、映射文件包、实体类包等信息

#jdbc设置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
#数据库连接池设置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#mybatis的package别名
mybatis.type-aliases-package=com.hfl.bean
#指定MyBatis的映射文件的路径
mybatis.mapper-locations=classpath:mapper/*.xml

3.编写相关dao层、service层、controller层、bean实体类层等信息

image.png 4.配置对应的mapper映射文件

image.png 5.在启动器中添加@MapperSacn("xxx.xxx.mapper")注解

image.png

6.在启动测试,在Controller层直接使用@RestController注解,返回页面的json格式数据。

利用thymeleaf模板查询到数据并在页面显示

修改Controller里面的注解,方法返回内容 image.png 添加thymeleaf模板信息

image.png

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
</head>
<body>
    <h1>用户信息</h1>
    <table border="1" style="width: 300px;align:center">
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>地址</th>
            <th>描述</th>
        </tr>
        <tr th:each="user:${list}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}"></td>
            <td th:text="${user.age}"></td>
            <td th:text="${user.address}"></td>
            <td th:text="${user.description}"></td>
        </tr>
    </table>
</body>
</html>

利用thymeleaf添加数据

页面:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户添加</title>
</head>
<body>
    <form th:action="@{/user/save}"  method="post">
        <label>姓名: </label><input type="text" name="name"/><br>
        <label>年龄: </label><input type="text" name="age"/><br>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>

逻辑层:从基础页面访问请求进入addUser.html页面,提交完数据后,后台进行数据添加,动作完成后跳转到查询页面展示信息 image.png

利用thymeleaf来进行修改数据

增加按钮user.html

image.png 跳转到修改界面updateUser.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>更新页面</title>
</head>
<body>
    <h1>更新用户</h1>
    <form th:action="@{/user/update}" method="post">
        <input type="hidden" name="id" th:value="${user.id}"/>
        <label>姓名: </label><input type="text" name="name" th:value="${user.name}"/><br>
        <label>年龄: </label><input type="text" name="age" th:value="${user.age}"/><br>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>

提交修改数据

@RequestMapping("/user/updateInfo")
public String updateInfo(Integer id, Model model) {
    User user = userService.queryUserById(id);
    model.addAttribute("user", user);
    return "updateUser";
}
@RequestMapping("/user/update")
public String updateUser(User user) {
    userService.updateUser(user);
    return "redirect:/user/query";
}

显示更新的信息user.html

利用thymeleaf模板删除信息

增加删除按钮

image.png 删除数据后页面跳转

@RequestMapping("/user/deleteUser")
public String deleteUser(Integer id) {
    userService.deleteUser(id);
    return "redirect:/user/query";
}