一个查询页面中包括关键字、所属机构、证照类型、证照状态、颁发日期、成立批准日期、入库时间等字段,要怎么写一个后端代码从controller、service到mapper层查询数据
时间: 2024-09-23 19:05:34 浏览: 42
证照自助打印页面代码
在Java的Spring Boot框架下,我们可以按照以下步骤编写从Controller到Mapper的查询数据代码:
1. **Controller层**(`com.example.demo.Controllers.YourController.java`)
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.Services.YourService;
public class YourController {
private final YourService yourService;
@Autowired
public YourController(YourService yourService) {
this.yourService = yourService;
}
@GetMapping("/search")
public ResponseEntity<List<YourEntity>> searchData(
@RequestParam(name = "keyword", required = false) String keyword,
@RequestParam(name = "institution", required = false) String institution,
// ...其他参数
) {
try {
List<YourEntity> data = yourService.searchData(keyword, institution, /*...*/);
return new ResponseEntity<>(data, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
```
2. **Service层** (`com.example.demo.Services.YourService.java`)
```java
import org.springframework.stereotype.Service;
import java.util.List;
import com.example.demo.Dtos.YourEntity;
import com.example.demo.Mappers.YourMapper;
@Service
public class YourService {
private final YourMapper yourMapper;
@Autowired
public YourService(YourMapper yourMapper) {
this.yourMapper = yourMapper;
}
public List<YourEntity> searchData(String keyword, String institution, /*...*/) {
List<YourEntity> entities = yourMapper.searchData(keyword, institution, /*...*/);
// 这里可能需要进一步处理返回的数据,如转换或过滤
return entities;
}
}
```
3. **Mapper层** (`com.example.demo.Mappers.YourMapper.java`, 使用MyBatis)
```java
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface YourMapper {
@Select("SELECT * FROM your_table WHERE 1=1")
List<YourEntity> searchData(
@Param("keyword") String keyword,
@Param("institution") String institution,
/* ...其他参数 */
);
}
```
在这个例子中,`your_table`是数据库表名,你需要根据实际数据库结构调整SQL查询。
阅读全文