跳至主要內容

数据库函数(新)

xuejmnet大约 5 分钟

数据库函数

框架默认提供了部分数据库函数,并且支持适配所有的数据库.包括常见的字符串函数和时间格式化函数,包括数学函数等

通用函数

方法描述entitylambdaproperty
nullOrDefault如果列为null则返回参数值b.id().nullOrDefault("123")b.fx().nullOrDefault(BlogEntity::getId, "123")b.fx().nullOrDefault("id", "123")
count统计数量返回longb.id().count()b.fx().count(BlogEntity::getId)b.fx().count("id")
intCount统计数量返回intb.id().intCount()b.fx().intCount(BlogEntity::getId)b.fx().intCount("id")
min最小值b.id().min()b.fx().min(BlogEntity::getId)b.fx().min("id")
max最大值b.id().max()b.fx().max(BlogEntity::getId)b.fx().max("id")

字符串函数

方法描述entitylambdaproperty
subString切割字符串,默认起始0b.id().subString(1,12)b.fx().subString(BlogEntity::getId,1,12)b.fx().subString("id",1,12)
concat链接多个列或者值b.expression().concat(x->x.expression(b.content()).value("123").expression(b.id()))fx.concat(x -> x.column(BlogEntity::getContent).value("123").column(BlogEntity::getId))fx.concat(x -> x.column("content").value("123").column("id"))
toLower转成小写b.content().toLower()b.fx().toLower(BlogEntity::getContent)b.fx().toLower("content")
toUpper转成大写b.content().toUpper()b.fx().toUpper(BlogEntity::getContent)b.fx().toUpper("content")
trim去掉前后空格b.content().trim()b.fx().trim(BlogEntity::getContent)b.fx().trim("content")
trimStart去掉前面空格b.content().trimStart()b.fx().trimStart(BlogEntity::getContent)b.fx().trimStart("content")
trimEnd去掉后面空格b.content().trimEnd()b.fx().trimEnd(BlogEntity::getContent)b.fx().trimEnd("content")
replace替换字符串b.content().replace("123","456")b.fx().replace(BlogEntity::getContent,"123","456")b.fx().replace("content","123","456")
leftPad往左补值b.content().leftPad(2,'a')b.fx().leftPad(BlogEntity::getContent,2,'a')b.fx().leftPad("content",2,'a')
rightPad往右补值b.content().rightPad(2,'a')b.fx().rightPad(BlogEntity::getContent,2,'a')b.fx().rightPad("content",2,'a')
join字符串多列join组合返回常用语group+逗号组合b.content().join(',')b.fx().join(BlogEntity::getContent,',')b.fx().join("content",',')
length字符串长度b.id().length()b.fx().length(BlogEntity::getId)b.fx().length("id")
compareTo比较字符串大小b.content().compareTo("aaaa")b.fx().stringCompareTo(BlogEntity::getContent, "aaaa")b.fx().stringCompareTo("content", "aaaa")

时间函数

方法描述
format格式化日期支持java格式化
plus增加时间
plusMonths增加月份
plusYears增加年份
dayOfYear当前天数在一年中代表第几天
dayOfWeek当前天数在一年中代表第几天 0-6星期日为0
year返回年份
month返回月份1-12
day返回月份中的天数1-31
hour返回小时0-23
minute返回分钟0-59
second返回秒数0-59
duration返回间隔天/小时/.... a.duration(b,DateTimeDurationEnum.Days) a比b大多少天,如果a小于b则返回负数 两个日期a,b之间相隔多少天
now当前时间
utcNow当前UTC时间

案例



List<BlogEntity> list = easyEntityQuery.queryable(BlogEntity.class)
        .where(b -> {
                b.id().nullOrDefault("123").eq("123");
                b.id().subString(1, 20).eq("456");
                b.expression().concat(x->x.expression(b.content()).value("123").expression(b.id())).eq("789");
                b.content().toUpper().eq("abc");
                b.content().toLower().eq("def");
                b.content().trim().eq("a");
                b.content().trimStart().eq("b");
                b.content().trimEnd().eq("c");
                b.content().replace("123", "456").eq("aaa");
                b.content().leftPad(2, 'a').eq("aa");
                b.content().rightPad(2, 'a').eq("aa");
                b.content().length().eq(1);
                b.content().compareTo("aaaa").ge(0);
        }).toList();


==> Preparing: SELECT `id`,`create_time`,`update_time`,`create_by`,`update_by`,`deleted`,`title`,`content`,`url`,`star`,`publish_time`,`score`,`status`,`order`,`is_top`,`top` FROM `t_blog` WHERE `deleted` = ? AND IFNULL(`id`,?) = ? AND SUBSTR(`id`,2,20) = ? AND CONCAT(`content`,?,`id`) = ? AND UPPER(`content`) = ? AND LOWER(`content`) = ? AND TRIM(`content`) = ? AND LTRIM(`content`) = ? AND RTRIM(`content`) = ? AND REPLACE(`content`,?,?) = ? AND LPAD(`content`, 2, ?) = ? AND RPAD(`content`, 2, ?) = ? AND CHAR_LENGTH(`content`) = ? AND STRCMP(`content`,?) >= ?
==> Parameters: false(Boolean),123(String),123(String),456(String),123(String),789(String),abc(String),def(String),a(String),b(String),c(String),123(String),456(String),aaa(String),a(String),aa(String),a(String),aa(String),1(Integer),aaaa(String),0(Integer)
<== Time Elapsed: 6(ms)
<== Total: 0

返回列函数


String sql1 = easyClient.queryable(Topic.class)
        .where(o -> o.eq(Topic::getId, "1"))
        .select(String.class, o -> o.sqlFunc(o.fx().ifNull(Topic::getId, "1"))).toSQL();
Assert.assertEquals("SELECT IFNULL(t.`id`,?) FROM `t_topic` t WHERE t.`id` = ?", sql1);

条件函数


String sql = easyQuery.queryable(Topic.class)
                .where(o -> o.eq(o.fx().ifNull(Topic::getId, "123"), o.fx().ifNull(Topic::getTitle, "456")))
                .toSQL();
Assert.assertEquals("SELECT `id`,`stars`,`title`,`create_time` FROM `t_topic` WHERE IFNULL(`id`,?) = IFNULL(`title`,?)", sql);

String sql = easyQuery.queryable(Topic.class)
            .where(o -> o.eq(Topic::getId, "1")
            )
            .orderByDesc(o -> o.column(Topic::getCreateTime))
            .select(Topic.class, o -> o.sqlFuncAs(o.fx().dateTimeFormat(Topic::getCreateTime, "yyyy-MM-dd"), Topic::getTitle)).toSQL();
Assert.assertEquals("SELECT DATE_FORMAT(t.`create_time`,'%Y-%m-%d') AS `title` FROM `t_topic` t WHERE t.`id` = ? ORDER BY t.`create_time` DESC", sql);
上次编辑于:
贡献者: Hoysing