第四天
今天团队完成了:安全管理系统: 星级评估管理 安全管理考核后端。完成了对安全风险检查表的
简单的增删改查。
再然后是套用模版,遇到和一开始代码一样的问题,发现是由于对于Repository不够了解,导致后端传到前端的数据和前端绑定的数据不匹配导致的
部分代码:
package com.example.springbootdemo.repository;
import com.example.springbootdemo.entity.SafetyRiskHistory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
@Repository
public interface SafetyRiskHistoryRepository extends JpaRepository<SafetyRiskHistory, Integer> {
// 使用 @Query 注解自定义查询
@Query("SELECT srh FROM SafetyRiskHistory srh WHERE srh.risk_code = :riskCode")
List<SafetyRiskHistory> findByRiskCode(String riskCode);
@Query("SELECT srh FROM SafetyRiskHistory srh " +
"WHERE (:riskCode IS NULL OR srh.risk_code = :riskCode) " +
"AND (:startDate IS NULL OR srh.operation_time >= :startDate) " +
"AND (:endDate IS NULL OR srh.operation_time <= :endDate)")
Page<SafetyRiskHistory> findByRiskCodeAndTimeRange(String riskCode, Date startDate, Date endDate, Pageable pageable);
}
package com.example.springbootdemo.repository;
import com.example.springbootdemo.entity.safetyrisk;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
@Repository
public interface SafetyriskRepository extends JpaRepository<safetyrisk, Integer> {
@Query("SELECT s FROM safetyrisk s WHERE (:id IS NULL OR s.id = :id) AND (:riskLevel IS NULL OR :riskLevel = '' OR s.risk_level = :riskLevel)")
List
List<safetyrisk> findAllById(Integer id);
@Query("SELECT s FROM safetyrisk s WHERE " +
"(:id IS NULL OR s.id = :id) AND " +
"(:risk_code IS NULL OR s.risk_code = :risk_code) AND " +
"(:department IS NULL OR s.department = :department) AND " +
"(:professional_system IS NULL OR s.professional_system = :professional_system) AND " +
"(:risk_category IS NULL OR s.risk_category = :risk_category) AND " +
"(:risk_item IS NULL OR s.risk_item = :risk_item) AND " +
"(:risk_point IS NULL OR s.risk_point = :risk_point) AND " +
"(:risk_level IS NULL OR s.risk_level = :risk_level) AND " +
"(:harm_degree IS NULL OR s.harm_degree = :harm_degree) AND " +
"(:control_measures IS NULL OR s.control_measures = :control_measures) AND " +
"(:control_position IS NULL OR s.control_position = :control_position) AND " +
"(:control_personnel IS NULL OR s.control_personnel = :control_personnel) AND " +
"(:quantified_requirements IS NULL OR s.quantified_requirements = :quantified_requirements) AND " +
"(:entry_start_date IS NULL OR s.entry_start_date = :entry_start_date) AND " +
"(:entry_end_date IS NULL OR s.entry_end_date = :entry_end_date) AND " +
"(:review_status IS NULL OR s.review_status = :review_status) AND " +
"(:review_date IS NULL OR s.review_date = :review_date)")
Page<safetyrisk> findByMultipleConditions(
Integer id,
String risk_code,
String department,
String professional_system,
String risk_category,
String risk_item,
String risk_point,
String risk_level,
String harm_degree,
String control_measures,
String control_position,
String control_personnel,
String quantified_requirements,
Date entry_start_date,
Date entry_end_date,
String review_status,
Date review_date,
Pageable pageable
);
@Query("SELECT s FROM safetyrisk s WHERE s.risk_level = :riskLevel")
List<safetyrisk> findByRiskLevel(@Param("riskLevel") String riskLevel);
@Query("SELECT s FROM safetyrisk s " +
"WHERE (:id IS NULL OR s.id = :id) AND " +
"(:risk_code IS NULL OR s.risk_code = :risk_code) AND " +
"(:department IS NULL OR s.department = :department) AND " +
"(:professional_system IS NULL OR s.professional_system = :professional_system) AND " +
"(:risk_category IS NULL OR s.risk_category = :risk_category) AND " +
"(:risk_item IS NULL OR s.risk_item = :risk_item) AND " +
"(:risk_point IS NULL OR s.risk_point = :risk_point) AND " +
"(:risk_level IS NULL OR s.risk_level = :risk_level) AND " +
"(:harm_degree IS NULL OR s.harm_degree = :harm_degree) AND " +
"(:control_measures IS NULL OR s.control_measures = :control_measures) AND " +
"(:control_position IS NULL OR s.control_position = :control_position) AND " +
"(:control_personnel IS NULL OR s.control_personnel = :control_personnel) AND " +
"(:quantified_requirements IS NULL OR s.quantified_requirements = :quantified_requirements) AND " +
"(:entry_start_date IS NULL OR s.entry_start_date = :entry_start_date) AND " +
"(:entry_end_date IS NULL OR s.entry_end_date = :entry_end_date) AND " +
"(:review_status IS NULL OR s.review_status = :review_status) AND " +
"(:review_date IS NULL OR s.review_date = :review_date)")
List<safetyrisk> findByMultipleConditions(
@Param("id") Integer id,
@Param("risk_code") String risk_code,
@Param("department") String department,
@Param("professional_system") String professional_system,
@Param("risk_category") String risk_category,
@Param("risk_item") String risk_item,
@Param("risk_point") String risk_point,
@Param("risk_level") String risk_level,
@Param("harm_degree") String harm_degree,
@Param("control_measures") String control_measures,
@Param("control_position") String control_position,
@Param("control_personnel") String control_personnel,
@Param("quantified_requirements") String quantified_requirements,
@Param("entry_start_date") Date entry_start_date,
@Param("entry_end_date") Date entry_end_date,
@Param("review_status") String review_status,
@Param("review_date") Date review_date
);
@Query("SELECT s FROM safetyrisk s WHERE s.risk_code = :risk_code")
List<safetyrisk> findByRiskCode(@Param("risk_code") String risk_code);
}

浙公网安备 33010602011771号