网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.postgis.Geometry;
import org.postgis.PGgeometry;
@MappedTypes({String.class})
public class PgGeometryTypeHandler extends BaseTypeHandler {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
PGgeometry pGgeometry = new PGgeometry(parameter);
Geometry geometry = pGgeometry.getGeometry();
geometry.setSrid(4326);
ps.setObject(i, pGgeometry);
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
String string = rs.getString(columnName);
return getResult(string);
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String string = rs.getString(columnIndex);
return getResult(string);
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String string = cs.getString(columnIndex);
return getResult(string);
}
private String getResult(String string) throws SQLException {
PGgeometry pGgeometry = new PGgeometry(string);
String s = pGgeometry.toString();
return s.replace("SRID=4326;", "");
}
}
注意,在getResult()中关于4326坐标系的定义,可以根据需要进行废弃。这里写上为了统一投影坐标系。
### 三、在数据中创建表,建表语句如下:
create table biz_point_test(
id int8 primary key,
name varchar(100),
geom geometry(Point,4326)
);
### 四、定义Mybatis-plus实体
package com.hngtghy.project.extend.student.domain;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.hngtghy.framework.handler.PgGeometryTypeHandler;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@TableName(value =“biz_point_test”,autoResultMap = true)
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class PointTest {
@TableId
private Long id;
private String name;
@TableField(typeHandler = PgGeometryTypeHandler.class)
private String geom;
@TableField(exist=false)
private String geomJson;
}
提醒:1、在属性上使用@TableField(typeHandler=xxx)来指定对应的类型转换器。2、需要在实体上定义autoResultMap=true。否则配置不一定生效。
### 五、定义mapper查询器
package com.hngtghy.project.extend.student.mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hngtghy.project.extend.student.domain.PointTest;
public interface PointTestMapper extends BaseMapper{
static final String FIND_GEOJSON_SQL="<script>"
+ "select st_asgeojson(geom) as geomJson from biz_point_test "
+ "where id = #{id} "
+ "<if test='null != name'>and p.name like concat('%', #{name}, '%')</if>"
+ "</script>";
@Select(FIND_GEOJSON_SQL)
PointTest findGeoJsonById(@Param("id")Long id,@Param("name")String name);
}
### 六、定义service业务类
package com.hngtghy.project.extend.student.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hngtghy.project.extend.student.domain.PointTest;
import com.hngtghy.project.extend.student.mapper.PointTestMapper;
import com.hngtghy.project.extend.student.service.IPointTestService;
@Service
public class PointTestServcieImpl extends ServiceImpl<PointTestMapper, PointTest> implements IPointTestService{
@Autowired
private PointTestMapper pointMapper;
@Override
public PointTest selectById(Long id) {
return pointMapper.selectById(id);
}
@Override
public List<PointTest> selectList(PointTest point) {
QueryWrapper<PointTest> queryWrapper = new QueryWrapper<PointTest>();
queryWrapper.select("id,name");
return this.getBaseMapper().selectList(queryWrapper);
}
@Override
public int insertPointTest(PointTest point) {
return pointMapper.insert(point);
}
@Override
public int updatePoint(PointTest point) {
return pointMapper.updateById(point);
}
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新