说明:
${}
拼接select SQL主体
${}
拼接update SQL主体
${}
拼接where后的语句
${}
拼接where后的语句(包括where。需注意在动态SQL中勿处于<where></where>标签内)
演示:
Mapper接口
@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
List<User> queryAll(@Param("tableName") String tableName,@Param() Wrapper wrapper);
boolean updateById(@Param("tableName") String tableName,@Param("id") int id,@Param() Wrapper wrapper);//若变量名为ew则无需注解
}
XML
<select resultType="">
select ${} from ${tableName} ${};
</select>
<update >
update ${tableName} set ${} ${};
</update>
Controller(或Test)
@Autowired
UserServiceImpl userService;
@RequestMapping("/query")
public List<User> queryAll(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
("*").eq("age","10");
return ("user",wrapper);
//return ("user", ().select("*").eq("age","10"));
}
@RequestMapping("/update")
public boolean upDateById(){
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
("name","5").eq("id","5");
return ("user",5,wrapper);
//return ("user",5,().set("name","5").eq("id",5));
}
}
注:关于wrapper的写法都是正确的,但后者需要注意使用的是静态类Wrappers
等效select SQL:select * from user where age = 10;
等效update SQL:update user set name = 5 where id = 5;