<?xml version="1.0" encoding="UTF-8" ?><!--MyBatis的DTD約束--><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--mapper:核心根標簽namespace屬性:名稱空間
--><mappernamespace="com.mybatis.mapper.StudentMapper"><!--select:查詢功能的標簽id屬性:唯一標識resultType屬性:指定結果映射對象類型parameterType屬性:指定參數映射對象類型--><selectid="selectAll"resultType="student">SELECT * FROM student</select><selectid="selectById"resultType="student"parameterType="int">SELECT * FROM student WHERE id = #{id}</select><!--對于增刪改返回的都是影響行數,所以resultType屬性可以不寫--><insertid="insert"parameterType="student">INSERT INTO student VALUES (#{id},#{name},#{age})</insert><updateid="update"parameterType="student">UPDATE student SET name = #{name},age = #{age} WHERE id = #{id}</update><deleteid="delete"parameterType="int">DELETE FROM student WHERE id = #{id}</delete></mapper>
publicclassStuTest{// 創建業務層對象privateStudentService service =newStudentMapperImpl();//查詢全部功能測試@TestpublicvoidselectAll(){List<Student> students = service.selectAll();for(Student stu : students){System.out.println(stu);}}//根據id查詢功能測試@TestpublicvoidselectById(){Student stu = service.selectById(3);System.out.println(stu);}//新增功能測試@Testpublicvoidinsert(){Student stu =newStudent(4,"趙六",26);Integer result = service.insert(stu);System.out.println(result);}//修改功能測試@Testpublicvoidupdate(){Student stu =newStudent(4,"趙六",16);Integer result = service.update(stu);System.out.println(result);}//刪除功能測試@Testpublicvoiddelete(){Integer result = service.delete(4);System.out.println(result);}}