|一、if
1、在UserMapper.xml中编写if条件(where:可以去掉多余的字符)
<!--查询多种条件 -->
<select id="selectAll" parameterType="map" resultType="com.createblue.bean.User">
select * from user
<where>
<if test="userName != null and userName !=''">
and user_name = #{userName}
</if>
<if test="userSex != null and userSex !=''">
and user_sex = #{userSex}
</if>
</where>
</select>
2、在UserMapper.java接口中引用方法
//多种条件查询
List<User> selectAll(Map<String, Object> map);
3、在controller调用
@RequestMapping("selectAll")
public void selectAll(@RequestParam Map<String, Object> map) {
List<User> selectAll = um.selectAll(map);
System.out.println(selectAll);
}
url地址访问:
二、foreach循环
1、在xml写del语句
<!-- 批量删除 -->
<delete id="dels" parameterType="List" >
delete from user where user_id in
<foreach collection="array" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</delete>
2、在同名的.java接口中调用del语句
//批量删除
int dels(Integer[] userId);
3、在controller层调用并执行
@RequestMapping("dels")
public void dels(Integer[] userId) {
int dels = um.dels(userId);
System.out.println(dels);
}
url访问地址:
三、自定义字段名称
1、使用resultMap(结果集映射)
<!--自定义字段关联 -->
<resultMap type="com.createblue.bean.User" id="a">
<id column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
<result column="user_sex" property="userSex"/>
<result column="user_password" property="userPwd"/>
</resultMap>
<!--查询 -->
<select id="queryAll" parameterType="map"
resultMap="a">
select * from user
</select>
版权声明:本文为ZHANGDANDAN04原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。