10. Mybatis 参数处理

环境准备:

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

创建数据库表: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 mapper
        PUBLIC "-//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 {
    @Test
    public 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();
    }

    @Test
    public 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();
    }

    @Test
    public void testSelectByName() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> names = mapper.selectByName("陈平安");
        System.out.println(names);
        sqlSession.close();
    }

    @Test
    public void testSelectById() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(1L);
        System.out.println(student);
        sqlSession.close();
    }
}

posted @ 2024-06-24 10:21  捞月亮的小北  阅读(3)  评论(0编辑  收藏  举报