<select id="selectById" resultType="com.macro.mall.tiny.model.UmsAdmin"> select username, password, icon, email, nick_name as nickName, note, create_time as createTime, login_time as loginTime, status from ums_admin where id = #{id} </select>
/** * 自定义UmsAdmin表查询 * Created by macro on 2022/10/20. */ @Repository public interface UmsAdminDao { /** * 根据ID修改用户信息 */ int updateById(UmsAdmin entity); }
/** * 自定义UmsAdmin表查询 * Created by macro on 2022/10/20. */ @Repository public interface UmsAdminDao { /** * 根据用户名和Email模糊查询用户 * 不输入查询所有 */ List<UmsAdmin> selectByUsernameAndEmailLike(@Param("username") String username, @Param("email") String email); }
xml中添加对应的SQL实现如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<select id="selectByUsernameAndEmailLike" resultType="com.macro.mall.tiny.model.UmsAdmin"> select username, password, icon, email, nick_name as nickName, note, create_time as createTime, login_time as loginTime, status from ums_admin where 1=1 <if test="username!=null and username!=''"> and username like concat('%',#{username},'%') </if> <if test="email!=null and email!=''"> and email like concat('%',#{email},'%') </if> </select>
<select id="selectByUsernameAndEmailLike2" resultType="com.macro.mall.tiny.model.UmsAdmin"> select username, password, icon, email, nick_name as nickName, note, create_time as createTime, login_time as loginTime, status from ums_admin where 1=1 <choose> <when test="username!=null and username!=''"> and username like concat('%',#{username},'%') </when> <when test="email!=null and email!=''"> and email like concat('%',#{email},'%') </when> <otherwise> and 1=2 </otherwise> </choose> </select>
<select id="selectByUsernameAndEmailLike3" resultType="com.macro.mall.tiny.model.UmsAdmin"> select username, password, icon, email, nick_name as nickName, note, create_time as createTime, login_time as loginTime, status from ums_admin <where> <if test="username!=null and username!=''"> and username like concat('%',#{username},'%') </if> <if test="email!=null and email!=''"> and email like concat('%',#{email},'%') </if> </where> </select>
/** * 自定义UmsAdmin表查询 * Created by macro on 2022/10/20. */ @Repository public interface UmsAdminDao { /** * 根据ID选择性修改用户信息 */ int updateByIdSelective(UmsAdmin entity); }
/** * 自定义UmsAdmin表查询 * Created by macro on 2022/10/20. */ @Repository public interface UmsAdminDao { /** * 批量插入用户 */ int insertBatch(@Param("entityList") List<UmsAdmin> adminList); }
/** * 自定义UmsAdmin表查询 * Created by macro on 2022/10/20. */ @Repository public interface UmsAdminDao { /** * 根据用户ID批量查询 */ List<UmsAdmin> selectByIds(@Param("ids") List<Long> ids); }
在xml中的对应SQL实现如下,我们可以使用open、close属性指定拼接语句的前后缀。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<select id="selectByIds" resultType="com.macro.mall.tiny.model.UmsAdmin"> select username, password, icon, email, nick_name as nickName, note, create_time as createTime, login_time as loginTime, status from ums_admin where id in <foreach collection="ids" item="item" open="(" close=")" separator=","> #{item} </foreach> </select>
<select id="selectResourceWithCategory" resultType="com.macro.mall.tiny.domain.UmsResourceExt"> select ur.id, ur.create_time as createTime, ur.name, ur.url, ur.description, ur.category_id as categoryId, urc.id as "category.id", urc.name as "category.name", urc.sort as "category.sort", urc.create_time as "category.createTime" from ums_resource ur left join ums_resource_category urc on ur.category_id = urc.id where ur.id = #{id} </select>
<select id="selectResourceWithCategory2" resultMap="ResourceWithCategoryMap"> select ur.id, ur.create_time, ur.name, ur.url, ur.description, ur.category_id, urc.id as category_id, urc.name as category_name, urc.sort as category_sort, urc.create_time as category_create_time from ums_resource ur left join ums_resource_category urc on ur.category_id = urc.id where ur.id = #{id} </select>