在调用mybatis的查询条件时,之前,遇到需要验证多个参数的查询时,往往需要把所有参数都绑定到一个实体中去,然后调用获取。

现在,我们来详细描述mybatis传递参数的细节。

  1. 1 package com.xm.mapper;
  2. 2
  3. 3 import java.util.List;
  4. 4
  5. 5 import com.xm.pojo.Student;
  6. 6
  7. 7 public interface StudentMapper {
  8. 8
  9. 9
  10. 10 /**
  11. 11 * 根据name查询
  12. 12 * @param name
  13. 13 * @return
  14. 14 */
  15. 15 public Student getByName(String name);
  16. 16
  17. 17 }

StudentMapper.java

 

  1. 1 <?xml version="1.0" encoding="UTF-8" ?>
  2. 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. 3 <mapper namespace="com.xm.mapper.StudentMapper">
  4. 4
  5. 5
  6. 6
  7. 7 <!-- 根据name查询 -->
  8. 8 <select id="getByName" resultType="student">
  9. 9 select * from student where name=#{name}
  10. 10 </select>
  11. 11 </mapper>

StudentMapper.xml

 

  1. 1 package com.xm;
  2. 2
  3. 3 import org.junit.Test;
  4. 4 import org.junit.runner.RunWith;
  5. 5 import org.springframework.beans.factory.annotation.Autowired;
  6. 6 import org.springframework.boot.test.context.SpringBootTest;
  7. 7 import org.springframework.test.context.junit4.SpringRunner;
  8. 8
  9. 9 import com.xm.mapper.StudentMapper;
  10. 10 import com.xm.pojo.Student;
  11. 11
  12. 12 @RunWith(SpringRunner.class)
  13. 13 @SpringBootTest
  14. 14 public class StudentTest {
  15. 15 @Autowired
  16. 16 private StudentMapper studentMapper;
  17. 17
  18. 18 @Test
  19. 19 public void selectStudent() {
  20. 20
  21. 21 Student student = studentMapper.getByName("郭小明");
  22. 22 System.out.println(student);
  23. 23
  24. 24 }
  25. 25
  26. 26
  27. 27 }

StudentTest.java

 

 

 注意在mapper映射中,单个参数类型,也可以不写 parameterType=””,参数名可以随意写,比如#{names}

  1. 1 <?xml version="1.0" encoding="UTF-8" ?>
  2. 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. 3 <mapper namespace="com.xm.mapper.StudentMapper">
  4. 4
  5. 5
  6. 6
  7. 7 <!-- 根据name查询 -->
  8. 8 <select id="getByName" resultType="student">
  9. 9 <!-- select * from student where name=#{name} -->
  10. 10 select * from student where name=#{names}
  11. 11 </select>
  12. 12 </mapper>

StudentMapper.xml

 

 

  1. 1 package com.xm.mapper;
  2. 2
  3. 3 import java.util.List;
  4. 4
  5. 5 import com.xm.pojo.Student;
  6. 6
  7. 7 public interface StudentMapper {
  8. 8
  9. 9
  10. 10 /**
  11. 11 * 根据用户名和id同时查询
  12. 12 * @param id
  13. 13 * @param name
  14. 14 * @return
  15. 15 */
  16. 16 public Student getStudentByIdAndName(Integer id,String name);
  17. 17
  18. 18 }

StudentMapper.java

 

  1. 1 <?xml version="1.0" encoding="UTF-8" ?>
  2. 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. 3 <mapper namespace="com.xm.mapper.StudentMapper">
  4. 4
  5. 5
  6. 6 <!-- 根据用户名和id同时查询 -->
  7. 7 <select id="getStudentByIdAndName" resultType="student">
  8. 8 select * from student where name=#{name} and id=#{id}
  9. 9 </select>
  10. 10 </mapper>

StudentMapper.xml

 

  1. 1 package com.xm;
  2. 2
  3. 3 import org.junit.Test;
  4. 4 import org.junit.runner.RunWith;
  5. 5 import org.springframework.beans.factory.annotation.Autowired;
  6. 6 import org.springframework.boot.test.context.SpringBootTest;
  7. 7 import org.springframework.test.context.junit4.SpringRunner;
  8. 8
  9. 9 import com.xm.mapper.StudentMapper;
  10. 10 import com.xm.pojo.Student;
  11. 11
  12. 12 @RunWith(SpringRunner.class)
  13. 13 @SpringBootTest
  14. 14 public class StudentTest {
  15. 15 @Autowired
  16. 16 private StudentMapper studentMapper;
  17. 17
  18. 18
  19. 19 @Test
  20. 20 public void selectStudent() {
  21. 21
  22. 22 /*Student student = studentMapper.getByName("郭小明");*/
  23. 23 Student student = studentMapper.getStudentByIdAndName(1, "郭小明");
  24. 24 System.out.println(student);
  25. 25
  26. 26 }
  27. 27
  28. 28
  29. 29 }

