@Mapper
interface UserMapper {
@Results(id = "userResult", value = [
@Result(property = "id", column = "id", id = true),
@Result(property = "enterprisePosition", column = "enterprise_position")
])
@Select("select * from user where id = #{id}")
User getUserById(Integer id);
@ResultMap("userResult")
@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
List<User> getUsersByName(
@Param("name") String name,
@Param("phone") String phone);
@UpdateProvider(type = UserSqlBuilder.class, method = "buildUpdate")
int update(User user);
class UserSqlBuilder {
String buildGetUsersByName(Map<String, Object> map) {
String sql = new SQL() {
{
SELECT("*")
FROM("user")
if (map.get("name") != null) {
WHERE("name=#{name}")
}
if (map.get("phone") != null) {
WHERE("phone=#{phone}")
}
}
}
println(sql)
return sql
}
String buildUpdate(User user) {
String sql = new SQL() {
{
//构造块
UPDATE("user")
if (user.name != null)
SET("name=#{name}")
if (user.phone != null)
SET("phone=#{phone}")
WHERE("id=#{id}")
}
}
println(sql)
return sql
}
}
}
是不是可以不用写xml了?以前新建个表要花4步,第一步新建java model,第二步新建接口,第三步新建xml,第四步起别名。。。现在只需要前两步就完事了,多爽!