使用mybatis框架实现带条件查询-多条件(传入实体类)

时间:2023-03-10 06:26:33
使用mybatis框架实现带条件查询-多条件(传入实体类)

在实际的项目开发中,使用mybatis框架查询的时候,不可能是只有一个条件的,大部分情况下是有多个条件的,那么多个条件应该怎样传入参数;

思考:

使用mybatis框架实现带条件查询-多条件(传入实体类)

 需求:根据用户姓名(模糊查询),和用户角色对用户表进行查询

UserMapper.xml

 <!-- 按照名称查询,用户角色,查询用户列表 mybatis给最基础的数据类型都内建了别名,对大小写是不敏感的-->
<select id="getUserListByUserName2" parameterType="User" resultType="User" >
select * from smbms_user
where username like CONCAT ('%',#{userName},'%')
and userRole = #{userRole}
</select>

UserMapper.java

 public interface UserMapper {
//查询用户表中的数据
//特别要注意的一点是:Mapper接口中的方法名要和Mapper.xml文件中sql语句的id名称要相同,否则是不行滴
public List<User> getUserList(); //根据用户姓名模糊查询用户表 注意入参要和mapper.xml配置文件中入参保持一致
public List<User> getUserListByUserName(String userName); //根据用户姓名模糊查询,用户角色,查询用户表 注意:现在的入参一定是对象
public List<User> getUserListByUserName2(User user); }

User.java 实体类

 package cn.smbms.dao.pojo;

 import java.util.Date;

 public class User {
private Integer id; //id
private String userCode; //鐢ㄦ埛缂栫爜
private String userName; //鐢ㄦ埛鍚嶇О
private String userPassword; //鐢ㄦ埛瀵嗙爜
private Integer gender; //鎬у埆
private Date birthday; //鍑虹敓鏃ユ湡
private String phone; //鐢佃瘽
private String address; //鍦板潃
private Integer userRole; //鐢ㄦ埛瑙掕壊
private Integer createdBy; //鍒涘缓鑰?
private Date creationDate; //鍒涘缓鏃堕棿
private Integer modifyBy; //鏇存柊鑰?
private Date modifyDate; //鏇存柊鏃堕棿 public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}

编写测试类:

 @Test
public void test6() {
String userNameString="赵";
Integer userRole=2;
User user=new User();
user.setUserName(userNameString);
user.setUserRole(userRole);
SqlSession sqlSession = null;
java.util.List<User> userList2 = new ArrayList<User>();
try {
sqlSession = MyBatisUtil.createSqlSession();
//使用mapper映射的方式实现
//userList2 = sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserListByUserName",userNameString);
//调用mapper接口的方式实现
userList2 = sqlSession.getMapper(UserMapper.class).getUserListByUserName2(user);
int size = userList2.size();
mlogger.info("获取到的记录数是:" + size); } catch (Exception e) {
// TODO: handle exception
} finally {
// 最后一定要注意:关闭会话
MyBatisUtil.closeSqlSession(sqlSession); } for (User user2 : userList2) {
mlogger.info("用户名:" + user2.getUserName() + ",密码:" + user2.getUserPassword());
} }

最终的运行结果:

使用mybatis框架实现带条件查询-多条件(传入实体类)

这里要强调一下,以上的都是数据库字段的名称和实体类的名称是相一致的。

不一致的情况在下面会进行讨论。