SpringBoot整合Mybatis和ThyMeleaf

首先我们导入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

书写配置文件

# 应用名称
spring.application.name=springBootMybatis
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.zzp.springbootmybatis.entity
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=账号
spring.datasource.password=密码
# 应用服务 WEB 访问端口
server.port=8080
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=false
# 检查模板是否存在,然后再呈现
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

创建实体类

package com.zzp.springbootmybatis.entity;

public class Users {
    private Integer usid;

    private String uname;

    private String upwd;

    private String urealname;

    private String rname;

    private Integer did;

    public Integer getUsid() {
        return usid;
    }

    public void setUsid(Integer usid) {
        this.usid = usid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

    public String getUrealname() {
        return urealname;
    }

    public void setUrealname(String urealname) {
        this.urealname = urealname;
    }

    public String getRname() {
        return rname;
    }

    public void setRname(String rname) {
        this.rname = rname;
    }

    public Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    @Override
    public String toString() {
        return "Users{" +
                "usid=" + usid +
                ", uname=" + uname +  +
                ", upwd=" + upwd +  +
                ", urealname=" + urealname +  +
                ", rname=" + rname +  +
                ", did=" + did +
                };
    }
}

创建接口

package com.zzp.springbootmybatis.mapper;

import com.zzp.springbootmybatis.entity.Users;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UsersMapper {
    int deleteByPrimaryKey(Integer usid);

    int insert(Users record);

    int insertSelective(Users record);

    Users selectByPrimaryKey(Integer usid);

    int updateByPrimaryKeySelective(Users record);

    int updateByPrimaryKey(Users record);

    Users login(@Param("uName")String uName,@Param("upwd")String upwd);
}

创建service

package com.zzp.springbootmybatis.service;

import com.zzp.springbootmybatis.entity.Users;


public interface UsersMapperService {
    public Users login(String uName,String upwd);
}

实现service

package com.zzp.springbootmybatis.service;

import com.zzp.springbootmybatis.entity.Users;
import com.zzp.springbootmybatis.mapper.UsersMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class UsersMapperServiceImpl implements UsersMapperService{
    @Resource
    UsersMapper usersMapper;

    @Override
    public Users login(String uName, String upwd) {
        return usersMapper.login(uName, upwd);
    }
}

配置启动类

@MapperScan("com.zzp.springbootmybatis.mapper")配置扫描接口

package com.zzp.springbootmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.zzp.springbootmybatis.mapper")
public class SpringBootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisApplication.class, args);
    }

}

创建控制层Controller

package com.zzp.springbootmybatis.controller;

import com.zzp.springbootmybatis.entity.Users;
import com.zzp.springbootmybatis.service.UsersMapperService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UsersController {

    @Resource
    UsersMapperService usersMapperService;

    @RequestMapping("/login")
    public String login(Users users, HttpServletRequest request, HttpSession session){
        Users users1=usersMapperService.login(users.getUname(), users.getUpwd());
        System.out.println(users1);
        if (users1!=null){
            request.setAttribute("users",users1);
            request.getSession().setAttribute("users1",users1);
            session.setAttribute("users2",users1);
            return "success";
        }else {
            return "error";
        }
    }
}

登录页面

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/login" method="post">
        uName<input type="text" name="uname"></br>
        upwd<input type="password" name="upwd"></br>
        <input type="submit" value="登录"></br>
    </form>
</body>
</html>

登录成功页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
登陆成功</br>
request.setAttribute("users",users1);<span th:text="${users}"></span></br>
request.setAttribute("users",users1);<span th:text="${session.users}"></span></br></br>
request.getSession().setAttribute("users1",users1);<span th:text="${users1}"></span></br><!--这里不能 对象.属性 不然会报错 这里用的是getSession()要写成${session.users1.xxx}-->
request.getSession().setAttribute("users1",users1);<span th:text="${session.users1}"></span></br></br>
session.setAttribute("users2",users1);<span th:text="${users2}"></span></br><!--这里不能 对象.属性 不然会报错 这里用的是session要写成${session.users2.xxx}-->
session.setAttribute("users2",users1);<span th:text="${session.users2}"></span></br>
</body>
</html>

登陆失败页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
登陆失败
</body>
</html>

然后启动登录

账号或密码错误会进入错误页面

成功会进入成功页面

​
 Thymeleaf通过 ${}来获取model中的变量,注意这不是el 表达式,而是ognl表达式,但是语法非常像。

​

这里可以参照一下Thymeleaf是如何返回结果的

经验分享 程序员 微信小程序 职场和发展