Springboot+mybatis-plus+PostgreSql CURD 实现
- pom.xml :加载PostgreSql数据包
-
`<dependency>
-
<groupId>org.postgresql</groupId>
-
<artifactId>postgresql</artifactId>
-
<version>42.2.26</version>
-
</dependency>
-
<!-- PostgreSql 驱动包 -->
-
<dependency>
-
<groupId>net.postgis</groupId>
-
<artifactId>postgis-jdbc</artifactId>
-
<version>2.5.0</version>
-
</dependency>`
- application-dev.yml :连接数据库
-
spring:
-
datasource:
-
type: com.alibaba.druid.pool.DruidDataSource
-
druid:
-
driver-class-name: org.postgresql.Driver
-
url: jdbc:postgresql://localhost:5432/test?currentSchema=public
-
username: postgres
-
password: ****
-
jpa:
-
properties:
-
hibernate:
-
dialect: org.hibernate.dialect.PostgreSQLDialect
-
hbm2ddl:
-
auto: update
-
- 数据库sql文件
-
-- ----------------------------
-
-- Table structure for geometries
-
-- ----------------------------
-
DROP TABLE IF EXISTS "public"."geometries";
-
CREATE TABLE "public"."geometries" (
-
"name" varchar COLLATE "pg_catalog"."default",
-
"geom" "public"."geometry",
-
"crttime" timestamp(6)
-
)
-
;
-
COMMENT ON TABLE "public"."geometries" IS '点、线、面';
-
-
-- ----------------------------
-
-- Records of geometries
-
-- ----------------------------
-
INSERT INTO "public"."geometries" VALUES ('Point', '010100000000000000000000000000000000000000', '2022-10-27 09:25:19');
-
INSERT INTO "public"."geometries" VALUES ('Linestring', '01020000000400000000000000000000000000000000000000000000000000F03F000000000000F03F0000000000000040000000000000F03F00000000000000400000000000000040', '2022-10-26 09:25:23');
-
INSERT INTO "public"."geometries" VALUES ('Polygon', '0103000000010000000500000000000000000000000000000000000000000000000000F03F0000000000000000000000000000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000000000', '2022-10-28 09:25:27');
-
INSERT INTO "public"."geometries" VALUES ('PolygonWithHole', '01030000000200000005000000000000000000000000000000000000000000000000002440000000000000000000000000000024400000000000002440000000000000000000000000000024400000000000000000000000000000000005000000000000000000F03F000000000000F03F000000000000F03F0000000000000040000000000000004000000000000000400000000000000040000000000000F03F000000000000F03F000000000000F03F', '2022-10-25 09:25:31');
-
INSERT INTO "public"."geometries" VALUES ('Collection', '0107000000020000000101000000000000000000004000000000000000000103000000010000000500000000000000000000000000000000000000000000000000F03F0000000000000000000000000000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000000000', '2022-11-01 09:25:34');
-
- controller
-
-- ----------------------------
-
-- Table structure for geometries
-
-- ----------------------------
-
DROP TABLE IF EXISTS "public"."geometries";
-
CREATE TABLE "public"."geometries" (
-
"name" varchar COLLATE "pg_catalog"."default",
-
"geom" "public"."geometry",
-
"crttime" timestamp(6)
-
)
-
;
-
COMMENT ON TABLE "public"."geometries" IS '点、线、面';
-
-
-- ----------------------------
-
-- Records of geometries
-
-- ----------------------------
-
INSERT INTO "public"."geometries" VALUES ('Point', '010100000000000000000000000000000000000000', '2022-10-27 09:25:19');
-
INSERT INTO "public"."geometries" VALUES ('Linestring', '01020000000400000000000000000000000000000000000000000000000000F03F000000000000F03F0000000000000040000000000000F03F00000000000000400000000000000040', '2022-10-26 09:25:23');
-
INSERT INTO "public"."geometries" VALUES ('Polygon', '0103000000010000000500000000000000000000000000000000000000000000000000F03F0000000000000000000000000000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000000000', '2022-10-28 09:25:27');
-
INSERT INTO "public"."geometries" VALUES ('PolygonWithHole', '01030000000200000005000000000000000000000000000000000000000000000000002440000000000000000000000000000024400000000000002440000000000000000000000000000024400000000000000000000000000000000005000000000000000000F03F000000000000F03F000000000000F03F0000000000000040000000000000004000000000000000400000000000000040000000000000F03F000000000000F03F000000000000F03F', '2022-10-25 09:25:31');
-
INSERT INTO "public"."geometries" VALUES ('Collection', '0107000000020000000101000000000000000000004000000000000000000103000000010000000500000000000000000000000000000000000000000000000000F03F0000000000000000000000000000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000000000', '2022-11-01 09:25:34');
-
- controller
-
-
-
-
-
public class GeoController {
-
-
private PgGisService pgGisService;
-
-
/**
-
* 测试pg数据库
-
*/
-
-
-
public CommonResult list(String name,String crttime){
-
List<PgGis> list = pgGisService.list1(name,crttime);
-
if(CollUtil.isEmpty(list)){
-
return CommonResult.failed("没有查询到值");
-
}
-
return CommonResult.success(list);
-
}
-
-
-
-
public CommonResult list2(PgGis pgGis){
-
List<PgGis> list = pgGisService.list2(pgGis);
-
if(CollUtil.isEmpty(list)){
-
return CommonResult.failed("没有查询到值");
-
}
-
return CommonResult.success(list);
-
}
-
-
}
-
- service
-
-
-
-
-
public class GeoController {
-
-
private PgGisService pgGisService;
-
-
/**
-
* 测试pg数据库
-
*/
-
-
-
public CommonResult list(String name,String crttime){
-
List<PgGis> list = pgGisService.list1(name,crttime);
-
if(CollUtil.isEmpty(list)){
-
return CommonResult.failed("没有查询到值");
-
}
-
return CommonResult.success(list);
-
}
-
-
-
-
public CommonResult list2(PgGis pgGis){
-
List<PgGis> list = pgGisService.list2(pgGis);
-
if(CollUtil.isEmpty(list)){
-
return CommonResult.failed("没有查询到值");
-
}
-
return CommonResult.success(list);
-
}
-
-
}
-
- service-impl
-
@Slf4j
-
@Service
-
@Transactional
-
public class PgGisServiceImpl extends BaseServiceImpl<geometriesMapper, PgGis> implements PgGisService {
-
-
@Autowired
-
private geometriesMapper geometriesMapper;
-
-
@Override
-
public List<PgGis> list1(String name,String crttime) {
-
return geometriesMapper.list1(name, crttime);
-
}
-
-
@Override
-
public List<PgGis> list2(PgGis pgGis) {
-
LambdaQueryWrapper<PgGis> qw = new QueryWrapper<PgGis>().lambda()
-
.eq(pgGis.getGeom() != null, PgGis::getGeom, pgGis.getGeom())
-
.eq(pgGis.getName() != null, PgGis::getName, pgGis.getName())
-
.eq(pgGis.getCrttime() != null, PgGis::getCrttime, pgGis.getCrttime());
-
return list(qw);
-
}
-
-
}
-
- entity
-
-
-
-
public class PgGis implements Serializable {
-
private static final long serialVersionUID = 1L;
-
-
-
-
private String name;
-
-
-
-
private String geom;
-
-
-
-
private Date crttime;
-
-
}
-
- mapper
-
@Mapper
-
public interface geometriesMapper extends BaseMapper<PgGis> {
-
-
static final String FIND_GEOJSON_SQL="<script>"
-
"SELECT name, st_asgeojson(geom) geom, crttime FROM geometries p "
-
"where 1 = 1 "
-
"<if test='null != name'>and p.name like concat('%', #{name}, '%')</if>"
-
"<if test='null != crttime'>and p.crttime like concat('%', #{crttime}, '%')</if>"
-
"</script>";
-
@Select(FIND_GEOJSON_SQL)
-
List<PgGis> list1(@Param("name")String name, @Param("crttime")String crttime);
-
}
-
- config配置
-
-
public class MyGeometryTypeHandler extends BaseTypeHandler<String> {
-
-
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
-
PGgeometry pGgeometry = new PGgeometry(parameter);
-
ps.setObject(i, pGgeometry);
-
}
-
-
-
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
-
PGgeometry pGgeometry = new PGgeometry(rs.getString(columnName));
-
if (pGgeometry == null) {
-
return null;
-
}
-
return pGgeometry.toString();
-
}
-
-
-
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
-
PGgeometry pGgeometry = new PGgeometry(rs.getString(columnIndex));
-
if (pGgeometry == null) {
-
return null;
-
}
-
return pGgeometry.toString();
-
}
-
-
-
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
-
-
PGgeometry pGgeometry = new PGgeometry(cs.getString(columnIndex));
-
if (pGgeometry == null) {
-
return null;
-
}
-
return pGgeometry.toString();
-
}
-
}
-
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfkfieg
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24