在实际的项目开发中,使用mybatis框架查询的时候,不可能是只有一个条件的,大部分情况下是有多个条件的,那么多个条件应该怎样传入参数;
思考:
需求:根据用户姓名(模糊查询),和用户角色对用户表进行查询
UserMapper.xml
1 <!-- 按照名称查询,用户角色,查询用户列表 mybatis给最基础的数据类型都内建了别名,对大小写是不敏感的--> 2 <select id="getUserListByUserName2" parameterType="User" resultType="User" > 3 select * from smbms_user 4 where username like CONCAT ('%',#{userName},'%') 5 and userRole = #{userRole} 6 </select>
UserMapper.java
1 public interface UserMapper { 2 //查询用户表中的数据 3 //特别要注意的一点是:Mapper接口中的方法名要和Mapper.xml文件中sql语句的id名称要相同,否则是不行滴 4 public List<User> getUserList(); 5 6 //根据用户姓名模糊查询用户表 注意入参要和mapper.xml配置文件中入参保持一致 7 public List<User> getUserListByUserName(String userName); 8 9 //根据用户姓名模糊查询,用户角色,查询用户表 注意:现在的入参一定是对象 10 public List<User> getUserListByUserName2(User user); 11 12 13 }
User.java 实体类
1 package cn.smbms.dao.pojo; 2 3 import java.util.Date; 4 5 public class User { 6 private Integer id; //id 7 private String userCode; //鐢ㄦ埛缂栫爜 8 private String userName; //鐢ㄦ埛鍚嶇О 9 private String userPassword; //鐢ㄦ埛瀵嗙爜 10 private Integer gender; //鎬у埆 11 private Date birthday; //鍑虹敓鏃ユ湡 12 private String phone; //鐢佃瘽 13 private String address; //鍦板潃 14 private Integer userRole; //鐢ㄦ埛瑙掕壊 15 private Integer createdBy; //鍒涘缓鑰? 16 private Date creationDate; //鍒涘缓鏃堕棿 17 private Integer modifyBy; //鏇存柊鑰? 18 private Date modifyDate; //鏇存柊鏃堕棿 19 20 public Integer getId() { 21 return id; 22 } 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 public String getUserCode() { 27 return userCode; 28 } 29 public void setUserCode(String userCode) { 30 this.userCode = userCode; 31 } 32 public String getUserName() { 33 return userName; 34 } 35 public void setUserName(String userName) { 36 this.userName = userName; 37 } 38 public String getUserPassword() { 39 return userPassword; 40 } 41 public void setUserPassword(String userPassword) { 42 this.userPassword = userPassword; 43 } 44 public Integer getGender() { 45 return gender; 46 } 47 public void setGender(Integer gender) { 48 this.gender = gender; 49 } 50 public Date getBirthday() { 51 return birthday; 52 } 53 public void setBirthday(Date birthday) { 54 this.birthday = birthday; 55 } 56 public String getPhone() { 57 return phone; 58 } 59 public void setPhone(String phone) { 60 this.phone = phone; 61 } 62 public String getAddress() { 63 return address; 64 } 65 public void setAddress(String address) { 66 this.address = address; 67 } 68 public Integer getUserRole() { 69 return userRole; 70 } 71 public void setUserRole(Integer userRole) { 72 this.userRole = userRole; 73 } 74 public Integer getCreatedBy() { 75 return createdBy; 76 } 77 public void setCreatedBy(Integer createdBy) { 78 this.createdBy = createdBy; 79 } 80 public Date getCreationDate() { 81 return creationDate; 82 } 83 public void setCreationDate(Date creationDate) { 84 this.creationDate = creationDate; 85 } 86 public Integer getModifyBy() { 87 return modifyBy; 88 } 89 public void setModifyBy(Integer modifyBy) { 90 this.modifyBy = modifyBy; 91 } 92 public Date getModifyDate() { 93 return modifyDate; 94 } 95 public void setModifyDate(Date modifyDate) { 96 this.modifyDate = modifyDate; 97 } 98 }
编写测试类:
1 @Test 2 public void test6() { 3 String userNameString="赵"; 4 Integer userRole=2; 5 User user=new User(); 6 user.setUserName(userNameString); 7 user.setUserRole(userRole); 8 SqlSession sqlSession = null; 9 java.util.List<User> userList2 = new ArrayList<User>(); 10 try { 11 sqlSession = MyBatisUtil.createSqlSession(); 12 //使用mapper映射的方式实现 13 //userList2 = sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserListByUserName",userNameString); 14 //调用mapper接口的方式实现 15 userList2 = sqlSession.getMapper(UserMapper.class).getUserListByUserName2(user); 16 int size = userList2.size(); 17 mlogger.info("获取到的记录数是:" + size); 18 19 } catch (Exception e) { 20 // TODO: handle exception 21 } finally { 22 // 最后一定要注意:关闭会话 23 MyBatisUtil.closeSqlSession(sqlSession); 24 25 } 26 27 for (User user2 : userList2) { 28 mlogger.info("用户名:" + user2.getUserName() + ",密码:" + user2.getUserPassword()); 29 } 30 31 }
最终的运行结果:
这里要强调一下,以上的都是数据库字段的名称和实体类的名称是相一致的。
不一致的情况在下面会进行讨论。