后端返回的结果为{"msg":"成功","code":200,"value":[{"fmodel":"423 ","fname":"423"}]},前端如何取出fmodel和fname的值
时间: 2023-09-03 10:10:38 浏览: 51
前端可以通过解析返回的 JSON 数据进行取值,代码示例如下:
```
// 假设后端返回的结果存储在 response 变量中
const response = {"msg":"成功","code":200,"value":[{"fmodel":"423","fname":"423"}]};
// 解析 JSON 数据
const data = JSON.parse(response);
// 获取 fmodel 和 fname 的值
const fmodel = data.value[0].fmodel;
const fname = data.value[0].fname;
// 打印结果
console.log(fmodel, fname);
```
相关问题
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实现
要使用Spring JPA实现上述查询,需要进行以下步骤:
1. 创建一个实体类,映射到数据库中的表PONYPUNI3.T_BD_MATERIALPURCHASING。示例代码如下:
```java
@Entity
@Table(name = "PONYPUNI3.T_BD_MATERIALPURCHASING")
public class MaterialPurchasing {
@Id
@Column(name = "FID")
private Long id;
// 添加其他属性对应数据库字段
// 添加对应的getter和setter方法
}
```
2. 创建一个Spring Data JPA的Repository接口,用于对数据库表PONYPUNI3.T_BD_MATERIALPURCHASING进行操作。示例代码如下:
```java
@Repository
public interface MaterialPurchasingRepository extends JpaRepository<MaterialPurchasing, Long> {
@Query("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)")
List<Object[]> findMaterialInfo(@Param("org") String org, @Param("keyword") String keyword);
}
```
3. 在你的服务类中注入MaterialPurchasingRepository,并调用findMaterialInfo方法进行查询。示例代码如下:
```java
@Service
public class MaterialService {
private final MaterialPurchasingRepository materialPurchasingRepository;
public MaterialService(MaterialPurchasingRepository materialPurchasingRepository) {
this.materialPurchasingRepository = materialPurchasingRepository;
}
public List<Object[]> searchMaterialInfo(String org, String keyword) {
return materialPurchasingRepository.findMaterialInfo(org, keyword);
}
}
```
请确保在Spring Boot项目中已经正确配置了数据库连接和JPA相关的配置。这样就可以使用Spring JPA实现上述查询了。
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实现
要使用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注解实现上述查询了。
阅读全文