• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot整合Mybatis实现高德地图定位并将数据存入数据库的步骤详解

springboot 搞代码 4年前 (2022-01-05) 31次浏览 已收录 0个评论

这篇文章主要介绍了SpringBoot整合Mybatis实现高德地图定位并将数据存入数据库的步骤详解,本文分步骤通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

第一步配置yml文件

 server: port: 8080 spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver thymeleaf: cache: false prefix: classpath:/templates/ suffix: .html encoding: UTF-8 content-type: text/html mode: HTML5 mybatis: mapper-locations: classpath:mapping/GaoDe.xml type-aliases-package: car2021.winter.com.demo.entity logging: file: name: car2021.winter.log

第二步对Mybatis进行配置,并将实体映射。

     insert into GaoDe (time ,Longitude,Latitude,Position) values(#{time},#{Longitude},#{Latitude},#{Position}) 

第三步写HTML,并引入自己的高德API(需要申请key)

   <title>高德地图</title> body { margin: 0; height: 100%; width: 100%; position: absolute; font-size: 12px; } #mapContainer { position: absolute; top: 0; left: 0; right: 0; bottom: 0; } #tip { background-color: #fff; border: 1px solid #ccc; padding-left: 10px; padding-right: 2px; position: absolute; min-height: 65px; top: 10px; font-size: 12px; right: 10px; border-radius: 3px; overflow: hidden; line-height: 20px; min-width: 400px; } #tip input[type="button"] { background-color: #0D9BF2; height: 25px; text-align: center; line-height: 25px; color: #fff; font-size: 12px; border-radius: 3px; outline: none; border: 0; cursor: pointer; } #tip input[type="text"] { height: 25px; border: 1px solid #ccc; padding-left: 5px; border-radius: 3px; outline: none; } #pos { height: 70px; background-color<strong style="color:transparent">来源gaodai#ma#com搞@代~码网</strong>: #fff; padding-left: 10px; padding-right: 10px; position: absolute; font-size: 12px; right: 10px; bottom: 30px; border-radius: 3px; line-height: 30px; border: 1px solid #ccc; } #pos input { border: 1px solid #ddd; height: 23px; border-radius: 3px; outline: none; } #result1 { max-height: 300px; }  <div id="mapContainer"></div><div id="tip"> <b>当前位置</b><br><b>请输入关键字:</b><button id="GaoDe" type="button" style="color: #4cae4c;width: 95%">点击提交坐标信息</button><div id="result1"></div></div><div id="pos"> <b>鼠标左键在地图上单击获取坐标</b><br><div>X: Y:</div></div>

创建实体类GaoDe

 package car2021.winter.com.demo.entity; import org.springframework.format.annotation.DateTimeFormat; /** * Author: PXY * Email: [email protected] * Date: 2021/1/4 */ public class GaoDe { @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private String time; private double Longitude;//经度 private double Latitude;//维度 private String Position;//当前位置 public GaoDe() { } public GaoDe(String time, double Longitude, double Latitude, String Position) { this.time = time; this.Longitude = Longitude; this.Latitude = Latitude; this.Position = Position; } @Override public String toString() { return "GaoDe{" + "time='" + time + '\'' + ", Longitude=" + Longitude + ", Latitude=" + Latitude + ", Position='" + Position + '\'' + '}'; } public String getId() { return time; } public void setId(String time) { this.time = time; } public double getLongitude() { return Longitude; } public void setLongitude(double longitude) { Longitude = longitude; } public double getLatitude() { return Latitude; } public void setLatitude(double latitude) { Latitude = latitude; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getPosition() { return Position; } public void setPosition(String position) { Position = position; } }

创建实体类newsCode

 package car2021.winter.com.demo.entity; /** * Author: PXY * Email: [email protected] * Date: 2021/1/5 */ public class NewsCode { private String code; private String message; public NewsCode(String code, String message) { this.code = code; this.message = message; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } 

创建接口Code

 package car2021.winter.com.demo.entity; /** * Author: PXY * Email: [email protected] * Date: 2021/1/5 */ public interface Code { String Code_OK = "200"; String message_OK = "数据提交成功"; String Code_NotOK="404"; String message_Not_Ok="数据提交失败"; String Code_Service="500"; String message_Service="服务器有点问题"; }

Mapper接口

 package car2021.winter.com.demo.mapper; import car2021.winter.com.demo.entity.GaoDe; import org.springframework.stereotype.Repository; /** * Author: PXY * Email: [email protected] * Date: 2021/1/5 */ @Repository public interface GaoDeMapper { /** * 将经纬度信息插入数据库 */ void insertGaoDe(GaoDe gaoDe); }

Service

 package car2021.winter.com.demo.service; import car2021.winter.com.demo.entity.GaoDe; import car2021.winter.com.demo.mapper.GaoDeMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Author: PXY * Email: [email protected] * Date: 2021/1/5 */ @Service public class GaoDeService { @Autowired GaoDeMapper gaoDeMapper; public void insertGaoDe(GaoDe gaoDe) { gaoDeMapper.insertGaoDe(gaoDe); } }

controller

 package car2021.winter.com.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Author: PXY * Email: [email protected] * Date: 2021/1/5 */ @Controller public class GaoDeMap { @RequestMapping("/GaoDe") public String GaoDeMap() { return "GaoDe"; } }
 package car2021.winter.com.demo.controller; import car2021.winter.com.demo.entity.Code; import car2021.winter.com.demo.entity.GaoDe; import car2021.winter.com.demo.entity.NewsCode; import car2021.winter.com.demo.service.GaoDeService; import ch.qos.logback.core.encoder.EchoEncoder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * Author: PXY * Email: [email protected] * Date: 2021/1/5 */ @RestController public class AlterGaoDe { @Autowired private GaoDeService gaoDeService; @RequestMapping(value = "/submitMap", method = RequestMethod.POST) public NewsCode insertMap(@RequestBody GaoDe gaoDe) { try { gaoDeService.insertGaoDe(gaoDe); System.out.println(gaoDe); return new NewsCode(Code.Code_OK, Code.message_OK); } catch (Exception e) { e.printStackTrace(); return new NewsCode(Code.Code_NotOK, Code.message_Not_Ok); } } }
 //最后启动主类 @SpringBootApplication @MapperScan("car2021.winter.com.demo.mapper") public class CarInfoApplication { public static void main(String[] args) { SpringApplication.run(CarInfoApplication.class, args); } }

访问localhost://post/Gaode就是要求的界面

代码结构

以上就是SpringBoot整合Mybatis实现高德地图定位并将数据存入数据库的步骤详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot整合Mybatis实现高德地图定位并将数据存入数据库的步骤详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址