(MVC)SpringBoot+Mybatis+Mapper.xml

这篇具有很好参考价值的文章主要介绍了(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工程的应用

(MVC)SpringBoot+Mybatis+Mapper.xml,SpringBoot学习,spring boot,mybatis,xml

上篇博客地址:

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

配置文件

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">
<!-- 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文件里完成对应的配置

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里的方法

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层与数据库的关联

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层关联的返回结果

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层 

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层

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在数据库里可以查询到

(MVC)SpringBoot+Mybatis+Mapper.xml,SpringBoot学习,spring boot,mybatis,xml 

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

(MVC)SpringBoot+Mybatis+Mapper.xml,SpringBoot学习,spring boot,mybatis,xml文章来源地址https://www.toymoban.com/news/detail-636866.html

到了这里,关于(MVC)SpringBoot+Mybatis+Mapper.xml的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 探索Java中最常用的框架:Spring、Spring MVC、Spring Boot、MyBatis和Netty

    🎉欢迎来到Java面试技巧专栏~探索Java中最常用的框架:Spring、Spring MVC、Spring Boot、MyBatis和Netty ☆* o(≧▽≦)o *☆嗨~我是IT·陈寒🍹 ✨博客主页:IT·陈寒的博客 🎈该系列文章专栏:Java面试技巧 📜其他专栏:Java学习路线 Java面试技巧 Java实战项目 AIGC人工智能 数据结构学习

    2024年02月08日
    浏览(42)
  • Spring5学习随笔-整合MyBatis(持久层)、连接池、Mapper文件

    学习视频:【孙哥说Spring5:从设计模式到基本应用到应用级底层分析,一次深入浅出的Spring全探索。学不会Spring?只因你未遇见孙哥】 JavaEE开发需要持久层进行数据库的访问操作。 JDBC MyBatis、Hibernate进行持久开发过程存在大量的代码冗余 Spring基于模板设计模式对与上述的持

    2024年02月05日
    浏览(41)
  • Mybatis mapper.xml 判断条件写法注意

    1.判断String是否为空 if test=\\\"stringParam != null and stringParam != \\\'\\\'\\\"/if 2.判断Integer是否大于0 判断等于  when test=\\\"item.mark == 1\\\"\\\" 3.判断List是否不为空 5.判断字符串是否等于特定字符(比如此处的user)

    2024年02月16日
    浏览(39)
  • IDEA+spring boot+mybatis+spring mvc+bootstrap+Mysql停车位管理系统源码

    随着时代和科技的进步,人们的生活水平越来越高,私家车的数量不断上涨,随之产生了一些问题,其中就包括停车难,很多地方人们根本找不到停车位,经常有司机为了找停车位转来转去,走了很多弯路,更重要的是浪费了大量的时间。 而停车场车位管理系统可以使司机清

    2024年02月12日
    浏览(29)
  • Spring Boot入门(10):Mybatis之XML映射文件>、<=等特殊符号使用 | 保姆级教程

            在Mybatis的XML映射文件中,我们经常需要使用一些特殊符号来完成SQL语句的拼接,比如小于号“”、小于等于号“=”等。然而,由于这些符号在XML中有特殊的含义,因此直接在映射文件中使用可能会导致编译错误或者运行时异常。那么,如何才能正确地使用这些特殊

    2024年02月11日
    浏览(45)
  • MyBatis - DAO 接口(Mapper.xml)支持方法重载吗?

    方法重载(Method Overloading)是指在同一个类中定义多个方法,它们具有相同的方法名但参数列表不同。 在 MyBatis 的 DAO 层接口中, 是允许方法重载的。 在 DAO 层接口中,可以根据不同的需求和条件定义多个方法,提供不同的查询或操作方式。 在 MyBatis 的 XML 映射文件中, 是

    2024年01月17日
    浏览(38)
  • mybatis中的mapper.xml中如何使用in方法

    提示:mapper.xml中如何使用in方法一般都是like或= 提示:使用foreach 注意,传入的参数是List ,如果传入的是array 则需要修改 collection部分定义为 collection=“array” 在MyBatis中使用in参数为集合时,需要使用到foreach标签。 下面详细介绍以下foreach标签的几个参数

    2024年02月15日
    浏览(32)
  • 深入实现 MyBatis 底层机制的任务阶段4 - 开发 Mapper 接口和 Mapper.xml

    😀前言 在我们的自定义 MyBatis 底层机制实现过程中,我们已经深入研究了多个任务阶段,包括配置文件的读取、数据库连接的建立、执行器的编写,以及 SqlSession 的封装。每个任务阶段都为我们揭示了 MyBatis 内部工作原理的一部分,为构建完整的底层框架打下了坚实的基础

    2024年02月09日
    浏览(34)
  • dom4j 解析 mybatis mapper xml 文件

    01:  CarMapper.xml :   02: Car pojo :    CarMapper.xml       \\\"C:Program FilesJavajdk-17binjava.exe\\\" -ea -Didea.test.cyclic.buffer.size=1048576 -Didea.launcher.port=62271 \\\"-Didea.launcher.bin.path=C:MinecloudIDEA_2019IntelliJ IDEA 2019.1bin\\\" -Dfile.encoding=UTF-8 -classpath \\\"C:MinecloudIDEA_2019IntelliJ IDEA 2019.1libidea_rt.jar;C:Minecl

    2024年02月10日
    浏览(30)
  • Spring Boot入门(09):如何使用MyBatis的XML配置方式实现MySQL的增删改查操作?

            想要快速高效地开发Java Web应用程序,选择使用Spring Boot和MyBatis无疑是明智之举。本篇文章将教你使用MyBatis的XML配置方式,结合MySQL数据库,实现常见的增删改查操作,让你的应用程序更加实用和强大。跟随本文一起来探索MyBatis在Spring Boot中的力量吧!        

    2024年02月11日
    浏览(51)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包