8 多个参数的传递

张创琦 2022.03.13

1 TeamMapper.java 配置

  1. package com.kkb.mapper;
  2. import com.kkb.pojo.QueryVO;
  3. import com.kkb.pojo.Team;
  4. import org.apache.ibatis.annotations.Param;
  5. import java.util.List;
  6. import java.util.Map;
  7. /**
  8. * ClassName: TeamMapper
  9. * Mapper接口
  10. * @author wanglina
  11. * @version 1.0
  12. */
  13. public interface TeamMapper {
  14. List<Team> queryByRange1(Integer min, Integer max);
  15. List<Team> queryByRange2(@Param("min") Integer min, @Param("max") Integer max);
  16. List<Team> queryByRange3(Map<String,Object> map);
  17. List<Team> queryByCondition(QueryVO vo);
  18. List<Team> queryByName(String teamName);
  19. List<Team> queryByLocation(String location);
  20. List<Team> queryByFiled(@Param("column") String column,@Param("columnValue") String columnValue);
  21. }
  1. TeamMapper.xml 配置
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.kkb.mapper.TeamMapper">
  6. <!-- 在这里省略之前输入的代码 -->
  7. <!--多个参数:标签中不需要parameterType属性
  8. 方式1:通过下标索引的方式,还有两种方式(太难记了,可以忽略,看看演示就好啦)
  9. select * from team where teamId &gt;=#{arg0} and teamId &lt;=#{arg1}; 也可以,注意下表索引细节1
  10. mybatis3.3版本之前:可以直接写#{0} #{1}
  11. mybatis3.4开始:#{arg0} #{arg1}... 或者是 #{param1} #{param2}...
  12. 细节2
  13. sql语句中不能使用小于号,使用转移符号替换;大于号没有限制,也可以使用转义符号替换&gt;
  14. -->
  15. <select id="queryByRange1" resultType="com.kkb.pojo.Team">
  16. select * from team where teamId >=#{param1} and teamId &lt;=#{param2};
  17. </select>
  18. <!--方式2:通过注解的方式:
  19. #{}中的名称必须与接口的方法中的参数注解@Param()保持一致
  20. select * from team where teamId >=#{param1} and teamId &lt;= #{param2}; 不推荐,但是语法也是正确的,但是不能使用arg0arg1......
  21. -->
  22. <select id="queryByRange2" resultType="com.kkb.pojo.Team">
  23. select * from team where teamId >=#{min} and teamId &lt;= #{max};
  24. </select>
  25. <!--方式3:通过map来传递多个参数:映射文件中的参数占位符必须和map中的String类型的字段名称一样-->
  26. <select id="queryByRange3" resultType="com.kkb.pojo.Team">
  27. select * from team where teamId >=#{min} and teamId &lt;= #{max};
  28. </select>
  29. <!--方式4:通过pojo类传递多个参数:映射文件中的参数占位符必须和pojo类中的字段完全一致-->
  30. <select id="queryByCondition" resultType="com.kkb.pojo.Team">
  31. select * from team
  32. where teamId>=#{min} and teamId&lt;=#{max}
  33. and teamName like #{name} and location=#{location}
  34. </select>
  35. <!-- ${}的测试案例 -->
  36. <select id="queryByName" resultType="com.kkb.pojo.Team">
  37. select * from team where teamName=#{teamName}
  38. </select>
  39. <select id="queryByLocation" resultType="com.kkb.pojo.Team">
  40. select * from team where location=#{location}
  41. </select>
  42. <select id="queryByFiled" resultType="com.kkb.pojo.Team">
  43. select * from team where ${column}=#{columnValue}
  44. </select>
  45. </mapper>
  1. TeamMapperArgs.java
  1. package com.kkb.test;
  2. import com.kkb.mapper.TeamMapper;
  3. import com.kkb.pojo.QueryVO;
  4. import com.kkb.pojo.Team;
  5. import com.kkb.utils.MybatisUtil;
  6. import org.apache.ibatis.session.SqlSession;
  7. import org.junit.Test;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. /**
  12. * ClassName: TestTeamMapperArg
  13. * 测试输入映射:多个参数
  14. * @author wanglina
  15. * @version 1.0
  16. */
  17. public class TestTeamMapperArg {
  18. private TeamMapper teamMapper = MybatisUtil.getSqlSession().getMapper(TeamMapper.class);
  19. private SqlSession sqlSession = MybatisUtil.getSqlSession();
  20. // 测试成功
  21. @Test
  22. public void test01(){
  23. List<Team> teams = teamMapper.queryByRange1(1004, 1010);
  24. teams.forEach(team -> System.out.println(team));
  25. }
  26. // 测试成功
  27. @Test
  28. public void test02(){
  29. List<Team> teams = teamMapper.queryByRange2(1005, 1011);
  30. teams.forEach(team -> System.out.println(team));
  31. }
  32. // 测试成功
  33. @Test
  34. public void test03(){
  35. TeamMapper teamDao= sqlSession.getMapper(TeamMapper.class);
  36. Map<String, Object> map=new HashMap<>();
  37. map.put("min",1001);
  38. map.put("max",2000);
  39. List<Team> teamList3 = teamDao.queryByRange3(map);
  40. for (Team team : teamList3) {
  41. System.out.println(team);
  42. }
  43. }
  44. // 乱码问题,测试失败
  45. @Test
  46. public void test04(){
  47. QueryVO vo=new QueryVO();
  48. vo.setLocation("洛杉矶");
  49. vo.setName("%球队%");
  50. vo.setMin(1001);
  51. vo.setMax(1111);
  52. List<Team> teams = teamMapper.queryByCondition(vo);
  53. teams.forEach(team -> System.out.println(team));
  54. }
  55. // 乱码问题,测试失败
  56. @Test
  57. public void test05(){
  58. System.out.println("根据球队名称查询:");
  59. List<Team> teams = teamMapper.queryByName("lina的球队");
  60. teams.forEach(team -> System.out.println(team));
  61. System.out.println("根据球队位置查询:");
  62. List<Team> teams2 = teamMapper.queryByLocation("洛杉矶");
  63. teams2.forEach(team -> System.out.println(team));
  64. }
  65. // 乱码问题,测试失败
  66. @Test
  67. public void test06() {
  68. System.out.println("根据球队名称查询:");
  69. List<Team> teams = teamMapper.queryByFiled("teamName", "lina的球队");
  70. teams.forEach(team -> System.out.println(team));
  71. System.out.println("根据球队位置查询:");
  72. List<Team> teams2 = teamMapper.queryByFiled("location", "洛杉矶");
  73. teams2.forEach(team -> System.out.println(team));
  74. }
  75. }