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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 行为识别研究摘录

    行为识别研究摘录基于3D卷积神经网络的行为识别:3DConvolutionalNeuralNetworksforHumanActionRecognitionhttp://www.cnblogs.com/Ponys/p/3450177.html

    2022年6月21日
    24
  • 面试之springboot自动配置原理「建议收藏」

    面试之springboot自动配置原理「建议收藏」自动配置首先从注解说起。@SpringBootApplication由三个注解组成:@CompanentScan,@EnableAutoConfiguration,@SpringBootConfiguration(其实就是@Configuration注解),其中@EnableAutoConfiguration通过@Import注解将AutoConfigurationImportSelector.class这个类引进来。该类会去加载所有jar包的META-INFO下面的spring-……

    2022年8月21日
    5
  • android读取短信_android发短信代码

    android读取短信_android发短信代码在Tasker中利用短信的通知实现短信内容转发到微信之前的教程是利用Tasker自带的短信变量来实现的,这有一个无法解决问题:在360,魅族,坚果等品牌的手机中无法获取短信的消息内容。利用短信的通知则可以获取短信的内容,并且对于华为手机来说也可以不用关闭短信验证码保护功能,所以相较于原来的方法更简单,方便。应用通知的变量是一个本地数组变量%evtprm(),这个变量数组包含%evtprm…

    2022年10月13日
    2
  • 通俗解释hash碰撞是什么以及如何解决

    通俗解释hash碰撞是什么以及如何解决Hash如何存数据hash表的本质其实就是数组,hash表中通常存放的是键值对Entry。如下图:这里的学号是个key,哈希表就是根据key值来通过哈希函数计算得到一个值,这个值就是下标值,用来确定这个Entry要存放在哈希表中哪个位置。Hash碰撞hash碰撞指的是,两个不同的值(比如张三、李四的学号)经过hash计算后,得到的hash值相同,后来的李四要放到原来的张三的位置,但是数组的位置已经被张三占了,导致冲突。解决方法hash碰撞的解决方式是开放寻址法和拉..

    2022年6月16日
    28
  • 程序员通过思考和语言改变世界

    程序员通过思考和语言改变世界

    2022年3月13日
    38
  • java jar 没有主清单_java运行jar命令提示没有主清单属性

    java jar 没有主清单_java运行jar命令提示没有主清单属性在 JAVA 中将 class 文件编译成 jar 文件包 运行提示没有主清单属性 这是怎么回事 今天来教大家如何解决这个问题 1 在 java 中编译 JAR 文件的时候我们都会用到 jar 这个命令 当用着 jar 文件时候我们不可少的是 cvf 这几个参数来生成 jar 文件 但是用个文件来了 在用不使用工具的前提下我们生成的 jar 文件包后运行会出现 某某类的没有主清单属性 如下图 2 这样的问题是因为 jar 包中的 META

    2025年11月6日
    2

发表回复

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

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