StudentTest.java

 

说明:这种简单的根据参数名传参是失败的

  1. 1 <?xml version="1.0" encoding="UTF-8" ?>
  2. 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. 3 <mapper namespace="com.xm.mapper.StudentMapper">
  4. 4
  5. 5
  6. 6 <!-- 根据用户名和id同时查询 -->
  7. 7 <select id="getStudentByIdAndName" resultType="student">
  8. 8 <!-- select * from student where name=#{name} and id=#{id} -->
  9. 9 select * from student where name=#{param2} and id=#{param1}
  10. 10 </select>
  11. 11 </mapper>

StudentMapper.xml

 

说明:针对这种情况,如果参数较多的情况下,获取准确的参数名更好一些

  1. 1 package com.xm.mapper;
  2. 2
  3. 3 import java.util.List;
  4. 4
  5. 5 import org.apache.ibatis.annotations.Param;
  6. 6
  7. 7 import com.xm.pojo.Student;
  8. 8
  9. 9 public interface StudentMapper {
  10. 10
  11. 11
  12. 12
  13. 13 /**
  14. 14 * 根据用户名和id同时查询
  15. 15 * @param id
  16. 16 * @param name
  17. 17 * @return
  18. 18 */
  19. 19 public Student getStudentByIdAndName(@Param("id")Integer id,@Param("name")String name);
  20. 20
  21. 21 }

StudentMapper.java

  1. 1 <?xml version="1.0" encoding="UTF-8" ?>
  2. 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. 3 <mapper namespace="com.xm.mapper.StudentMapper">
  4. 4
  5. 5
  6. 6 <!-- 根据用户名和id同时查询 -->
  7. 7 <select id="getStudentByIdAndName" resultType="student">
  8. 8 select * from student where name=#{name} and id=#{id}
  9. 9 <!-- select * from student where name=#{param2} and id=#{param1} -->
  10. 10 </select>
  11. 11 </mapper>

StudentMapper.xml

 

说明:针对参数较多,且参数都属于同一个pojo类的时候,可以把参数先封装入实体,再获取

  1. 1 package com.xm.mapper;
  2. 2
  3. 3 import java.util.List;
  4. 4
  5. 5 import org.apache.ibatis.annotations.Param;
  6. 6
  7. 7 import com.xm.pojo.Student;
  8. 8
  9. 9 public interface StudentMapper {
  10. 10
  11. 11
  12. 12
  13. 13 /**
  14. 14 * 根据用户名和id同时查询
  15. 15 * @param id
  16. 16 * @param name
  17. 17 * @return
  18. 18 */
  19. 19 public Student getStudentByIdAndName(Student student);
  20. 20
  21. 21 }

StudentMapper.java

  1. 1 package com.xm;
  2. 2
  3. 3 import org.junit.Test;
  4. 4 import org.junit.runner.RunWith;
  5. 5 import org.springframework.beans.factory.annotation.Autowired;
  6. 6 import org.springframework.boot.test.context.SpringBootTest;
  7. 7 import org.springframework.test.context.junit4.SpringRunner;
  8. 8
  9. 9 import com.xm.mapper.StudentMapper;
  10. 10 import com.xm.pojo.Student;
  11. 11
  12. 12 @RunWith(SpringRunner.class)
  13. 13 @SpringBootTest
  14. 14 public class StudentTest {
  15. 15 @Autowired
  16. 16 private StudentMapper studentMapper;
  17. 17
  18. 18 @Test
  19. 19 public void selectStudent() {
  20. 20
  21. 21 /*Student student = studentMapper.getByName("郭小明");*/
  22. 22 /*Student student = studentMapper.getStudentByIdAndName(1, "郭小明");*/
  23. 23 Student student = new Student();
  24. 24 student.setName("郭小明");
  25. 25 student.setId(1);
  26. 26 Student student2 = studentMapper.getStudentByIdAndName(student);
  27. 27 System.out.println(student2);
  28. 28
  29. 29 }
  30. 30
  31. 31
  32. 32 }

StudentMapper.java

 

版权声明:本文为TimerHotel原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/TimerHotel/p/springboot_matatis_06.html