EntityManager.createNativeQuery的使用

1.单表或多表关联查询

注意:sql的字段别名必须与Entity的属性名相同

Query query = entityManager.createNativeQuery(sql, clazz);

sql:

StringBuilder sb = new StringBuilder();
sb.append("    select           ");
sb.append("     t.id,           ");
sb.append("     t.year,         ");
sb.append("     t.month,        ");
sb.append("     t.port_zone portZone,           ");
sb.append("     t.port_code portCode,           ");
sb.append("     t.port_name_en portNameEn,          ");
sb.append("     t.port_name_cn portNameCn,          ");
sb.append("     t.load_port_code loadPortCode,          ");
sb.append("     t.load_port_name_en loadPortNameEn,         ");
sb.append("     t.load_port_name_cn loadPortNameCn,         ");
sb.append("     t.cargo_volume cargoVolume          ");
sb.append("    from ore_vessel_unload_volume t          ");

clazz:

package com.csg.sds.server.mulitjpa.domain.ods;

import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 港口卸货量
 */
@Data
@Entity
public class VesselUnloadVolumeEntity implements Serializable {

    @Id
    private long id;

    private String year;

    private String month;

    private String portZone;

    private String portCode;

    private String portNameEn;

    private String portNameCn;

    private String loadPortCode;

    private String loadPortNameEn;

    private String loadPortNameCn;

    private BigDecimal cargoVolume;
}

2.单表查询:

注意:sql字段名要在Entity的属性名进行映射

Query query = entityManager.createNativeQuery(sql, clazz);

sql:

StringBuilder sb = new StringBuilder();
sb.append("    select           ");
sb.append("     t.id,           ");
sb.append("     t.year,         ");
sb.append("     t.month,        ");
sb.append("     t.port_zone,           ");
sb.append("     t.port_code,           ");
sb.append("     t.port_name_en,          ");
sb.append("     t.port_name_cn,          ");
sb.append("     t.load_port_code,          ");
sb.append("     t.load_port_name_en,         ");
sb.append("     t.load_port_name_cn,         ");
sb.append("     t.cargo_volume cargoVolume          ");
sb.append("    from ore_vessel_unload_volume t          ");

clazz:

package com.csg.sds.server.mulitjpa.domain.ods;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;

/**
 * 港口卸货量
 */
@Entity
@Data
@Table(name = "ore_vessel_unload_volume", schema = "shipping_ods", catalog = "")
public class VesselUnloadVolumeEntity implements Serializable {

    @Id
    @Column(name = "id")
    private long id;

    @Column(name = "year")
    private String year;

    @Column(name = "month")
    private String month;

    @Column(name = "port_zone")
    private String portZone;

    @Column(name = "port_code")
    private String portCode;

    @Column(name = "port_name_en")
    private String portNameEn;

    @Column(name = "port_name_cn")
    private String portNameCn;

    @Column(name = "load_port_code")
    private String loadPortCode;

    @Column(name = "load_port_name_en")
    private String loadPortNameEn;

    @Column(name = "load_port_name_cn")
    private String loadPortNameCn;

    @Column(name = "cargo_volume")
    private BigDecimal cargoVolume;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值