8 多个参数的传递
张创琦 2022.03.13
1 TeamMapper.java 配置
package com.kkb.mapper;
import com.kkb.pojo.QueryVO;
import com.kkb.pojo.Team;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* ClassName: TeamMapper
* Mapper接口
* @author wanglina
* @version 1.0
*/
public interface TeamMapper {
List<Team> queryByRange1(Integer min, Integer max);
List<Team> queryByRange2(@Param("min") Integer min, @Param("max") Integer max);
List<Team> queryByRange3(Map<String,Object> map);
List<Team> queryByCondition(QueryVO vo);
List<Team> queryByName(String teamName);
List<Team> queryByLocation(String location);
List<Team> queryByFiled(@Param("column") String column,@Param("columnValue") String columnValue);
}
- TeamMapper.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kkb.mapper.TeamMapper">
<!-- 在这里省略之前输入的代码 -->
<!--多个参数:标签中不需要parameterType属性
方式1:通过下标索引的方式,还有两种方式(太难记了,可以忽略,看看演示就好啦)
select * from team where teamId >=#{arg0} and teamId <=#{arg1}; 也可以,注意下表索引细节1:
mybatis3.3版本之前:可以直接写#{0} #{1}
从mybatis3.4开始:#{arg0} #{arg1}... 或者是 #{param1} #{param2}...
细节2:
sql语句中不能使用小于号,使用转移符号替换;大于号没有限制,也可以使用转义符号替换>
-->
<select id="queryByRange1" resultType="com.kkb.pojo.Team">
select * from team where teamId >=#{param1} and teamId <=#{param2};
</select>
<!--方式2:通过注解的方式:
#{}中的名称必须与接口的方法中的参数注解@Param()保持一致
select * from team where teamId >=#{param1} and teamId <= #{param2}; 不推荐,但是语法也是正确的,但是不能使用arg0,arg1......
-->
<select id="queryByRange2" resultType="com.kkb.pojo.Team">
select * from team where teamId >=#{min} and teamId <= #{max};
</select>
<!--方式3:通过map来传递多个参数:映射文件中的参数占位符必须和map中的String类型的字段名称一样-->
<select id="queryByRange3" resultType="com.kkb.pojo.Team">
select * from team where teamId >=#{min} and teamId <= #{max};
</select>
<!--方式4:通过pojo类传递多个参数:映射文件中的参数占位符必须和pojo类中的字段完全一致-->
<select id="queryByCondition" resultType="com.kkb.pojo.Team">
select * from team
where teamId>=#{min} and teamId<=#{max}
and teamName like #{name} and location=#{location}
</select>
<!-- ${}的测试案例 -->
<select id="queryByName" resultType="com.kkb.pojo.Team">
select * from team where teamName=#{teamName}
</select>
<select id="queryByLocation" resultType="com.kkb.pojo.Team">
select * from team where location=#{location}
</select>
<select id="queryByFiled" resultType="com.kkb.pojo.Team">
select * from team where ${column}=#{columnValue}
</select>
</mapper>
- TeamMapperArgs.java
package com.kkb.test;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.QueryVO;
import com.kkb.pojo.Team;
import com.kkb.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* ClassName: TestTeamMapperArg
* 测试输入映射:多个参数
* @author wanglina
* @version 1.0
*/
public class TestTeamMapperArg {
private TeamMapper teamMapper = MybatisUtil.getSqlSession().getMapper(TeamMapper.class);
private SqlSession sqlSession = MybatisUtil.getSqlSession();
// 测试成功
@Test
public void test01(){
List<Team> teams = teamMapper.queryByRange1(1004, 1010);
teams.forEach(team -> System.out.println(team));
}
// 测试成功
@Test
public void test02(){
List<Team> teams = teamMapper.queryByRange2(1005, 1011);
teams.forEach(team -> System.out.println(team));
}
// 测试成功
@Test
public void test03(){
TeamMapper teamDao= sqlSession.getMapper(TeamMapper.class);
Map<String, Object> map=new HashMap<>();
map.put("min",1001);
map.put("max",2000);
List<Team> teamList3 = teamDao.queryByRange3(map);
for (Team team : teamList3) {
System.out.println(team);
}
}
// 乱码问题,测试失败
@Test
public void test04(){
QueryVO vo=new QueryVO();
vo.setLocation("洛杉矶");
vo.setName("%球队%");
vo.setMin(1001);
vo.setMax(1111);
List<Team> teams = teamMapper.queryByCondition(vo);
teams.forEach(team -> System.out.println(team));
}
// 乱码问题,测试失败
@Test
public void test05(){
System.out.println("根据球队名称查询:");
List<Team> teams = teamMapper.queryByName("lina的球队");
teams.forEach(team -> System.out.println(team));
System.out.println("根据球队位置查询:");
List<Team> teams2 = teamMapper.queryByLocation("洛杉矶");
teams2.forEach(team -> System.out.println(team));
}
// 乱码问题,测试失败
@Test
public void test06() {
System.out.println("根据球队名称查询:");
List<Team> teams = teamMapper.queryByFiled("teamName", "lina的球队");
teams.forEach(team -> System.out.println(team));
System.out.println("根据球队位置查询:");
List<Team> teams2 = teamMapper.queryByFiled("location", "洛杉矶");
teams2.forEach(team -> System.out.println(team));
}
}