[学习笔记] MyBatis_07 解决MyBatis只能接受一个参数的问题

解决MyBatis只能接受一个参数的问题

以用户登录为例, 传入参数时需要传入多个参数(用户名密码), 此时只允许传入一个参数显然不满足要求

Object selectOne(String statement, Object parameterObject);

方式1: 封装POJO

把需要传递的参数封装为一个对象, 传递参数时传递该对象即可

创建用户模型对象:

public class Client {
	private Long id;
	private String username;
	private String password;
	@Override
	public String toString() {
		return "Client [username=" + username + ", password=" + password + "]";
	}
	public Client(String username, String password) {
		this.username = username;
		this.password = password;
	}
}

ClientMapper.java中定义login1方法:

public interface ClientMapper {
	//多个参数封装为POJO(JavaBean)
	Client login1(VO vo);
}

创建登录信息对象:

//封装登录信息
public class VO {
	private String username;
	private String password;
	public VO(String username, String password) {
		this.username = username;
		this.password = password;
	}
}

ClientMapper.xml代码:

<mapper namespace="com.gx.mybatis.hello.mapper.ClientMapper">
	<resultMap id="ClientType" type="Client">
		<id column="id" property="id"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
	</resultMap>
	 
	 <select id="login1" parameterType="VO" resultMap="ClientType">
	 	SELECT * FROM t_client WHERE username=#{username} AND password=#{password}
	 </select>
</mapper>

测试代码:

	@Test
	public void testLogin1(){
		
		VO vo = new VO("Fighter", "test");
		
		SqlSession session = MyBatisUtil.getSqlSession();
		ClientMapper mapper = session.getMapper(ClientMapper.class);
		
		Client c = mapper.login1(vo);
		
		System.out.println(c);
		session.close();
	}

缺点: 需要定义很多POJO(java)对象


方式2: 封装Map对象

因为JavaBeanMap非常像, 所以可以直接封装为map对象

ClientMapper.java中定义login2方法:

public interface ClientMapper {
	//多个参数封装为POJO(JavaBean)
	Client login1(VO vo);
    
    //**************************
    //使用Map来封装对象
    Client login2(Map<String, Object> paramMap);
    //**************************
}

ClientMapper.xml代码:

<mapper namespace="com.gx.mybatis.hello.mapper.ClientMapper">
	<resultMap id="ClientType" type="Client">
		<id column="id" property="id"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
	</resultMap>
	 
	 <select id="login1" parameterType="VO" resultMap="ClientType">
	 	SELECT * FROM t_client WHERE username=#{username} AND password=#{password}
	 </select>
     
	 <!-- ************************************ -->
	 <select id="login2" parameterType="map" resultMap="ClientType">
	 	SELECT * FROM t_client WHERE username=#{username} AND password=#{password}
	 </select>
	 <!-- ************************************ -->
</mapper>

此时查询条件参数#{}中应该为map中的key

测试代码:

	@Test
	public void testLogin2(){
		
		Map<String, Object> paramMap = new HashMap<String, Object>(){
			{
				this.put("username", "Fighter");
				this.put("password", "test");
			}
		};
		
		SqlSession session = MyBatisUtil.getSqlSession();
		ClientMapper mapper = session.getMapper(ClientMapper.class);
		
		Client c = mapper.login2(paramMap);
		
		System.out.println(c);
		session.close();
	}

方式1中的问题依然没得到解决


方式3: 使用Param注解

底层原理是方式2, MyBatis帮我们使用Map封装

ClientMapper.java中定义login3方法:

public interface ClientMapper {
	//多个参数封装为POJO(JavaBean)
	Client login1(VO vo);
    
    //使用Map来封装对象
    Client login2(Map<String, Object> paramMap);
    
    //**************************
	Client login3(@Param("u")String username, @Param("p")String password);
    //**************************
}

使用Param注解, 原理是方式2(使用Map封装), Param注解中的字符串表示Map中的key

ClientMapper.xml代码:

<mapper namespace="com.gx.mybatis.hello.mapper.ClientMapper">
	<resultMap id="ClientType" type="Client">
		<id column="id" property="id"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
	</resultMap>
	 
	 <select id="login1" parameterType="VO" resultMap="ClientType">
	 	SELECT * FROM t_client WHERE username=#{username} AND password=#{password}
	 </select>
     
	 <select id="login2" parameterType="map" resultMap="ClientType">
	 	SELECT * FROM t_client WHERE username=#{username} AND password=#{password}
	 </select>
	 
	 <!-- ************************************ -->
	 <select id="login3" resultMap="ClientType">
	 	SELECT * FROM t_client WHERE username=#{u} AND password=#{p}
	 </select>
	 <!-- ************************************ -->
</mapper>

测试代码:

	@Test
	public void testLogin3(){
		
		SqlSession session = MyBatisUtil.getSqlSession();
		ClientMapper mapper = session.getMapper(ClientMapper.class);
		
		Client c = mapper.login3("Fighter", "test");
		
		System.out.println(c);
		session.close();
	}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值