A, where and if the label
1, the new method in the interface IUserDao
2, in the preparation of the corresponding content in IUserDao.xml
Here's if
the inside test
for the conditional sentence, which the parameter names correspond java documents in the field instead of the database
if
content tag for splicing sql statement
in which where
the label is actually where 1 = 1 is rewritten as select * from user
Note that when a plurality of particular if
time, since the label splice sql statement content, and so do not forget the following connection as
username like the userName} {# and
Sex = ...
<select id="findUserByCondition" resultMap="userMap" parameterType="user">
select * from user
<where>
<if test="userName != null and userName.length() > 0">
username like #{userName}
</if>
<if test="userSex != null and userSex.length() > 0">
and sex = #{userSex}
</if>
</where>
</select>
ResultMap or mention above is:
<resultMap id="userMap" type="user">
<id property="userId" column="id"></id>
<result property="userName" column="username"></result>
<result property="userBirthday" column="birthday"></result>
<result property="userSex" column="sex"></result>
<result property="userAddress" column="address"></result>
</resultMap>
3. Writing Test Methods
public void testFindUserByCondition(){
User user = new User();
user.setUserName("%老新%");
user.setUserSex("女");
List<User> users = userDao.findUserByCondition(user);
for (User u : users){
System.out.println(u);
}
}
Two, foreach and sql tag
1, write methods IUserDao interface
Returns a List, transmission parameters for packaging entity QueryVo
2, adding QueryVo entity class field and get & set
Create a List, which are all id (s) you want to find
private List<Integer> ids = new ArrayList<Integer>();
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
3, write test classes
Create a List called IDS , this name is very important, it will be mentioned below
the rest see the comment on the line
public void testFindUserByIds(){
List<Integer> ids = new ArrayList<Integer>();
//创建一个vo
QueryVo vo = new QueryVo();
//往ids列表中添加要查找的id们
ids.add(67);
ids.add(73);
ids.add(74);
//将ids传给vo实体类
vo.setIds(ids);
//产生结果List
List<User> users = userDao.findUserByIds(vo);
for (User user: users) {
System.out.println(user);
}
}
4, added in xml
Here the argument passed to a queryvo objects
if
in the test
still corresponds to the search criteria
Key :
<foreach>
Tag inside of parameters:
Collection: object name need to pass a set of
open: conditions, attention followed by a left parenthesis (open parenthesis, so as Open)
Item: and the following contents #{id}
in the id corresponding to
such item="id"
correspondence #{id}
, item="uid"
correspondence #{uid}
, as passed in alias parameters,
Close: close bracket, in conjunction with the open
separator: delimiter between a plurality of parameters sql statement, where a comma
<select id="findUserByIds" resultMap="userMap" parameterType="queryvo">
select * from user
<where>
<if test="ids != null and ids.size() >0 ">
<foreach collection="ids" open="and id in (" item="id" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select>