@Service public class TestServiceImpl extends ServiceImpl<TestDao, Test> implements ITestService { @Autowired private TestDao testDao; @Override public IPage<Test> getPage(int currentPage, int pageSize) { IPage page = new Page(currentPage,pageSize); testDao.selectPage(page,null); return page; } @Override public IPage<Test> getPage(int currentPage, int pageSize, Test test) { LambdaQueryWrapper<Test> lqw = new LambdaQueryWrapper<Test>(); lqw.eq(test.getTestId()!=null,Test::getTestId,test.getTestId()); lqw.like(Strings.isNotEmpty(test.getTestName()), Test::getTestName, test.getTestName()); lqw.like(Strings.isNotEmpty(test.getTestLabel()), Test::getTestLabel, test.getTestLabel()); IPage page = new Page(currentPage, pageSize); testDao.selectPage(page, lqw); return page; } @Override public void collectTestView(Integer id) { Test test = testDao.selectById(id); int a = testDao.selectById(id).getTestViews(); a=a+1; test.setTestViews(a); testDao.updateById(test); } @Override public List<Map> getByCompany(String company) { return testDao.selectByCompany(company); } @Override public List<Map> getSome(Integer id) { return testDao.selectAll(id); } }
时间: 2024-01-15 16:02:49 浏览: 70
这段代码是一个 Java 类,实现了一个接口 ITestService,并且继承了 ServiceImpl 类。其中 getPage() 方法获取指定页数和大小的测试数据,getPage() 方法还可以通过传入 Test 对象来进行筛选查询,collectTestView() 方法用于收集测试数据的浏览量,getByCompany() 方法和 getSome() 方法分别用于根据公司和 ID 获取测试数据的集合。这段代码似乎是使用了 MyBatis-Plus 框架对数据库进行操作。
相关问题
postGis有这样一张表test:字段id,空间数据geom,time字段。现在需要前端输入时间参数,后端按这个参数查询数据库数据,将查询到的数据导出为一份shp文件。现在我使用语句 select id,ST_AsText(geom) as wkt,time from test where time='2023 04-25 00:00'查出需要的结果,请给出一份完整的代码示例,使用springboot框架,给出包含实体类、mapper层、service、serviceImpl、controller的所有代码 可以使用geotools或gdal
以下是使用 GeoTools 的完整代码示例,包含实体类、mapper 层、service、serviceImpl、controller 的所有代码。
1. 实体类
```java
import org.locationtech.jts.geom.Geometry;
public class TestEntity {
private Long id;
private Geometry geom;
private String time;
// getter 和 setter 略
}
```
2. Mapper 层
```java
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.locationtech.jts.geom.Geometry;
import java.util.List;
@Mapper
public interface TestMapper {
@Select("SELECT id, geom, time FROM test WHERE time = #{time}")
List<TestEntity> findByTime(@Param("time") String time);
@Select("SELECT ST_AsText(geom) FROM test WHERE id = #{id}")
String findWktById(@Param("id") Long id);
}
```
3. Service
```java
import java.util.List;
public interface TestService {
List<TestEntity> findByTime(String time);
byte[] exportToShp(List<TestEntity> entities);
}
```
4. ServiceImpl
```java
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Autowired
private DataSource dataSource;
@Override
public List<TestEntity> findByTime(String time) {
return testMapper.findByTime(time);
}
@Override
@Transactional(readOnly = true)
public byte[] exportToShp(List<TestEntity> entities) {
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Object> params = new HashMap<>();
params.put("url", getTempFileUrl());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore dataStore;
try {
dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
} catch (IOException e) {
throw new RuntimeException("创建 ShapefileDataStore 失败", e);
}
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName("TestEntity");
typeBuilder.setCRS(org.geotools.referencing.crs.DefaultGeographicCRS.WGS84);
typeBuilder.add("geom", Geometry.class);
typeBuilder.add("id", Long.class);
typeBuilder.add("time", String.class);
SimpleFeatureType featureType = typeBuilder.buildFeatureType();
try {
dataStore.createSchema(featureType);
} catch (IOException e) {
throw new RuntimeException("创建 Schema 失败", e);
}
Transaction transaction = new DefaultTransaction("create");
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource;
try {
featureSource = dataStore.getFeatureSource(typeName);
} catch (IOException e) {
throw new RuntimeException("获取 FeatureSource 失败", e);
}
List<SimpleFeature> features = new ArrayList<>();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
for (TestEntity entity : entities) {
Geometry geometry = entity.getGeom();
Long id = entity.getId();
String time = entity.getTime();
SimpleFeature feature = featureBuilder.buildFeature(null);
feature.setDefaultGeometry(geometry);
feature.setAttribute("id", id);
feature.setAttribute("time", time);
features.add(feature);
}
try {
SimpleFeatureCollection featureCollection = new ListFeatureCollection(featureType, features);
featureSource.addFeatures(featureCollection);
transaction.commit();
} catch (IOException e) {
throw new RuntimeException("添加 Feature 失败", e);
} finally {
try {
transaction.close();
} catch (IOException e) {
throw new RuntimeException("关闭 Transaction 失败", e);
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
org.geotools.data.DataUtilities.encodeFeatureCollection(featureSource.getFeatures(), outputStream);
} catch (IOException e) {
throw new RuntimeException("编码 FeatureCollection 失败", e);
}
return outputStream.toByteArray();
}
private String getTempFileUrl() {
try {
return "file://" + java.nio.file.Files.createTempFile("temp", ".shp").toAbsolutePath();
} catch (IOException e) {
throw new RuntimeException("创建临时文件失败", e);
}
}
}
```
5. Controller
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/export")
public ResponseEntity<byte[]> export(@RequestParam String time) {
List<TestEntity> entities = testService.findByTime(time);
byte[] shpBytes = testService.exportToShp(entities);
String filename = "test_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".zip";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", filename, StandardCharsets.UTF_8);
return ResponseEntity.ok().headers(headers).body(shpBytes);
}
}
```
希望这份代码对你有所帮助!
@Qualifiter注解的作用
@Qualifier注解的作用是解决使用@Autowired注入属性时可能出现的NoUniqueBeanDefinitionException异常。当有多个满足条件的bean可供自动装配时,程序无法正确判断使用哪一个bean。通过在@Autowired注解上使用@Qualifier注解,可以指定具体要注入的bean的名称,从而解决这个问题。例如,可以使用@Autowired和@Qualifier组合注解来指定要注入的bean的名称,如@Autowired @Qualifier("testServiceImpl1") private TestService testService;。这样,程序就能正确地选择要注入的bean。\[2\]
#### 引用[.reference_title]
- *1* *3* [Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件...](https://blog.csdn.net/zhongyuwang_ysu/article/details/52095899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Spring的注解@Qualifier用法与简析](https://blog.csdn.net/zl18603543572/article/details/127117857)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文