已转移
动态sql元素解析
<where >
<!--where标签相当于<trim prefix="WHERE" prefixOverrides="AND |OR ">-->
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<!--如果没有传入这个参数则不执行 criteria.valid!=null -->
<trim prefix="(" suffix=")" prefixOverrides="and" >
<!--这种操作的好处是 在只满足第二个when而变成and ...什么什么的时候能够自动识别-->
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
script可以像xml那样写
@Select("<script> select type,status,count(1) as count from acl_user where create_time between #{startTime} and #{endTime}" +
"and status in <foreach collection='ids' item='item' open='(' separator=',' close=')'>" +
"#{item}" +
"</foreach>"+
"group by type,status </script>")
List<AclUserData> selectAclUserIncrData(@Param("ids")List<Integer> statusIds, @Param("startTime")Date startTime, @Param("endTime")Date endTime);
@Insert 注解
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int saveOrUpdateAnswer(AnswerDO answerDO);
插入的时候返回自增主键
@Insert("insert into table3 (id, name) values(#{nameId}, #{name})")
@SelectKey(statement="call next value for TestSequence", keyProperty="nameId", before=true, resultType=int.class)
int insertTable3(Name name);
读取插入前的
通过指定@Result的id属性来命名结果集
@Results(id = "userResult", value = {
@Result(property = "id", column = "uid", id = true),
@Result(property = "firstName", column = "first_name"),
@Result(property = "lastName", column = "last_name")
})
@Select("select * from users where id = #{id}")
User getUserById(Integer id);
@Results(id = "companyResults")
@ConstructorArgs({
@Arg(property = "id", column = "cid", id = true),
@Arg(property = "name", column = "name")
})
@Select("select * from company where id = #{id}")
Company getCompanyById(Integer id);
代码更清晰
@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
List<User> getUsersByName(String name);
class UserSqlBuilder {
public String buildGetUsersByName(final String name) {
return new SQL(){{
SELECT("*");
FROM("users");
if (name != null) {
WHERE("name like #{value} || '%'");
}
ORDER_BY("id");
}}.toString();
}
}