第一步配置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)
高德地图 当前位置
请输入关键字:鼠标左键在地图上单击获取坐标
X: Y:
创建实体类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实现高德地图定位并将数据存入数据库的文章就介绍到这了,更多相关SpringBoot整合Mybatis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!