(MVC)SpringBoot+Mybatis+Mapper.xml

前言:本篇博客主要对MVC架构、Mybatis工程加深下理解,前面写过一篇博客:SprintBoot+html/css/js+mybatis的demo,里面涉及到了Mybatis的应用,此篇博客主要介绍一种将sql语句写到了配置文件里的方法,即Mybatis里Mapper.xml文件配置,其主要用于定义sql语句和映射关系

目录

MVC架构流程图

配置文件

mapper层

model层

service层

controller层

结果展示


MVC架构流程图

根据自己的理解画了一下访问接口时,涉及到service层、mapper层、mybatis工程的应用

上篇博客地址:

https://blog.csdn.net/MRJJ_9/article/details/131884585

配置文件

xml配置文件

java 复制代码
<?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">
<!-- namespace:填写映射当前的Mapper接口,所有的增删改查的参数和返回值类型,
		就可以直接填写缩写,不区分大小写,直接通过方法名去找类型-->
<mapper namespace="com.example.interfaceautotest.mapper.CaseMapper">
<!--    id 对应的是mapper.CaseMapper里的方法名-->
    <select id="getInfoByPhone" resultType="com.example.interfaceautotest.model.MysqlUserData">
        select * from user where phone =#{phone} and pw =#{pw}  </select>

</mapper>

在application.properties文件里完成对应的配置

java 复制代码
spring.datasource.url=jdbc:mysql://localhost:3306/auto_test_data?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:/mapper/*.xml

mapper层

mapper里的方法

java 复制代码
package com.example.interfaceautotest.mapper;

import com.example.interfaceautotest.model.MysqlUserData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface CaseMapper {
    
    MysqlUserData getInfoByPhone(@Param("phone") String phone,
                               @Param("pw") String pw);
}

model层

model层与数据库的关联

java 复制代码
package com.example.interfaceautotest.model;

//注册表
public class MysqlUserData{
    private String id;
    private String usr;
    private String pw;
    private String phone;
    private String email;

    public String getId(){
        return id;
    }
    public void setId(String id){
        this.id = id;
    }
    public String getUsr(){
        return usr;
    }
    public void setUsr(String usr){
        this.usr = usr;
    }

    public String getPw(){
        return pw;
    }
    public void setPw(String pw){
        this.pw = pw;
    }
    public String getPhone(){
        return phone;
    }
    public void setPhone(String phone){
        this.phone = phone;
    }
    public String getEmail(){
        return email;
    }
    public void setEmail(String email){
        this.email = email;
    }

    }

model层与service层关联的返回结果

java 复制代码
package com.example.interfaceautotest.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
    public int code;
    public String msg;
    public Object data;

    public int getCode(){
        return code;
    }
    public void setCode(int code){
        this.code = code;
    }
    public String getMsg(){
        return msg;
    }
    public void setMsg(String msg){
        this.msg = msg;
    }

    public Object getData(){
        return data;
    }
    public void setData(Object data){
        this.data = data;
    }
}

service层

java 复制代码
package com.example.interfaceautotest.service.impl;

import com.example.interfaceautotest.mapper.CaseMapper;
import com.example.interfaceautotest.model.MysqlUserData;
import com.example.interfaceautotest.model.Result;
import com.example.interfaceautotest.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("UserSevice")
public class UserServiceImpl implements UserService {
    @Resource
    CaseMapper CaseMapper;
    public Result login(String username, String password) {
        if (username == null || password == null)
            return new Result(-1,"用户名或密码不能为空","用户名或密码不能为空");
            MysqlUserData user = CaseMapper.getInfoByPhone(username, password);
            if (user==null){
                return new Result(-3,"用户名或密码错误","用户名或密码错误");
            }
            else {
                return new Result(1,"登录成功",user);
            }
    }
}

controller层

java 复制代码
package com.example.interfaceautotest.controller;
import com.example.interfaceautotest.model.Result;
import com.example.interfaceautotest.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/test")
public class Login {
        @Resource
        UserService UserService;
        @GetMapping("/login")

        public Result login(String username, String password){
            return UserService.login(username,password);
        }

}

结果展示

传入的参数phone和pw在数据库里可以查询到

传入的参数phone和pw在数据库里不可以查询到

相关推荐
SuniaWang2 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
韩立学长2 小时前
Springboot校园跑腿业务系统0b7amk02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
sheji34162 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
Meepo_haha5 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34165 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
xiaohe076 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
gechunlian887 小时前
Spring Boot中的404错误:原因、影响及处理策略
java·spring boot·后端
givemeacar7 小时前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
Mr.45678 小时前
Spring Boot集成Redis:单机、哨兵、集群三种模式统一配置实战
spring boot·redis·bootstrap
lay_liu9 小时前
Spring Boot 自动配置
java·spring boot·后端