这个参数处理的参数指的是接口里面方法传的参数
简单类型包括(七种基本数据类型加七种包装类再加String和两种Date)
<select id="selectById" resultType="Student" parameterType="java.lang.Long">
select * from t_student where id = #{id}
select>
parameterType
属性的作用:
告诉mybatis框架,我这个方法的参数类型是什么类型
mybatis框架自身带有类型自动判断机制,所有大部分情况下parameterType
属性是可以省略不写的
SQL语句最终是这样子的
select * form t_student where id = ?
JDBC代码是一定要给?传值的
怎么传值呢?
ps.seyXxx
ps.setLong(1,1L);
ps.setString(1,“zhangsan”)
…
mybatis底层到底调用setXxx的哪个方法,取决于parameterType属性的值
注意:mybatis底层实际上内置了很多别名,可以参考开发手册。https://mybatis.net.cn/configuration.html(这里的类型别名那个tag里)
<insert id="insertStudentByMap" parameterType="map">
insert into t_student(id,name,age,sex,birth,height) values (null,#{姓名},#{年龄},#{性别},#{生日},#{身高})
insert>
@Test
public void testInsertStudentByMap(){
SqlSession sqlSession = sqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("年龄","赵六");
map.put("年龄",20);
map.put("身高",1.81);
map.put("性别",'男');
map.put("生日",new Date());
mapper.insertStudentByMap(map);
sqlSession.commit();
sqlSession.close();
}
这种方式是手动封装Map集合,将每个条件以key和value的形式存放到集合中。然后在使用的时候通过#{map集合的key}来取值
这个好像在刚讲CRUD的时候有演示
<insert id="insertStudentByPojo" parameterType="studet">
insert into t_student(id,name,age,sex,birth,height) values (null,#{name},#{age},#{sex},#{birth},#{height})
insert>
public void testInsertStudentByPojo(){
SqlSession sqlSession = sqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student();
student.setName("张飞"):
student.setAge(50):
student.setSex('女'):
student.setBirth(new Date()):
student.setHeight(10.0):
mapper.insertStudentByMap(student);
sqlSession.commit();
sqlSession.close();
}
如果是多个参数,mybatis框架底层是怎么做的呢?
mybatis框架会自动创建一个Map集合,并且Map集合是以这种方式存储参数的:
map.put(“arg0”,name);
map.put(“arg1”,sex);
map.put(“param1”,name);
map.put(“param2”,sex)
List<Student> selectByNameAndSex(String name, Charater sex);
<select id="selectByNameAndSex" resultType="Student">
select * from t_student where name = #{name} and sex = #{sex}
select>
@Test
public void testSelectByNameAndSex(){
SqlSession sqlsession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlsession.getMapper(StudentMapper.class);
List<Student> students = mapper.selectByNameAndSex("张三","男");
students.forEach(student -> System.out.println(student));
sqlsession.close();
}
代码会报错
Error querying database. Cause:org,apache.ibatis.binding,BindingException: Parameter 'name' not found. Available parameters are [arg1,arg0,param1,param2]
就是说这个name参数找不到,有效的参数是arg1、arg0、param1、param2
所以要改成:
<select id="selectByNameAndSex" resultType="Student">
select * from t_student where name = #{arg0} and sex = #{arg1}
select>
或者
<select id="selectByNameAndSex" resultType="Student">
select * from t_student where name = #{param1} and sex = #{param2}
select>
接上回的arg几和param几的,可读性太差,就可以使用@Param注解来指定名字
List<Student> selectByNameAndSex2(@Param("name") String name, @Param("sex") Charaster sex);
<select id="selectByNameAndSex2" resultType="Student">
select * from t_student where name = #{name} and sex = #{sex}
select>
但是还是可以用param几的,只是将arg的替换掉了