Mybatis学习笔记7 参数处理专题

Mybatis学习笔记6 使用时的一些小技巧_biubiubiu0706的博客-CSDN博客

1.单个简单类型参数

2.Map参数

3.实体类参数

4.多参数

5.@Param注解(命名参数)

6.@Param源码分析

建表

插入点数据

新建模块

pom.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mybatis-06</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!--logback-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
        </dependency>
    </dependencies>
</project>

目录结构

1.单个简单类型参数包括:

byte short int long float double char

Byte Short Integer Long Double Char

String

java.util.Date

java.sql.Date

持久层接口

pojo

映射文件

复制代码
<?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.example.mapper.StuMapper">
    <!--
    parameterType属性的作用:
        告诉mybatis框架,我这个方法的参数类型是什么类型。
        mybatis框架自身带有类型自动推断机制,所以大部分情况下parameterType属性都是可以省略不写的。

        SQL语句最终是这样的:
            select * from t_student where id = ?
        JDBC代码是一定要给?传值的。
        怎么传值?ps.setXxx(第几个问号, 传什么值);
            ps.setLong(1, 1L);
            ps.setString(1, "zhangsan");
            ps.setDate(1, new Date());
            ps.setInt(1, 100);
            ...
        mybatis底层到底调用setXxx的哪个方法,取决于parameterType属性的值。

    注意:mybatis框架实际上内置了很多别名。可以参考开发手册。

    -->
    <select id="selectById" resultType="student" parameterType="java.lang.Long">
        select * from t_student where id=#{id}
    </select>

    <select id="selectByName" resultType="student">
        select * from t_student where name=#{name}
    </select>

    <select id="selectByBirth" resultType="student">
        select * from t_student where birth=#{birth}
    </select>

    <select id="selectBySex" resultType="student">
        select * from t_student where sex=#{sex}
    </select>
</mapper>

测试代码

复制代码
import com.example.mapper.StuMapper;
import com.example.pojo.Student;
import com.example.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author hrui
 * @date 2023/9/19 1:24
 */
public class Test {
    @org.junit.Test
    public void selectBySex(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        List<Student> students = mapper.selectBySex('男');
        students.forEach(student-> System.out.println(student));
        sqlSession.close();
    }
    @org.junit.Test
    public void selectByBirth() throws ParseException {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        //List<Student> students = mapper.selectByBirth(new Date(1978-1900,1,3));

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse("1980-10-11");
        List<Student> students = mapper.selectByBirth(parse);
        students.forEach(student-> System.out.println(student));
        sqlSession.close();
    }
    @org.junit.Test
    public void selectByName(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        List<Student> students = mapper.selectByName("张三");
        students.forEach(student-> System.out.println(student));
        sqlSession.close();
    }

    @org.junit.Test
    public void selectById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        List<Student> students = mapper.selectById(1L);
        students.forEach(student-> System.out.println(student));
        sqlSession.close();
    }
}

加上这个或许效率高一点,但是一般不这么麻烦,不需要

2.Map参数

map也可以这样,#{XXX}是key就行

3.实体类参数

4.多参数

假如说在没有使用@Param指定参数名时候,你非想试试其他的

这里注意下,低版本mybatis可以写#{0},#{1}

上面也可以混着用 比如第一个参数#{arg0} 第二个参数#{param2} 也是可以的

使用@Param指定名字 这样 arg0 arg1就失效了 但是param1 param2还是可以用

相关推荐
ClutchoQ10 小时前
【你指的API是哪个API?软件工程师跨服聊天实录】
笔记·其他
二哈赛车手12 小时前
新人笔记---Spring AI的Advisor以及其底层机制讲解(涉及源码),包含一些遇见的Spring AI的Advisor缺陷问题的解决方案
java·人工智能·spring boot·笔记·spring
red_redemption13 小时前
自由学习记录(181)
学习
wuxinyan12313 小时前
大模型学习之路007:RAG 零基础入门教程(第四篇):生成侧核心技术与大模型集成
人工智能·学习·rag
Java成神之路-13 小时前
面试题:MyBatis延迟加载的底层原理
mybatis
阿豪只会阿巴14 小时前
【没事学点啥】TurboBlog轻量级个人博客项目——Turbo Blog 项目学习与上线指南
开发语言·python·学习·状态模式
Slow菜鸟14 小时前
Docker 学习篇(三)| Docker安装指南(Linux版)
linux·学习·docker
Tutankaaa14 小时前
知识竞赛软件SaaS版 vs 本地部署
人工智能·经验分享·笔记·学习
小仙女的小稀罕14 小时前
培训要点写不完不会整理?规范培训转待办可这样操作
大数据·人工智能·学习·自然语言处理·语音识别
许长安14 小时前
RPC 异步调用基本使用方法:基于官方helloworld-async 示例
c++·经验分享·笔记·rpc