SpringBoot配置Mybatis:详细易懂「建议收藏」

SpringBoot配置Mybatis:详细易懂「建议收藏」文章目录SpringBoot配置Mybatis:详细易懂前期准备工作Mybatis相应配置编写相应代码文件结构和结果增删查改Mybatis动态SQL参考文章SpringBoot配置Mybatis:详细易懂Mybatis作为后端持久层框架,在互联网大厂中应用广泛,所以掌握Mybatis,可谓是必备的。最近准备系统得复习一下Mybatis框架,所以博客会更几期关于Mybatis得文章,如果觉得…

大家好,又见面了,我是你们的朋友全栈君。

SpringBoot配置Mybatis:详细易懂

Mybatis作为后端持久层框架,在互联网大厂中应用广泛,所以掌握Mybatis,可谓是必备的。最近准备系统得复习一下Mybatis框架,所以博客会更几期关于Mybatis得文章,如果觉得看完有收获,希望收藏、点赞。如果觉得写得不好,欢迎评论区指正。

前期准备工作

Mybatis相应配置

修改pom.xml,获取Mybatis、MySQL相关依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.2</version>
    <scope>compile</scope>
</dependency>

application.properties文件中配置Mybatis连接环境

mybatis.type-aliases-package=com.test.demo
#mysql驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#远程数据库链接 serverTimezone不可少
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
#MySQL数据库用户名、密码
spring.datasource.username=root
spring.datasource.password=xxxxx
#xml格式的mapper文件位置
mybatis.mapper-locations=classpath:/mapper/*.xml

编写相应代码

添加bean类(对应与数据库属性列)

package com.test.demo;

public class Student { 
   
    public int sNo;
    public String sName;
    public int sAge;
    public Student(int sNo, String sName, int sAge) { 
   
        this.sNo = sNo;
        this.sName = sName;
        this.sAge = sAge;
    }
    //Getter、Setter方法,我省略了,你在代码中别省略
}

添加mapper接口代码

package com.test.demo.mapper;

import com.test.demo.Student;

public interface StudentMapper { 
   
    void insert(Student student);
}

添加mapper接口对应的xml格式文件

一定注意路径对应,com.test.demo.mapper.StudentMapper

这里只写了插入,下文会补充删除、更新、查询

<?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">

<mapper namespace = "com.test.demo.mapper.StudentMapper">

    <insert id="insert" parameterType="com.test.demo.Student" >
        INSERT INTO student (Sno,Sname,Sage) VALUES (#{ 
   sNo},#{ 
   sName},#{ 
   sAge})
    </insert> 
</mapper>

最后的controller代码文件

package com.test.demo.controller;
import org.springframework.web.bind.annotation.RestController;
import com.test.demo.Student;
import com.test.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HomeController { 
   
    @Autowired
    private StudentMapper  studentMapper;
    @RequestMapping("/")
    public String index() throws Exception { 
   
        studentMapper.insert(new Student(1, "天宇", 24));
        return "OK";
    }
}

文件结构和结果

SpringBoot配置Mybatis:详细易懂「建议收藏」

SpringBoot配置Mybatis:详细易懂「建议收藏」

增删查改

mapper接口

package com.test.demo.mapper;
import com.test.demo.Student;

public interface StudentMapper { 
   
    void insert(Student student);
    void delete(String sNo);
    Student selectByNumber(Integer sNo);
    void updateName(Student student);
}

对应xml文件

参数的类型一定是完整的包名,参数名写ben类的属性名

<?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">

<mapper namespace = "com.test.demo.mapper.StudentMapper">
	<!--插入操作-->
    <insert id="insert" parameterType="com.test.demo.Student" >
        INSERT INTO student (Sno,Sname,Sage) VALUES (#{sNo},#{sName},#{sAge})
    </insert> 
    
    <!--删除操作-->
    <delete id="delete" parameterType="java.lang.String">
        DELETE FROM student where Sno = #{sNo};
    </delete>

    <!--查询操作-->
    <select id="selectByNumber" parameterType="java.lang.Integer" resultType="com.test.demo.Student">
        SELECT * FROM student where Sno = #{sNo}
    </select>

    <!--更新操作-->
    <update id="updateName" parameterType="com.test.demo.Student">
        UPDATE student SET Sname = #{sName} where Sno = #{sNo}
    </update>
</mapper>

Mybatis 动态SQL

在实际应用开发过程中,我们往往需要写复杂的 SQL 语句,需要拼接,而拼接SQL语句又稍微不注意,由于引号,空格等缺失可能都会导致错误。Mybatis提供了动态SQL,也就是可以根据用户提供的参数,动态决定查询语句依赖的查询条件或SQL语句的内容。

if、foreach关键字用法

mapper接口

public interface StudentMapper { 
   
    List<Student> findByCondition(String sName, Integer sAge);
    List<Student> selectByNumberList(List<Integer> list);
}

对应的xml格式mapper文件

<select id="findByCondition" resultType="com.test.demo.Student" parameterType="map">
        select * from student
        <!--名字不为空,则限定名字-->
        <!--年龄参数不为空,则限定查询结果的年龄-->
		<where>
            <if test="sName!=null">
                and Sname = #{sName}
            </if>
            <if test="sAge!=null">
                and Sage &lt; #{sAge}
            </if>
		</where>
    </select>
    <select id="selectByNumberList" resultType="com.test.demo.Student" parameterType="list">
        select * from student
        <where>
            Sno in
            <foreach item="item" collection="list" separator="," open="(" close=")" index="">
                #{item}
            </foreach>
        </where>
    </select>

参考文章

简单,Spring boot 配置mybatis

Mybatis入门看这一篇就够了

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152651.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python与java的区别(java 和python)

    java和python区别Python或Java,哪个更好?这个问题在全球开发者社区引发了许多激烈的讨论。初学者开发人员可能对应该掌握两者中的哪一个有所怀疑。初创公司和公司可能想知道哪种方案在他们的下一个项目中会更好。这两种语言都可以以相同的效率解决许多任务,这不足为奇。但是,在某些情况下,一个人可以击败另一个人。在本文中,我们将基于多个方面来分析它们的优缺点。对于那…

    2022年4月18日
    83
  • python移动应用开发_python做手机app

    python移动应用开发_python做手机app广告关闭腾讯云11.11云上盛惠,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元!解压后依次执行以下命令安装sdk。$cdtencentcloud-sdk-python$pythonsetup.pyinstall示例代码说明:所有示例代码仅作参考,无法直接编译和运行…13711112222为手机号,最多不要超过200个手机号req.pho…

    2022年8月12日
    5
  • pywin32、win32api、win32gui、win32com、win32con 都是啥?「建议收藏」

    pywin32、win32api、win32gui、win32com、win32con 都是啥?「建议收藏」pywin32、win32api、win32gui、win32com、win32con名称非常类似,特别容易混淆,今天就用600字给大家区分一下文章目录pywin32win32guiwin32conwin32apiwin32com记录时间pywin32pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个模块库。该模块的另一个作用是是通过Python进行COM编程。落地场景:如果你想在Windows操作系统用Python实现自动化工作,pywin32模块经常用到

    2022年10月11日
    0
  • vscode设置vue模板_vscode怎么创建vue项目

    vscode设置vue模板_vscode怎么创建vue项目VSCode配置Vue模板代码前端行业使用的编辑器有很多,比如VSCode和webStorm,其中在创建vue文件后webStorm可以自动生成相关的代码,而在VSCode中得一个一个的敲,这样既浪费时间又效率低,因此,在VSCode中可以一键生成vue模板吗?当然可以,过程如下:1、打开VSCode编辑器2、左上角文件(F)=>首选项=>用户片段3、在出现的框中输入vue之后按回车键4、在出现的vue.json文件内写入以下代码

    2022年9月6日
    2
  • ffmpeg制作视频_跟我学刺绣教学视频教程

    ffmpeg制作视频_跟我学刺绣教学视频教程最近一段时间找时间录制了一些Ffmpeg视频教程,还有录制完毕,会持续更新,内容会包含Ffmeg保存文件,网络流转发,编码,解码,播放器制作,以及服务端搭建等等,适合初学者,有需要的朋友的可以关注:

    2022年8月4日
    5
  • nginx正向代理(超简单)

    正向代理是一个位于客户端和原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。环境192.168.153.179:正向代理192.168.153.178:客户端CentOSLinuxrelease7.5.1804(Core)关闭防火墙和selinux开始部署:首先,两台服务器安装nginx源码安装:1、安装启动安装依赖yum-yinstallwgetgcc

    2022年4月5日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号