(代码小白自我总结,仅供参考)
关键词:参数传递,sql注入,
映射文件mapper 下的namespace
是属性,属性的键必须小写,值一律是字符串
前期为了解决映射文件中命名空间相同的情况区分id,后期强制要求namespace的值是一个接口
传参(一个,多个)
1,单个:pojo(要求映射文件#中的名字必须和pojo属性完全一致)
接口,
//pojo Student select1(Student stu);
映射
<select id="select1" parameterType="student" resultType="Student"> select * from student where sid=#{sid} and sname=#{sname} </select>
测试
@Test public void select1(){ Student pojo = new Student(6, "子路"); SqlSession session = factory.openSession(); StudentMapper mapper = session.getMapper(StudentMapper.class); Student student = mapper.select1(pojo); System.out.println(student); }
2,单个map:有泛型<>(要求映射文件sql占位符中的名字必须和map参数中的key完全一致)
<haspmap相关代码>
map以key 和value为主
通过键找对应的值
接口
//map Student select2(Map<String,Object > m);
映射
<select id="select2" parameterType="map" resultType="Student"> select * from student where sid=#{aa} and sname=#{b} </select>
测试
@Test public void select2(){ Map<String,Object> map = new HashMap<>(); map.put("aa",6); map.put("b","子路"); SqlSession session = factory.openSession(); StudentMapper mapper = session.getMapper(StudentMapper.cl