关于使用spring boot jpa自定义sql 多表关联查询获取多表字段

很多时候,我们在使用spring boot jpa JpaRepository时需要多表关联查询获取多表字段,但是又不想在实体类对应数据库中实现这个字段(因为实体类中存在,数据库中不存在时会抛出sql异常),更不想写hql,那么可以实现一个取巧的方法。
示例 :实体类

注意:实体类中没有标记表名称

@Entity
@Data
@NoArgsConstructor
@DynamicInsert
public class ChargingJournaliseForCount implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", length = 19, unique = true, nullable = false, columnDefinition = "bigint(19) unsigned comment '主键'")
    private Long id;

    @Column(name = "count", columnDefinition = "bigint(5) NOT NULL default '0' comment '计数器,每次上报持久化一下,以防缓存数据丢失'")
    private Integer count;

    @Column(name = "order_no", columnDefinition = "varchar(32) DEFAULT '' COMMENT '订单号'")
    private String orderNo;
    //其他表字段,可以写成自定义名称
    @Column(name = "pile_property")
    private Integer pilePropertye;
    
    //其他表字段,可以写成自定义名称
    @Column(name = "pile_capacity")
    private BigDecimal pileCapacity;

}

Repository

@Repository
public interface ChargingJournaliseForCountRepository extends JpaRepository<ChargingJournaliseForCount, Long>, JpaSpecificationExecutor<ChargingJournaliseForCount> {


    @Query(nativeQuery = true, value = "SELECT * FROM journalise j " +
            " LEFT JOIN rchives a ON j.pile_inter_no = a.pile_inter_no" +
            " WHERE a.data_platform_id = ?1 AND date_format( j.update_time, '%Y-%m-%d' ) = ?2")
    List<ChargingJournaliseForCount> getJournaliseAndPileTypeByDataPlatformId(Integer dataPlatformId, String date);

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot使用 JPA 配置自动维护表结构,需要进行以下步骤: 1. 在 `application.properties` 文件中配置数据源信息: ``` spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=root spring.datasource.password=123456 ``` 2. 在 `pom.xml` 文件中添加 JPA 和 MySQL 驱动的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 3. 创建实体类,并使用注解描述实体类和字段的属性: ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "username") private String username; @Column(name = "password") private String password; // 省略 getter 和 setter 方法 } ``` 4. 创建 JPA Repository 接口,并继承 `JpaRepository` 接口: ```java public interface UserRepository extends JpaRepository<User, Long> { } ``` 5. 在 `application.properties` 文件中添加 JPA 相关的配置: ``` # 自动建表 spring.jpa.hibernate.ddl-auto=create # 显示 SQL 语句 spring.jpa.show-sql=true # 数据库方言 spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect ``` 6. 启动应用程序,JPA 会自动根据实体类和配置文件的信息生成数据库表结构。如果需要更新表结构,可以将 `spring.jpa.hibernate.ddl-auto` 的值改为 `update`。 以上就是使用 Spring BootJPA 配置自动维护表结构的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值