<?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.wangYuan.mapper.EmpMapper">
<!--
测试入参类型
基本数据类型|包装类 String Date JAVABEAN 数组 List Map
JAVABEAN : 当sql需要的参数存在多个,并且为一个对象的不同属性,可以先把对个参数封装成javabean对象,然后作为一个入参传递
sql中占位符的名字需要匹配入参对象的属性名
数组|list :sql需要的多个参数是同种类型,一个字段的多个值,适合使用数组|list
sql语句中需要使用foreach标签进行遍历
Map : sql需要的多个参数,不属于一个对象的不不同属性值,可以封装为Map集合中的不同键值对
sql中占位符的名字需要匹配键值对的key
-->
<!--int-->
<select id="queryByDeptno" parameterType="int" resultType="Emp">
select * from emp where deptno=#{deptno}
</select>
<!--String-->
<select id="queryByName" parameterType="String" resultType="Emp">
select * from emp where ename=#{ename}
</select>
<!--Date-->
<select id="queryByDate" parameterType="Date" resultType="Emp">
select * from emp where hiredate=#{date}
</select>
<!-- JAVABEAN-->
<select id="queryByEnameDeptno" parameterType="emp" resultType="Emp">
select * from emp where ename=#{ename} or deptno=#{deptno}
</select>
<!-- 数组-->
<select id="queryByIdAll" resultType="Emp">
select * from emp where empno in (
<!--遍历数组元素 遍历出来的元素叫item 每个元素之间用,隔开-->
<foreach collection="array" item="item" separator=",">
#{item}
</foreach>
)
</select>
<!--Map-->
<select id="queryBySalDeptno" resultType="emp" parameterType="map">
select * from emp where sal>#{sal} or deptno=#{deptno}
</select>
</mapper>
java代码测试
public class test03 {
//测试不同的入参类型
public static void main(String[] args) throws ParseException {
//获取sql连接
SqlSession sqlSession = sessionUtils.getSqlSession();
List<Emp> list = sqlSession.selectList("com.wangYuan.mapper.EmpMapper.queryByDeptno", 20);
list = sqlSession.selectList("com.wangYuan.mapper.EmpMapper.queryByName", "wang");
Date parse = new SimpleDateFormat("yyyy/MM/dd").parse("1981/12/3");
list = sqlSession.selectList("com.wangYuan.mapper.EmpMapper.queryByDate", parse);
Emp emp = new Emp();
emp.setEname("ALLEN");
emp.setDeptno(20);
list = sqlSession.selectList("com.wangYuan.mapper.EmpMapper.queryByEnameDeptno", emp);
list = sqlSession.selectList("com.wangYuan.mapper.EmpMapper.queryByIdAll", new int[]{6666,7369});
list = sqlSession.selectList("com.wangYuan.mapper.EmpMapper.queryBySalDeptno", Map.of("sal","1500","deptno","30"));
list.forEach(System.out::println);
}
}