【MyBatis】多种参数处理

1. 单个简单类型参数:

  • mybatis会自动的进行类型推断, 因此parameterType属性可以不用写

  • 简单类型包括:

1. byte short int long float double char
2. Byte Short Integer Long Float Double Character
3. String
4. java.util.Date
5. java.sql.Date
  • 单个参数Long类型:

parameterType属性的作用:
    告诉mybatis框架, 我的这个方法的参数类型是什么类型
    mybatis框架自带有类型自动推断机制, 所以大部分情况下parameterType都是可以省略不写的

sql语句最终是这样的:
    select * from student where id = ?
JDBC的代码一定要给?传值的 ,  怎么传值
    ps.setXxx(第几个问号, 传什么值)
    ps.setLong(1, 1L)
    ps.setString(1, "张三")
    mybatis底层就调用setXxx的哪个方法, 取决于parameterType的属性

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

// 接口
public interface StudentMapper{
    List<Student> selectById(Long long);
}

// @test
public static void main(String[] args){
    SqlSession SqlSession = SqlSessionUtil.openSession();
    StudentMapper mapper = SqlSession.getMapper(StudentMapper.class);
    List<Student> students = mapper.selectById(1L);
    students.forEach(student -> System.out.println(student));
    SqlSession.close();
}
  • 单个参数Date类型:

// StudentMapper.xml
<select id="selectByBirth" resultType="Student">
    select * from student where birth = #{birth}
</select>

// 接口
public interface StudentMapper{
    List<Student> selectByBirth(Date birth);
}

// @test
public static void main(String[] args){
    SqlSession sqlSession = SqlSessionUtil.openSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date birth = sdf.parse("2020-10-10");

    List<Student> students = mapper.selectByBirth(birth);
    students.forEach(student -> System.out.println(student));

    sqlSession.close();
}

// char --> Character
Character sex = Character.valueOf('男');
List<Student> students = mapper.selectBySex(sex);

2. Map参数:

// StudentMapper.xml
// #{}里面写的是map集合的key 
// 此处parameterType="map"省略不写也可以
<insert id="insertStudentByMap" parameterType="map">
    insert into student(id,name,age,sex,birth,height)
            values(null,#{姓名},#{年龄},#{性别},#{生日},#{身高})
</insert>

// 接口
public interface StudentMapper{
    int insertStudentByMap(Map<String,Object> map);
}

// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

    Map<String,Object> map = new HashMap<>();
    map.put("姓名","李四");
    map.put("年龄",6);
    map.put("身高",179);
    map.put("性别",'男');
    map.put("生日",new Date());

    mapper.insertStudentByMap(map);
    sqlSession.commit();
    sqlSession.close();
}

3. 实体类参数:

// StudentMapper.xml
// 此处parameterType="Student"省略不写,也可以
<insert id="insertStudentByPOJO" parameterType="Student">
    insert into student(id,name,age,sex,birth,height)
            values(null,#{name},#{age},#{sex},#{birth},#{height})
</insert>

// 接口
public interface StudentMapper{
    // student是单个参数, 不是简单类型
    int insertStudentByPOJO(Student student);
}

// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    
    Student student = new Student(.....需要传的参数......);
    mapper.insertStudentByPOJO(student);

    sqlSession.commit();
    sqlSession.close();
}

4. 多参数:

4.1 arg0和arg1:

// StudentMapper.xml
<select id="selectByNameAndSex" resultType="Student">
    select * from student where name = #{arg0} and sex = #{arg1}
</select>

// 接口
public interface StudentMapper{
    // 多个参数!
    // mybatis底层会自动创建一个Map集合, 并且Map集合是以下面这种方式存储参数的.
    //     map.put("arg0",name);
    //     map.put("arg1",sex);
    //     map.put("param1",name);
    //     map.put("param2",sex);
    List<Student> selectBYNameAndSex(String name,Character sex);
}

// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    
    List<Student> students = mapper.selectByNameAndSex("张三",'男');
    students.forEach(student -> System.out.println(student));
    sqlSession.close();
}

4.2 使用@Param注解:

  • 使用了@Param注解之后, 再使用arg0/arg就会报错, 使用param1/param2则不会

  • 核心: @Param("这里填写的其实就是map集合的key")

// StudentMapper.xml
// 此时大括号里面可以使用name,sex,param1, param2
<select id="selectByNameAndSex2" resultType="Student">
    select * from student where name = #{name} and sex = #{sex}
</select>

// 接口
public interface StudentMapper{
    // 多个参数!
    // mybatis框架底层的实现原理
    //     map.put("name",name);
    //     map.put("sex",sex);
    List<Student> selectBYNameAndSex2
            (@Param("name") String name, @Param("sex") Character sex);
}

// @test
public static void main(String[] args) {
    SqlSession sqlSession = SqlSessionUtil.openSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    
    List<Student> students = mapper.selectByNameAndSex2("张三",'男');
    students.forEach(student -> System.out.println(student));
    sqlSession.close();
}

4.3 @Param注解源码分析:

猜你喜欢

转载自blog.csdn.net/qq_68993495/article/details/128843758
今日推荐