使用mybatis框架實現帶條件查詢-多條件(傳入實體類)


在實際的項目開發中,使用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     }

最終的運行結果:

 

這里要強調一下,以上的都是數據庫字段的名稱和實體類的名稱是相一致的。

不一致的情況在下面會進行討論。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM