springboot+mybatis+Thymeleaf整合

1.新建一个springboot项目
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
需要选择上面的模块(也可以自己添加相关依赖).

2.新建目录结构
在这里插入图片描述
3.编写代码
properties文件(有些自带的不用写,缺啥写啥)

# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080
#数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testaaa
spring.datasource.username=root
spring.datasource.password=123456
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath*:mapper/*.xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.example.demo.mybatis.entity
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=true
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html


show.html

<!DOCTYPE html>
<html xmlns:th="http://www.themeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1px">
    <tr>
    <th >编号</th>
    <th>姓名</th>
    <th>年龄</th>
    <th>地址</th>
    <th>性别</th>
    </tr>
    <tr th:each="s:${user}">
        <td th:text="${s.id}"></td>
        <td th:text="${s.name}"></td>
        <td th:text="${s.age}"></td>
        <td th:text="${s.address}"></td>
        <td th:text="${s.sex}"></td>
    </tr>

</table>
</body>
</html>

thymeleaf接口

package com.example.demo.controller;

import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @program:bootMybatisThemleaf
 * @description:
 * @author:ww
 * @create:2021-09-09 17:18
 */
@Controller
public class ThymeleafController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/display")
    public String display(Model model)
    {
        model.addAttribute("user",userMapper.select());
        return "show";
    }
}

测试接口(可以不写)

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
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 org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program:bootMybatisThemleaf
 * @description:
 * @author:ww
 * @create:2021-09-09 15:52
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("showuser.do")
    public List<User> show(){
        return userService.selectSer();
    }
}

user类

package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Bean;

/**
 * @program:bootMybatisThemleaf
 * @description:实体类
 * @author:ww
 * @create:2021-09-09 15:50
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String address;
    private String sex;
}

mapper类

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {

    @Select("select * from user")
    public List<User> select();
}

service

package com.example.demo.service;

import com.example.demo.entity.User;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program:bootMybatisThemleaf
 * @description:
 * @author:ww
 * @create:2021-09-09 15:52
 */
public interface UserService {
    public List<User> selectSer();
}

serviceimpl

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program:bootMybatisThemleaf
 * @description:
 * @author:ww
 * @create:2021-09-09 15:58
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> selectSer() {
        return userMapper.select();
    }
}

数据库
在这里插入图片描述
此代码做了整合,功能只写了数据展示,另外一个页面没有写,可以自己添加功能.

完结撒花~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值