Skip to content

TkMapper kotlin

basicfu edited this page Feb 11, 2019 · 3 revisions

TkMapper kotlin

Example

针对kotlin语言封装的example对象,简化代码写法

val example = example<Resume> {
    distinct()
    //乐观锁,可传入true或false方法内不跟参数默认为true
    forUpdate(false)
    select("name", "mobile")
    notSelect("id")
    from("resume")
    //可以不在where里写直接使用,等价where{}内的代码
    //所有该层级的and*和or*开头的方法条件等价于一个andWhere{}
    //当不需要使用高级查询中不带()时请直接使用这种方式
    andEqualTo(Resume::address, "b")
    andEqualTo {
        name = "xiaoming"
        mobile = "1234"
    }
    //查询条件在一个()内
    //where、andWhere、orWhere在第一个sql where后不会区分and还是or
    where {
        andIsNull(Resume::email, Resume::language)
        andIsNotNull(Resume::email, Resume::language)
        //支持两种写法
        //1.java8 lambda,当只需要一个查询条件时建议使用这种方式
        andEqualTo(Resume::address, "b")
        //2.对象名直接赋值,会反射Resume对象中所有不为null的值做为查询条件,当查询条件超过2个以上时使用这种方式
        andEqualTo {
            mobile = "12312344"
        }
        andNotEqualTo(Resume::useUserid, "123")
        andNotEqualTo {
            mobile = "1234"
        }
        andGreaterThan(Resume::email, "12345")
        andGreaterThan {
            mobile = "1234"
        }
        andGreaterThanOrEqualTo(Resume::email, "12345")
        andGreaterThanOrEqualTo {
            mobile = "1234"
        }
        andLessThan(Resume::email, "12345")
        andLessThan {
            mobile = "1234"
        }
        andLessThanOrEqualTo(Resume::email, "12345")
        andLessThanOrEqualTo {
            mobile = "1234"
        }
        andIn(Resume::email, arrayListOf("123"))
        andNotIn(Resume::email, arrayListOf("123"))
        //between注意事项
        //1.支持其中两个参数任意一个参数为null,当第一个参数为null时自动使用andLessThanOrEqualTo(k,v2)小于或等于v2,当第二个参数为null时自动使用andGreaterThanOrEqualTo(k,v1)大于或等于v2
        andBetween(Resume::email, "1234", null)
        andNotBetween(Resume::email, "1234", "1234")
        //like注意事项
        //1.自动在要模糊的值左右两边追加%,例:值abc则查询结果为like %abc%
        //2.如果值中包含%或_这两个特殊符号会在前自动加转移/且最后跟 escape '/'例:值xi_ao则查询结果为like %xi/_ao% escape '/'
        andLike(Resume::email, "xi_ao")
        andNotLike(Resume::email, "xiao")
  //or
        orIsNull(Resume::email, Resume::language)
        orIsNotNull(Resume::email, Resume::language)
        orEqualTo(Resume::address, "b12")
        orEqualTo {
            name = "xiaoming"
            mobile = "1234"
        }
        orNotEqualTo(Resume::useUserid, "123")
        orNotEqualTo {
            mobile = "1234"
        }
        orGreaterThan(Resume::email, "12345")
        orGreaterThan {
            mobile = "1234"
        }
        orGreaterThanOrEqualTo(Resume::email, "12345")
        orGreaterThanOrEqualTo {
            mobile = "1234"
        }
        orLessThan(Resume::email, "12345")
        orLessThan {
            mobile = "1234"
        }
        orLessThanOrEqualTo(Resume::email, "12345")
        orLessThanOrEqualTo {
            mobile = "1234"
        }
        orIn(Resume::email, arrayListOf("123"))
        orNotIn(Resume::email, arrayListOf("123"))
        orBetween(Resume::email, "1234", null)
        orNotBetween(Resume::email, "1234", "1234")
        orLike(Resume::email, "xiao")
        orNotLike(Resume::email, "xiao")
    }
    //等价于where{}都属于and()
    andWhere {
        andIsNull(Resume::email, Resume::language)
        orIsNull(Resume::email, Resume::language)
    }
    //()前的条件为or,例:where (a=1) or (b=2)
    orWhere {
        andIsNull(Resume::email, Resume::language)
        orIsNull(Resume::email, Resume::language)
    }
    orderByAsc("name")
    orderByDesc("name", "mobile")
}
mapper.selectByExample(example)

Demo

查询
val example = example<Resume> {
    distinct()
    forUpdate(true)
    select("name", "mobile")
    notSelect("id")
    from("resume")
    andEqualTo {
        name = "xiaoming"
        mobile = "1234"
    }
    where {
        andEqualTo(Resume::address, "b")
        andBetween(Resume::email, "1234", null)
        andLike(Resume::email, "xi_ao")
    }
    andWhere {
        andIsNull(Resume::email)
    }
    orWhere {
        andIsNull(Resume::language)
        orIsNull(Resume::email)
    }
    orderByDesc("name", "mobile")
}
mapper.selectByExample(example)
实际执行sql
SELECT distinct name , mobile FROM resume WHERE ( address = ? and email >= ? and email like ? ) and ( email is null ) or ( language is null or email is null ) and ( name = ? and mobile = ? and is_history = ? and is_deliver = ? ) order by name,mobile Desc FOR UPDATE
Clone this wiki locally