1 完成工程搭建及项目结构
需要添加的依赖:
- Spring Boot DevTools:热部署,修改内容,工程自动重启
Lombok:打日志、注解工具,调试代码用
- Spring Configuration Processor:引入配置的工具
Spring Web Starter:web工程启动工具
- Thymeleaf:整合前端用的
JDBC API:连数据库的引擎
- Mybatis Framwork:Mybatis框架
PostgreSQL Driver:pg库引擎
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--devtools-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--postgresql-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
<!-- <scope>runtime</scope>-->
</dependency>
<!--configuration-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<optional>true</optional>
</dependency>新增的一些依赖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!--解析json的-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!-- 解析几何geometry对象用的-->
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version>
</dependency>完善工程结构
java.com.cc:
- controller
- MapController
- dao
- MapDao
- model
- PointModel
- service
- impl
- MapServiceImpl
- MapService
- impl
resource:
- mapper
- MapwebMapper.xml
- application.yml
- controller
package(包)的命名都需要小写;接口和类的命名要符号驼峰规则
点击小齿轮,取消勾选Compack Middle Packages,勾选上的话,会把空的package给折叠了
2 连接数据库
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37server:
port: 8080
# 数据库配置
spring:
main:
allow-circular-references: true
datasource:
initialize: flase
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: 123456
# alibaba数据连接池
type: com.alibaba.druid.pool.DruidDataSource
# postgresql驱动
driver-class-name: org.postgresql.Driver
druid:
# 初始化连接大小
initial-size: 8
# 最小空闲连接数
min-idle: 5
# 最大连接数
max-active: 10
# 查询超时时间
query-timeout: 6000
# 事务查询超时时间
transaction-query-timeout: 6000
# 关闭空闲连接超时时间
remove-abandoned-timeout: 1800
filter: stat,config
mybatis:
# sql映射文件
mapper-locations: classpath:mapper/*.xml
# 定义别名包
type-aliases-package: com.cc.model
3 增删改查
3.1 DAO(Mapper)层
MapDao
1
2
3
4
5
6
7
public interface MapDao {
List<PointModel> getCntyPoint(; Integer gId)
int insertCntyPoint(PointModel pointModel);
int updateCntyPoint(PointModel pointModel);
int deleteCntyPoint(; Integer gId)
}
3.2 Mapper配置
MapwebMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<mapper namespace="com.cc.dao.MapDao" >
<resultMap id="pointModelResult" type="com.cc.model.PointModel">
<result property="gId" column="gid" jdbcType="BIGINT"/>
<result property="nameCh" column="name_ch" jdbcType="VARCHAR"/>
</resultMap>
<sql id="BASE_TABLE">
v6_time_cnty_pts_utf_wgs84
</sql>
<sql id="BASE_COLUMN">
gid,name_ch
</sql>
<!--查询-->
<select id="getCntyPoint" resultMap="pointModelResult">
SELECT
<include refid="BASE_COLUMN"></include>
FROM
<include refid="BASE_TABLE"/>
WHERE gid=#{gId}
</select>
<!--插入-->
<insert id="insertCntyPoint" parameterType="com.cc.model.PointModel">
INSERT INTO
<include refid="BASE_TABLE"/>
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="gId != null">
gid,
</if>
<if test="nameCh != null">
name_ch,
</if>
</trim>
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
<if test="gId != null">
#{gId, jdbcType=BIGINT},
</if>
<if test="nameCh != null">
#{nameCh, jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!--更新-->
<update id="updateCntyPoint" parameterType="com.cc.model.PointModel">
UPDATE
<include refid="BASE_TABLE"/>
SET
name_ch=#{nameCh}
WHERE
gid=#{gId}
</update>
<!--删除-->
<delete id="deleteCntyPoint" parameterType="com.cc.model.PointModel">
DELETE FROM
<include refid="BASE_TABLE"/>
WHERE
gid=#{gId}
</delete>
</mapper>application.yml
1
2
3mybatis:
# sql映射文件
mapper-locations: classpath:mapper/*.xml
3.3 Service层
接口——MapService
1
2
3
4
5
6public interface MapService {
List<PointModel> getCntyPointByGid(Integer gId);
int addCntyPoint(PointModel pointModel);
int modifyCntyPoint(PointModel pointModel);
int removeCntyPoint(Integer gId);
}实现类——MapServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MapServiceImpl implements MapService {
private MapDao mapDao;
public List<PointModel> getCntyPointByGid(Integer gId){
return mapDao.getCntyPoint(gId);
}
public int addCntyPoint(PointModel pointModel){
return mapDao.insertCntyPoint(pointModel);
}
public int modifyCntyPoint(PointModel pointModel){
return mapDao.updateCntyPoint(pointModel);
}
public int removeCntyPoint(Integer gId){
return mapDao.deleteCntyPoint(gId);
}
}
3.4 Controller层
MapController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class MapController {
private MapService mapService;
public PointModel getPoint({ Integer gId)
return mapService.getCntyPointByGid(gId).get(0);
}
public int addPoint(PointModel pointModel){
return mapService.addCntyPoint(pointModel);
}
public int update(PointModel pointModel){
return mapService.modifyCntyPoint(pointModel);
}
public int removetPoint({ Integer gId)
return mapService.removeCntyPoint(gId);
}
}
3.5 测试
可以用工具测试,也可以用浏览器直接访问,测试工具Postman用得较多,但是现在好像不能直接在浏览器中使用,但有一个Postwoman 扩展也差不多!
查(直接访问)
1
http://localhost:8080/history/pointmodel?gid=1
增(POST请求)
1
http://localhost:8080/history/add?gid=14354&nameCh=test
改(POST请求)
1
http://localhost:8080/history/modify?gid=14354&nameCh=test01
删(GET请求)
1
http://localhost:8080/history/remove?gid=1435
3.6 上传代码至GitHub
最简便的方法就是,可以直接通过IDEA上传代码至GitHub仓库。“VCS”==>”Share Project on GitHub”
4 报错
报错1:
循环依赖问题:
1
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
解决办法:在yml配置文件中新增以下语句
1
2
3spring:
main:
allow-circular-references: true
报错2:
Invalid bound statement——无效绑定,是mybatis中常见的一个异常。
1
Invalid bound statement (not found): com.cc.dao.MapDao.getCntyPoint] with root cause
问题实质:mapper接口(dao层)和xml没有绑定成功,也叫做与xml文件没有映射!
解决办法:在yml配置文件中,发现映射xml的路径打错了,多打了一个s
一些Tips:
接口与xml的绑定原理:mapper接口并没有实现类,所以框架会通过JDK动态代理模式获取接口的代理实现类,进而根据接口全限定类名+id去一一绑定xml中的sql
mapper文件要和.xml的目录结构一样
可以通过target目录查看mapper是否成功加载
三步教你解决Invalid bound statement (not found): com.xxx.dao.xxxDao.selectByxx错误!!!!很简单_倩女有文的博客-CSDN博客