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

网友投稿 314 2023-02-09

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: PYGxMfO.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)

当前位置


请输入关键字:

autocomplete="off"/>

鼠标左键在地图上单击获取坐标


value=""/>

创建实体类GaoDe

package car2021.winter.com.demo.entity;

import org.springframework.format.annotation.DateTimeFormat;

/**

* Author: PXY

* Email: 1817865166@qq.com

* 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: 1817865166@qq.com

* 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: 1817865166@qq.com

* 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: 1817865166@qq.com

* 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: 1817865166@qq.com

* 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: 1817865166@qq.com

* 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: 1817865166@qq.com

* 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就是要求的界面

代码结构

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决Mybatis 大数据量的批量insert问题
下一篇:java中join方法的理解与说明详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~