publicinterfaceUserMapper { @Select("SELECT * FROM t_users WHERE id = #{id}") public User selectUserById(Integer id);
@Select("SELECT * FROM t_users WHERE id = #{id} AND password = #{pwd}") public User selectUserByIdAndPwd_annotation(@Param("id") Integer id, @Param("pwd") String password); }
1.2 删除
1 2
@Delete(value = "DELETE FROM t_users WHERE id = #{id}") publicintdeleteUser(Integer id);
1.3 修改
1 2
@Update("UPDATE t_users SET name = #{name} , password = #{password} , salary = #{salary} , birthday = #{birthday} WHERE id = #{id}") publicintupdateUser(User user);
1.4 插入
1 2 3 4 5 6
@Insert("INSERT INTO t_users VALUES(#{id},#{name},#{password},#{salary},#{birthday},null)") publicintinsertUser(User user);
public List<User> selectAllUsers1(User user); // ${name} ${id} 可获取user中的属性值 public List<User> selectAllUsers2(@Param("rule") String rule); //必须使用@Param否则会作为属性解析
1 2 3 4 5 6 7 8
<selectid="selectAllUsers1"resultType="user"> SELECT * FROM t_users WHERE name = '${name}' or id = ${id} <!-- 拼接name和id,如果是字符类型需要用单引号:'${name}' --> </select> <selectid="selectAllUsers2"resultType="user"> SELECT * FROM t_users ORDER BY id ${rule} <!-- 拼接 asc | desc --> </select>
<selectid="selectUsersByKeyword"resultType="user"> SELECT * FROM t_user WHERE name = '${name}' <!-- 会存在注入攻击 比如传入参数是 【String name = "tom' or '1'='1";】--> </select>