Introduction
In this project, you will learn how to use MyBatis to perform various operations on a course table, including querying course information, updating course teacher, and batch querying course data.
👀 Preview



🎯 Tasks
In this project, you will learn:
- How to query course information whose course name starts with an uppercase "C" using dynamic SQL
- How to update the teaching teacher of a specific course while keeping others unchanged, using dynamic SQL
- How to batch query course information with a list of course numbers, using dynamic SQL
🏆 Achievements
After completing this project, you will be able to:
- Use MyBatis input and output parameters
- Implement dynamic SQL in MyBatis
- Use result mapping to specify the result set mapping
- Use the
<foreach>tag for batch queries
Query Course Information
In this step, you will learn how to query the course information whose course name starts with an uppercase "C" using dynamic SQL.
Open the
CourseMapper.javafile located in the/org/lanqiao/mapper/directory.Implement the
selectCoursesmethod in theCourseMapperinterface. This method should accept aMap<String, Object>parameter to hold the input parameters.
List<Course> selectCourses(Map<String, Object> params);
Open the
CourseMapper.xmlfile located in the/org/lanqiao/mapper/directory.Implement the
selectCoursesSQL statement in theCourseMappernamespace. Use dynamic SQL to check if thecourseNameparameter is empty. If it is not empty, add a condition to filter the course names starting with an uppercase "C".
<select id="selectCourses" resultMap="courseResultMap" parameterType="java.util.Map">
SELECT * FROM course
<where>
<if test="courseName != null and courseName != ''">
AND cName LIKE #{courseName}
</if>
</where>
</select>
- Define the
courseResultMapin theCourseMapper.xmlfile to specify the result set mapping.
<resultMap id="courseResultMap" type="org.lanqiao.pojo.Course">
<id property="cNo" column="cNo"/>
<result property="cName" column="cName"/>
<result property="teacher" column="teacher"/>
</resultMap>
Open the
MyBatisTest.javafile located in the/org/lanqiao/test/directory.Implement the
testSelectCoursestest method to test theselectCoursesfunctionality.
@Test
public void testSelectCourses() {
CourseMapper mapper = session.getMapper(CourseMapper.class);
Map<String, Object> params = new HashMap<>();
params.put("courseName", "C%");
List<Course> courses = mapper.selectCourses(params);
System.out.println(courses);
}
Update Course Teacher
In this step, you will learn how to update the teaching teacher of the course with the course number "7" to "Blue" using dynamic SQL.
Open the
CourseMapper.javafile located in the/org/lanqiao/mapper/directory.Implement the
updateTeachermethod in theCourseMapperinterface. This method should accept aMap<String, Object>parameter to hold the input parameters.
void updateTeacher(Map<String, Object> params);
Open the
CourseMapper.xmlfile located in the/org/lanqiao/mapper/directory.Implement the
updateTeacherSQL statement in theCourseMappernamespace. Use dynamic SQL to check if theteacherNameparameter is empty. If it is not empty, update theteachercolumn for the course with the specifiedcourseId.
<update id="updateTeacher" parameterType="java.util.Map">
UPDATE course
<set>
<if test="teacherName != null and teacherName != ''">
teacher = #{teacherName},
</if>
</set>
WHERE cNo = #{courseId}
</update>
Open the
MyBatisTest.javafile located in the/org/lanqiao/test/directory.Implement the
testUpdateTeachertest method to test theupdateTeacherfunctionality.
@Test
public void testUpdateTeacher() {
CourseMapper mapper = session.getMapper(CourseMapper.class);
Map<String, Object> params = new HashMap<>();
params.put("courseId", 7);
params.put("teacherName", " Blue");
mapper.updateTeacher(params);
session.commit();
}
Batch Query Course Information
In this step, you will learn how to batch query the course information with the course numbers "1,2,3" using dynamic SQL.
Open the
CourseMapper.javafile located in the/org/lanqiao/mapper/directory.Implement the
selectCoursesByIdsmethod in theCourseMapperinterface. This method should accept aMap<String, Object>parameter to hold the input parameters.
List<Map<String, Object>> selectCoursesByIds(Map<String, Object> params);
Open the
CourseMapper.xmlfile located in the/org/lanqiao/mapper/directory.Implement the
selectCoursesByIdsSQL statement in theCourseMappernamespace. Use the<foreach>tag to iterate over theidslist and build the IN clause.
<select id="selectCoursesByIds" resultMap="courseResultMap" parameterType="java.util.Map">
SELECT * FROM course WHERE cNo IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
Open the
MyBatisTest.javafile located in the/org/lanqiao/test/directory.Implement the
testSelectCoursesByIdstest method to test theselectCoursesByIdsfunctionality.
@Test
public void testSelectCoursesByIds() {
CourseMapper mapper = session.getMapper(CourseMapper.class);
List<Integer> ids = Arrays.asList(1, 2, 3);
Map<String, Object> params = new HashMap<>();
params.put("ids", ids);
List<Map<String, Object>> coursesByIds = mapper.selectCoursesByIds(params);
System.out.println(coursesByIds);
}
Run
To make sure that the code works, take care to introduce relevant classes into the code.
CourseMapper.java:
import org.lanqiao.pojo.Course;
import java.util.List;
import java.util.Map;
MyBatisTest.java:
import java.util.Arrays;
import java.util.HashMap;
Finally you can compile and run the code in the terminal and check the test files with Maven, using the following commands:
cd MyBatisCourseDemo03
mvn test
You can refer to 👀 Preview for a running rendering.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



