首先是项目的结构图
pom.xml如下
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
//lombok为自动生成实体类get、set方法等,具体用法可百度
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
//引入阿里的数据源
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
resources配置:
用yml比较多,故使用的是yml来写配置文件
server:
port: 9000
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://```此处填入数据库地址和端口```/wechat_map?characterEncoding=utf-8
username: 填入用户名
password:填入密码
type: com.alibaba.druid.pool.DruidDataSource
# 初始连接数
initialSize: 50
# 最大连接数
maxActive: 200
# 最长等待时间
maxWait: 60000
# 定义最小空闲
minIdle: 20
# 每隔60秒运行一次空闲连接回收器
timeBetweenEvictionRunsMillis: 60000
# 连接池中的空闲连接5分钟后被回收
minEvictableIdleTimeMillis: 300000
# 验证使用的sql语句
validationQuery: SELECT 1 FROM DUAL
# 如果连接被空闲连接回收器进行检验,如果检验失败,连接则被从池中去除
testWhileIdle: true
# 借出连接时不测试 true为测试(据百度说法影响性能)
testOnBorrow: false
# 猜测大概是是否返回测试结果或者重复测试等(为了性能不开启)
testOnReturn: false
# 缓存游标,优化查询效率
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
# 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
# 监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
filters: stat,wall,log4j
# 当大于0时,poolPreparedStatements自动触发修改为true。
# 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
maxPoolPreparedStatementPerConnectionSize: 20
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: true
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
# 根据与源表联接的结果,对目标表执行插入、更新或删除操作。例如,根据在另一个表中找到的差异在一个表中插入、更新或删除行,
# 可以对两个表进行同步。”,通过这个描述,我们可以看出Merge是关于对于两个表之间的数据进行操作的
# (我理解为主外键表之间的数据同步)
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
mapper-locations: classpath:mapper/*.xml
# 自动开启驼峰转换
configuration:
map-underscore-to-camel-case: true
**以下为示例 **
首先是实体类bean
package com.example.wechatmap.bean;
import lombok.Data;
/**
* @author wj
* @version 1.0
* @className backmarkers
* @description 4个标记点
* @date 2019/8/6 13:14
*/
@Data
public class markers {
private String id;
private Float longitude;
private Float latitude;
private Integer type;
private String personname;
}
Dao层
package com.example.wechatmap.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import com.example.wechatmap.bean.markers;
@Mapper
public interface markersDao {
int insert(@Param("pojo") markers pojo);
int insertSelective(@Param("pojo") markers pojo);
int insertList(@Param("pojos") List<markers> pojo);
int update(@Param("pojo") markers pojo);
List<markers> selectAll();
markers selectbyId(String id);
}
mapper层
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.wechatmap.dao.markersDao">
<!--auto generated Code-->
<resultMap id="AllColumnMap" type="com.example.wechatmap.bean.markers">
<result column="id" property="id"/>
<result column="longitude" property="longitude"/>
<result column="latitude" property="latitude"/>
<result column="type" property="type"/>
<result column="personname" property="personname"/>
</resultMap>
<!--auto generated Code-->
<sql id="all_column">
`id`,
`longitude`,
`latitude`,
`type`,
`personname`
</sql>
<!--auto generated Code-->
<insert id="insert" useGeneratedKeys="true" keyProperty="pojo.id">
INSERT INTO markers (
`id`,
`longitude`,
`latitude`,
`type`,
`personname`
) VALUES (
#{pojo.id},
#{pojo.longitude},
#{pojo.latitude},
#{pojo.type},
#{pojo.personname}
)
</insert>
<!--auto generated Code-->
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="pojo.id">
INSERT INTO markers
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="pojo.id!=null"> `id`,</if>
<if test="pojo.longitude!=null"> `longitude`,</if>
<if test="pojo.latitude!=null"> `latitude`,</if>
<if test="pojo.type!=null"> `type`,</if>
<if test="pojo.personname!=null"> `personname`,</if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="pojo.id!=null">#{pojo.id},</if>
<if test="pojo.longitude!=null">#{pojo.longitude},</if>
<if test="pojo.latitude!=null">#{pojo.latitude},</if>
<if test="pojo.type!=null">#{pojo.type},</if>
<if test="pojo.personname!=null">#{pojo.personname},</if>
</trim>
</insert>
<!--auto generated Code-->
<insert id="insertList">
INSERT INTO markers (
<include refid="all_column"/>
)VALUES
<foreach collection="pojos" item="pojo" index="index" separator=",">
(
#{pojo.id},
#{pojo.longitude},
#{pojo.latitude},
#{pojo.type},
#{pojo.personname}
)
</foreach>
</insert>
<!--auto generated Code-->
<update id="update">
UPDATE markers
<set>
<if test="pojo.id != null"> `id` = #{pojo.id}, </if>
<if test="pojo.longitude != null"> `longitude` = #{pojo.longitude}, </if>
<if test="pojo.latitude != null"> `latitude` = #{pojo.latitude}, </if>
<if test="pojo.type != null"> `type` = #{pojo.type}, </if>
<if test="pojo.personname != null"> `personname` = #{pojo.personname} </if>
</set>
WHERE id = #{pojo.id}
</update>
<select id="selectAll" resultType="com.example.wechatmap.bean.markers">
select * from markers
</select>
<select id="selectbyId" resultType="com.example.wechatmap.bean.markers">
select * from markers where id=#{id}
</select>
</mapper>
services层
package com.example.wechatmap.service;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.example.wechatmap.bean.markers;
import com.example.wechatmap.dao.markersDao;
@Service
public class markersService{
@Resource
private markersDao markersDao;
public int insert(markers pojo){
return markersDao.insert(pojo);
}
public int insertSelective(markers pojo){
return markersDao.insertSelective(pojo);
}
public int insertList(List<markers> pojos){
return markersDao.insertList(pojos);
}
public int update(markers pojo){
return markersDao.update(pojo);
}
public List<markers> getAllMarkers(){
return markersDao.selectAll();
}
public markers getbyId(String id){
return markersDao.selectbyId(id);
}
}
Controller层
package com.example.wechatmap.controller;
import com.example.wechatmap.bean.markers;
import com.example.wechatmap.service.markersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @author wj
* @version 1.0
* @className markersController
* @description 标记点的返回层
* @date 2019/8/6 13:33
*/
@RestController
@CrossOrigin
@RequestMapping("/markers")
public class markersController {
@Autowired
private markersService markersservice;
/**
* 查询所有的标记点
* @return 标记点
*/
@RequestMapping("/getAllMarkers")
public List<markers> getAllMarkers(){
return markersservice.getAllMarkers();
}
/**
*根据marker的Id查询出所有信息
* @return
*/
@RequestMapping("/getchoosemarker")
public markers getchoosemarker(String id){
return markersservice.getbyId(id);
}
}
以上为具体示例,如果有问题可加qq:1016400304