SELECT m.FNUMBER as id, m.FNAME_L2 as name, d.FNAME_L2 as unit, m.FMODEL as model FROM PONYPUNI3.T_BD_MATERIALPURCHASING mp INNER JOIN PONYPUNI3.T_BD_MATERIAL m ON m.FID = mp.FMATERIALID LEFT OUTER JOIN PONYPUNI3.T_BD_MeasureUnit d on m.fbaseunit=d.fid WHERE mp.FSTATUS = 1 AND m.FSTATUS = 1 AND mp.FORGUNIT=:org AND ( m.FNUMBER LIKE :keyword OR m.FNAME_L2 LIKE :keyword OR m.FMODEL LIKE :keyword ) 怎么用spring jpa @JoinColumn实现
时间: 2024-04-22 13:24:05 浏览: 129
要使用Spring JPA的@JoinColumn注解实现上述查询,你需要进行以下步骤:
1. 创建一个实体类,映射到数据库中的表PONYPUNI3.T_BD_MATERIALPURCHASING。示例代码如下:
```java
@Entity
@Table(name = "PONYPUNI3.T_BD_MATERIALPURCHASING")
public class MaterialPurchasing {
@Id
@Column(name = "FID")
private Long id;
@OneToOne
@JoinColumn(name = "FMATERIALID")
private Material material;
// 添加其他属性对应数据库字段
// 添加对应的getter和setter方法
}
```
2. 创建一个实体类,映射到数据库中的表PONYPUNI3.T_BD_MATERIAL。示例代码如下:
```java
@Entity
@Table(name = "PONYPUNI3.T_BD_MATERIAL")
public class Material {
@Id
@Column(name = "FID")
private Long id;
@Column(name = "FNUMBER")
private String number;
@Column(name = "FNAME_L2")
private String name;
@Column(name = "FMODEL")
private String model;
@OneToOne(mappedBy = "material")
private MaterialPurchasing materialPurchasing;
@OneToOne
@JoinColumn(name = "FBASEUNIT")
private MeasureUnit baseUnit;
// 添加其他属性对应数据库字段
// 添加对应的getter和setter方法
}
```
3. 创建一个实体类,映射到数据库中的表PONYPUNI3.T_BD_MeasureUnit。示例代码如下:
```java
@Entity
@Table(name = "PONYPUNI3.T_BD_MeasureUnit")
public class MeasureUnit {
@Id
@Column(name = "FID")
private Long id;
@Column(name = "FNAME_L2")
private String name;
// 添加其他属性对应数据库字段
// 添加对应的getter和setter方法
}
```
4. 创建一个Spring Data JPA的Repository接口,用于对数据库表PONYPUNI3.T_BD_MATERIALPURCHASING进行操作。示例代码如下:
```java
@Repository
public interface MaterialPurchasingRepository extends JpaRepository<MaterialPurchasing, Long> {
List<MaterialPurchasing> findByStatusAndOrgUnitAndMaterialInfoContaining(int status, String org, String keyword);
}
```
5. 在你的服务类中注入MaterialPurchasingRepository,并调用findByStatusAndOrgUnitAndMaterialInfoContaining方法进行查询。示例代码如下:
```java
@Service
public class MaterialService {
private final MaterialPurchasingRepository materialPurchasingRepository;
public MaterialService(MaterialPurchasingRepository materialPurchasingRepository) {
this.materialPurchasingRepository = materialPurchasingRepository;
}
public List<MaterialPurchasing> searchMaterialInfo(String org, String keyword) {
int status = 1;
return materialPurchasingRepository.findByStatusAndOrgUnitAndMaterialInfoContaining(status, org, keyword);
}
}
```
请确保在Spring Boot项目中已经正确配置了数据库连接和JPA相关的配置。这样就可以使用Spring JPA的@JoinColumn注解实现上述查询了。
阅读全文