<select id="findByCondition" parameterType="User" resultType="User">
select * from t_user where nick_name like #{nickName} and balance>#{balance}
</select>
<select id="findByusernamepassword" resultType="User" parameterType="map">
select * from t_user where username=#{username} and password=#{password}
</select>
在xml类型文件中传入参数必须是一个parameterType的类型必须是一个阐述那么我们就要通过一些方式,实现可以通过两种方式实现多阐述执行Mysql语句
1.根据所要传的参数封装成一个对象,
package Jopo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long userId;
private String username; //成员变量
private String password;
private String nickName;
private Integer isAdmin;
private String phone;
private Integer gender;
private Date birth;
private Integer userStatus;
private Date userCreateTime;
private Date userUpdateTime;
private Integer isDelete;
private Integer isMember;
private BigDecimal balance;
public User( String nickName,BigDecimal balance){
this.nickName=nickName;
this.balance=balance;
}
}
2.第二种方法是将要传来的参数封装成一个Map,Map的key是表示对应列名,value对应的值
public User findByusernamepassword(Map<String,Object> map);
@Test
public void findByusernamebiance(){
try{
User user=mapper.findByusernamebiance("bb",new BigDecimal(1111));
System.out.println(user);
MyBatisUtil.loadcommint();
}catch (Exception e){
e.printStackTrace();
MyBatisUtil.Rrollback();
}finally {
MyBatisUtil.close();
}
}
也可以使用注解来实现
public User findByusernamebiance(@Param("username")String username, @Param("balance")BigDecimal balance);
实际上底层也是用Map封装的,但是用注解的话也比较方便