10. Mybatis 参数处理

news/2024/9/22 1:18:52

环境准备:

创建模块 ,引入相关依赖 和 配置文件

创建数据库表:t_student

image

向数据库表中填充数据

image

  • 创建pojo类 :student

    public class Student {private Long id;private String name;private Integer age;private Double height;private Date birth;private Character sex;
    }
    

1. 单个简单类型参数

简单类型包括:

  • byte short int long float double char
  • Byte Short Integer Long Float Double Character
  • String
  • java.util.Date
  • java.sql.Date

通过下面所写代码测试得知,简单类型对于mybatis来说都是可以自动类型识别的:

  • 也就是说对于mybatis来说,它是可以自动推断出ps.setXxxx()方法的。ps.setString()还是ps.setInt()。它可以自动推断。

其实SQL映射文件中的配置比较完整的写法是

<select id="selectByName" resultType="student" parameterType="java.lang.String">select * from t_student where name = #{name, javaType=String, jdbcType=VARCHAR}
</select>

其中sql语句中的javaType,jdbcType,以及select标签中的parameterType属性,都是用来帮助mybatis进行类型确定的。不过这些配置多数是可以省略的。因为mybatis它有强大的自动类型推断机制。

  • javaType:可以省略
  • jdbcType:可以省略
  • parameterType:可以省略

如果参数只有一个的话,#{} 里面的内容就随便写了。对于 ${} 来说,注意加单引号。

StudentMapper接口 , 需求:根据name查、根据id查、根据birth查、根据sex查

package com.north.mybatis.mapper;import com.north.mybatis.pojo.Student;import java.util.Date;
import java.util.List;/*** @Author North* @Date 2024/4/7*/
public interface StudentMapper {Student selectById(Long id);List<Student> selectByName(String name);List<Student> selectByBirth(Date birth);List<Student> selectBySex(char sex);
}

StudentMapper.xml 映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.north.mybatis.mapper.StudentMapper"><select id="selectById" resultType="Student">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>

测试类:

package com.north.mybatis.test;import com.north.mybatis.mapper.StudentMapper;
import com.north.mybatis.pojo.Student;
import com.north.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;/*** @Author North* @Date 2024/4/7*/
public class StudentMapperTest {@Testpublic void testSelectBySex() {SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);List<Student> students = mapper.selectBySex('女');students.forEach(student -> System.out.println(student));sqlSession.close();}@Testpublic void testSelectByBirth() throws ParseException {SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date birth = simpleDateFormat.parse("2001-8-16");List<Student> students = mapper.selectByBirth(birth);students.forEach(student -> System.out.println(student));sqlSession.close();}@Testpublic void testSelectByName() {SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);List<Student> names = mapper.selectByName("陈平安");System.out.println(names);sqlSession.close();}@Testpublic void testSelectById() {SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student = mapper.selectById(1L);System.out.println(student);sqlSession.close();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/47182.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

【C++】【MFC】MFC多文档框架

相较于单文档架构,多文档基本架构则是有起码两个框架窗口,与其相对应的也会有两个文档类进行数据管理。 参与架构的类:CMDIFrameWnd / CMDIChildWnd / CWinApp / CView / CDocument 需要用的类:CDocTemplate(文档模板类)|-> CMultiDocTemplate(多文档模板类)CDocMan…

网络物理隔离后 可以用保密U盘进行数据安全交换吗?

企业用的保密U盘通常被设计用于存储和传输敏感信息,以确保数据的安全和保密性。在网络之间实现了物理隔离后,使用保密U盘进行数据安全交换是一种常见的做法。物理隔离确保了两个网络之间的完全分离,因此使用保密U盘可以作为一种安全的手段来在这两个网络之间传输数据。 以下…

CTH: 谁帮我切开这个蛋糕???

$\quad $ 看到CTH立马就开始做了好吧,很适合当做入门题。$\quad $ 首先定义 \(f[i]\) 表示进行到第 \(i\) 位时的答案数,\(bit\) 数组表示 \(01\) 序列。那么当 \(bit[i]\) 为 \(1\) 时,有 \[f[i]=\Sigma_{j=i+1}^{n+1} f[j] \]$\quad $ 至于为什么循环到 \(n+1\) ,循环到第…

MyBatis针对String类型的数字if标签失效问题

需求描述: 大致场景是订单模块去接受流程模块发送的MQ消息,针对MQ消息发送的是一个实体类,该实体类中有一个String类型的字段,用于判断当前业务状态,1 表示 审核中 2 表示 已审核 等。订单模块根据这个状态去修改自身状态的信息可以看到这里有一个If标签,用于判断这个ev…

业务和IT部门都喜欢的文件摆渡设备是什么样的?

网络隔离技术作为网络安全和数据安全的重要保障手段被广泛应用到各个行业领域,国家出台了相关法律法规,对“网络隔离”和“数据交换安全”提出了更明确、更规范的要求,尤其是对于政府、教育、医疗、能源、航空航天、金融等行业,国家出台了更细的行业性法规,指导网络和数据…

颠覆传统编程,用ChatGPT十倍提升生产力

我们即将见证一个新的时代!这是最好的时代,也是最坏的时代!我们即将见证一个新的时代!这是最好的时代,也是最坏的时代!需求背景背景:平时会编写博客,并且会把这个博客上传到github上,然后自己买一个域名挂到github上。 我平时编写的博客会有一些图片来辅助说明的,写完…

Java开发者的神经网络进阶指南:深入探讨交叉熵损失函数

在本文中,我们深入探讨了交叉熵函数作为一种重要的损失函数,特别适用于神经网络训练中。交叉熵通过衡量真实标签分布与模型预测分布之间的差异,帮助优化模型的性能。我们从信息论的角度解释了交叉熵的概念,它是基于Shannon信息论中的熵而来,用于度量两个概率分布之间的差异…

结构型设计模式

适配器模式 需求方法M1。但已经存在一个方法M2能实现需求功能,引入子类来覆盖M1方法(M1方法中调用已有的M2方法)。这个新子类就是适配器 将已有的方法转换为需求的另一种方法(一般由于方法名差异;参数不同) 这一模式中的“接口”是广义接口,可代指一个/一组方法集合 优点…