写在前面

之前用过stringTemplate,类似如下使用方式,但适用函数有限

StringTemplate dateExpr = Expressions.stringTemplate("DATE_FORMAT({0},'%Y-%m-%d')",
                qtc.chargeCostTime);
        JPAQuery<ProjectViewVO> jpaQuery = new JPAQuery<>(em).select(
                Projections.bean(ProjectViewVO.class, // 返回自定义实体的类型
                        dateExpr.as("chargeCostTime"),
                        qtc.chargeCostHour,
                        qt.title,
                        qt.taskId,
                        qp.name.as("projectName"),
                        qp.projectId,
                        qt.groupName.as("projectGroup"),
                        qu.userName,
                        qu.userCode))

四舍五入

    @Test
    // round,四舍五入
// select tcity0_.country as col_0_0_, round(tcity0_.id, 1) as col_1_0_ from t_city tcity0_
    public void round() {
   
        JPAQueryFactory queryFactory = new JPAQueryFactory(em);
        StringTemplate Expr = Expressions.stringTemplate("ROUND({0},1)",
                qtCity.id);

        final List<Tuple> list = queryFactory.select(
                qtCity.country,
                Expr
        ).from(qtCity).fetchResults().getResults();
        System.out.println(list.toString());
    }

    @Test
    // round,四舍五入
// select tcity0_.country as col_0_0_, round(avg(tcity0_.id), 1) as col_1_0_ from t_city tcity0_ group by tcity0_.country
    public void round2() {
   
        JPAQueryFactory queryFactory = new JPAQueryFactory(em);
        StringTemplate Expr = Expressions.stringTemplate("ROUND({0},1)",
                qtCity.id.avg());

        final List<Tuple> list = queryFactory.select(
                qtCity.country,
                Expr
        ).from(qtCity).groupBy(qtCity.country).fetchResults().getResults();
        System.out.println(list.toString());
    }

这里简单介绍下,QueryDsl引入其他Mysql内置函数的记录

一、两个基础配置类

第一个

import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.type.StandardBasicTypes;

// 自定义函数
public class CustomMysqlDialect extends MySQL5Dialect {
   
    public CustomMysqlDialect() {
   
        super();
        this.registerFunction("group_concat", new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)"));
        // 这里还可继续增加自定义引入的Mysql内置函数
    }
}

第二个

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

@Configuration
public class JpaConfiguration {
   
    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
   
        AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql(true);
        adapter.setDatabase(Database.MYSQL);
        // package to CustomMysqlDialect
        adapter.setDatabasePlatform("com.wondertek.pis.config.CustomMysqlDialect");
        adapter.setGenerateDdl(false);
        return adapter;
    }
}

二、使用测试

2.1、group_concat()

JPAQueryFactory jqf = new JPAQueryFactory(em);

        SimpleTemplate<String> simpleTemplate = Expressions.simpleTemplate(String.class, "group_concat({0})", pg.name);

        JPAQuery<ProjectAdminVO> limit = jqf.select(
                Projections.constructor(ProjectAdminVO.class,
                        pp.projectId,
                        pp.status,
                        pp.name,
                        pp.type,
                        pc.contractCode,
                        simpleTemplate,
                        pc.contractName,
                        pc.signStatus,

                        pc.contractMoney,
                        pc.estimateMoney
// ptcp.chargeCostHour.sum().round()
                )).from(pp)
                .leftJoin(pg).on(pp.projectId.eq(pg.projectId))
                .leftJoin(pc).on(pp.contractId.eq(pc.contractId))
// .leftJoin(ptcp).on(pp.projectId.eq(ptcp.projectId))
                .where(builder)
                .groupBy(pp.projectId)
                .orderBy(pp.projectId.asc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize());
        return new PageImpl<>(limit.fetchResults().getResults(), pageable, limit.fetchCount());
    }

控制台打印

select count(distinct projectinf0_.project_id) as col_0_0_
from pis_project projectinf0_
         left outer join pis_group groupinfo1_ on (projectinf0_.project_id = groupinfo1_.project_id)
         left outer join pis_contract contractin2_ on (projectinf0_.contract_id = contractin2_.contract_id);


select projectinf0_.project_id         as col_0_0_,
       projectinf0_.status_            as col_1_0_,
       projectinf0_.name_              as col_2_0_,
       projectinf0_.type_              as col_3_0_,
       contractin2_.contract_code      as col_4_0_,
       group_concat(groupinfo1_.name_) as col_5_0_,
       contractin2_.contract_name      as col_6_0_,
       contractin2_.sign_status        as col_7_0_,
       contractin2_.contract_money     as col_8_0_,
       contractin2_.estimate_money     as col_9_0_
from pis_project projectinf0_
         left outer join pis_group groupinfo1_ on (projectinf0_.project_id = groupinfo1_.project_id)
         left outer join pis_contract contractin2_ on (projectinf0_.contract_id = contractin2_.contract_id)
group by projectinf0_.project_id
order by projectinf0_.project_id asc
limit ?;