一:单个简单类型参数
parameterType:表示参数的类型,指定mapper方法的形参数据类型。这个形参的数据类型是给mybatis给sql语句的参数赋值时使用,使用的时候有两种方法:
1.方法一
java类型的全限定类型名称 parameterType = “java.lang.Integer”,也可以是自定义的实体类对象;
DELETE FROM student WHERE id = #{id} 2.方法二
如果是常见的数据类型,可以使用别名映射的类型;
别名 | 映射类型 | 别名 | 映射类型 |
_byte | byte | _long | long |
_short | short | _int | int |
_integer | int | _double | double |
_float | float | Byte | byte |
_boolean | boolean | Long | long |
String | string | Short | short |
Integer | int | Date | date |
Integer | integer | Object | object |
Double | double | Map | map |
Float | float | HashMap | hashmap |
Boolean | boolean | List | list |
ArrayList | arraylist | Collection | collection |
BigDecimal | bigdecimal | Iterator | iterator |
3.方法三
对于简单的类型(简单类型:八种基本类型 + String),parameterType不写也是可以的,parameterType不是强制的,mybatis通过反射机制是可以得到参数类型的,绝大多数情况下我们不需要写这个参数。
二、多个简单参数
1.参数传值
若mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合中以arg0,arg1…为键,以参数为值,或者以param1,param2…为键,以参数为值;
因此只需要通过${}和#{}访问map集合的键就可以获取相对应的值,注意${}需要手动加单引号。
使用arg或者param都行,要注意的是,arg是从arg0开始的,param是从param1开始的。
/** * 根据姓名和年龄查询学生信息 * @param name * @param age * @return */ public List selectStuByNameAndAge(String name,Integer age); @Test public void selectStuByNameAndAge() { //读取配置文件到数据流 InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); //创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); //创建SqlSession //openSession(true) 表示提交事务 SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取StudentMapper StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); //查询信息 List studentList = mapper.selectStuByNameAndAge("小明", 18); studentList.forEach(student -> System.out.println(student)); } 如果使用param的方式,使用如下,不过需要注意的是,arg0从0开始,param1从1开始;
② 使用@Param
如果mapper中的参数是多个,需要使用@Param这个注解,解释某一个参数对应类型及名称;@Param:参数在方法的形参前面使用的,定义参数名。
@Param("name") 中的name需要和mapper.xml语句中的参数一致,通过对应告诉给对应的参数的赋值;
返回值类的包装是和查询出来的字段一致的,字段和实体类的映射确定能否包装;
/** * 根据姓名和年龄查询学生信息 * @param name * @param age * @return */ public List selectStuByNameAndAge(@Param("name") String name, @Param("age") Integer age); @Testpublic void selectStuByNameAndAge() { //读取配置文件到数据流 InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); //创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); //创建SqlSession //openSession(true) 表示提交事务 SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取StudentMapper StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); //查询信息 List studentList = mapper.selectStuByNameAndAge("小明", 18); studentList.forEach(student -> System.out.println(student));} 三、使用对象作为参数
方法地形参是一个javabeen对象,这个javabeen对象表示多个参数。使用对象的属性值作为参数使用,这个时候需要参数的名称和对应的属性名称一致;
/** * 添加学生信息 * @param student * @return */ public int addStudent(Student student); insert into student(id, name, age, address, clazzno) values (#{id}, #{name}, #{age}, #{address}, #{clazzno}) @Testpublic void insertStudent() { //读取配置文件到数据流 InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); //创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); //创建SqlSession //openSession(true) 表示提交事务 SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取StudentMapper StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); //学生信息实例 Student student=new Student(); student.setId("6"); student.setName("小花"); student.setAge(18); //执行查询数据方法 int i = mapper.addStudent(student); //打印结果 System.out.println("i = " + i);}map作为一个万能的参数十分好用,如果存在多个参数,可以把参数直接封装到map中,使用key和待赋值的字段名称一致,value直接给字段赋值;
/** * 根据map参数查询学生信息 * @param map 查询参数封装的map * @return */ public List selectStuByMap(Map map); @Testpublic void selectStuByMap() { // 读取配置文件到数据流 InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); // 创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); //创建SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); //获取StudentMapper StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); //构造查询参数的map集合 Map map = new HashMap<>(); map.put("name", "小明"); map.put("age", "18"); //根据map查询学生信息 List studentList = mapper.selectStuByMap(map); //打印学生信息 studentList.forEach(student -> System.out.println(student));} | 留言与评论(共有 0 条评论) “” |