<!--按联表进行映射--> <resultMapid="studentTeacher1"type="Student"> <resultproperty="id"column="sid"/> <resultproperty="name"column="sname"/> <associationproperty="teacher"javaType="Teacher"> <resultproperty="name"column="tname"/> <resultproperty="id"column="tid"/> </association> </resultMap> <selectid="getAll"resultMap="studentTeacher1"> select s.id as sid, s.name as sname, t.name as tname, t.id as tid from t_student as s, t_teacher as t where s.tid = t.id </select> <!--按子查询进行映射--> <resultMapid="studentTeacher2"type="Student"> <associationproperty="teacher"javaType="Teacher"select="getTeacher"column="tid"/> </resultMap>
<selectid="getAllBySubquery"resultMap="studentTeacher2"> select * from t_student </select> <selectid="getTeacher"resultType="Teacher"> select * from t_teacher t where t.id=#{id}; </select>
<selectid="getAll"resultMap="TeacherStudent1"> select s.id as sid, t.id as tid, s.name as sname, t.name as tname from t_teacher as t, t_student as s where t.id=s.tid </select> <!--按子查询进行映射--> <resultMapid="TeacherStudent2"type="Teacher"> <resultcolumn="tid"property="id"/> <resultcolumn="tname"property="name"/> <collectionproperty="students"ofType="Student"select="getStudent"column="tid"/> </resultMap> <selectid="getAllSubquery"resultMap="TeacherStudent2"> select id as tid, name as tname from t_teacher </select> <selectid="getStudent"resultType="Student"> select * from t_student where tid=#{id} </select>
<selectid="getCourseIf"parameterType="map"resultType="Course"> select * from t_course where 1=1 <includerefid="if-id-name"></include> </select> <selectid="getCourseChoose"resultType="Course"> select * from t_course where 1=1 <choose> <!--如果满足when条件,则拼接when中的sql语句--> <whentest="name !=null"> and name like #{name} </when> <!--如果没有when条件满足,则拼接otherwise中的sql语句--> <otherwise> and id = 1 </otherwise> </choose> </select> <!-- where标签的含义是,如果有条件,则会sql自动地添加上where,如果没有条件,则不会添加where ,如果where前面是连接关键字的话,会把关键字给去掉--> <selectid="getCourseWhere"resultType="Course"> select * from t_course <where> <includerefid="if-id-name"></include> </where> </select> <!-- select * from t_course where (id = ? or id = ?...)--> <selectid="getCourseForeach"resultType="Course"> select * from t_course <where> <!--这里的集合是从map中传递过来的,这里的值必须与map的key一致,而item的值必须与后面的条件#{}中的值一致,否则值将无法传入--> <!--这里open的and是为了如果前面有条件而添加的,如果没有where会将其去掉--> <foreachcollection="ids"item="id" open="and ("close=")"separator="or"> id = #{id} </foreach> </where> </select>
Sql片段
讲公共部分的sql抽取出来,如下:
1 2 3 4 5 6 7 8 9
<sqlid="if-id-name"> <iftest="id != null"> id = #{id} </if> <!--从第二个标签开始需要加连接关键字--> <iftest="name != null"> and name = #{name} </if> </sql>
然后在要插入的地方使用include,如下:
1 2 3 4 5 6 7 8 9 10
<selectid="getCourseIf"parameterType="map"resultType="Course"> select * from t_course where 1=1 <includerefid="if-id-name"></include> </select> <selectid="getCourseWhere"resultType="Course"> select * from t_course <where> <includerefid="if-id-name"></include> </where> </select>
try(final SqlSession sqlSession = MybatisUtils.getSqlSession()){ final CourseMapper mapper = sqlSession.getMapper(CourseMapper.class); final HashMap<Object, Object> map = new HashMap<>(); final List<Integer> list = Arrays.asList(2, 3); map.put("ids", list); final List<Course> courses = mapper.getCourseForeach(map); for (Course cours : courses) { System.out.println(cours); } //二级缓存会读取一级缓存进行保存 } //第二次查询数据的时候会从二级缓存中读取数据 try(final SqlSession sqlSession = MybatisUtils.getSqlSession()){ final CourseMapper mapper = sqlSession.getMapper(CourseMapper.class); final HashMap<Object, Object> map = new HashMap<>(); final List<Integer> list = Arrays.asList(2, 3); map.put("ids", list); final List<Course> courses = mapper.getCourseForeach(map); for (Course cours : courses) { System.out.println(cours); } }