網(wǎng)上不太好找詳細(xì)的mybatis整合postgresql數(shù)據(jù)庫搭建springboot2框架web工程的教程。所以以下我就一步一步詳解,寫一個細(xì)化" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運(yùn)營 > 從零開始,構(gòu)建電子地圖網(wǎng)站:0_7_mybatis+postgresql+springboot2

從零開始,構(gòu)建電子地圖網(wǎng)站:0_7_mybatis+postgresql+springboot2

時間:2023-05-16 17:06:02 | 來源:網(wǎng)站運(yùn)營

時間:2023-05-16 17:06:02 來源:網(wǎng)站運(yùn)營

從零開始,構(gòu)建電子地圖網(wǎng)站:0_7_mybatis+postgresql+springboot2:本例細(xì)化到每一步驟的操作。

網(wǎng)上不太好找詳細(xì)的mybatis整合postgresql數(shù)據(jù)庫搭建springboot2框架web工程的教程。所以以下我就一步一步詳解,寫一個細(xì)化的傻瓜教程。因為對于初學(xué)者來說,教程不夠細(xì)化,就沒有什么可復(fù)用性。

以下,照著一步一步操作,可以搭建出來一個web工程。

另外,寫教程不附贈源碼,就是耍流氓,最后有g(shù)it地址。




一、開發(fā)環(huán)境:

開發(fā)IDE:IntelliJ IDEA。

JDK:jdk1.8

框架:springboot2+mybatis

數(shù)據(jù)庫:postgresql

數(shù)據(jù):數(shù)據(jù)就采用之前上傳到postgresql庫中v6_time_cnty_pts_utf_wgs84表。







二、工程搭建

1. Create New Project

打開IDEA,Create New Project ?;蛘咭呀?jīng)是打開了一個工程的頁面,F(xiàn)ile——New——Project??傊陆üこ?。










2.初始化Spring

如圖,選擇Spring Initializr;

Project SDK選擇JDK(JAVA語言的軟件開發(fā)包);

保證網(wǎng)絡(luò)暢通,Next;







3.工程命名

給自己的工程起個名,Group一般以com.開頭,有點像域名,Artifact是工程的名稱,兩者都以英文命名,且不能有大寫字母,這是工程規(guī)范。

Type,默認(rèn)的就是Maven Project,Maven工程管理jar包很便捷。

Language是Java,不用說了。

Packaging是jar,打包編譯類型肯定是jar包了。

Java Version是8,JDK的版本是1.8。

Version,版本號是0.0.1-SNAPSHOT,快照版。

Name是根據(jù)Artifact生成的,兩個一樣。

Description是工程描述,可以描述一下。

Package是Group.Artifact,都是自動生成的,,不用管。

這步驟,填下Group和Artifact,next就可以。










4.配置maven依賴

這個步驟是在創(chuàng)建工程的時候,就把一些maven依賴(Dependencies)配置上。

如圖所示,點選Developer Tools,勾選Spring Boot DevTools、Lombok、Spring Configuration Processor。

依次把Web中的Spring Web Starter;Template Engines中的Thymeleaf;SQL中的JDBC API、Mybatis Framwork、PostgreSQL Driver都勾選上,點擊next。







稍微介紹下這些依賴都是干什么的。

Spring Boot DevTools:熱部署,修改內(nèi)容,工程自動重啟。

Lombok:打日志、注解工具,調(diào)試代碼用。

Spring Configuration Processor:引入配置的工具。

這三個都是方便寫代碼的。




Spring Web Starter:web工程啟動工具。

Thymeleaf:整合前端用的。




JDBC API:連數(shù)據(jù)庫的引擎。

Mybatis Framwork:Mybatis框架。

PostgreSQL Driver:pg庫引擎。




先加這些依賴,不夠用的話,以后在配置文件中,也非常好加。




5.保存工程

Project name就是Artifact的名稱,這個不用改,給工程找個地方放,Project location放工程的文件夾,確保這個文件夾名跟Project name一樣。點擊finish。










6.靜待下載依賴

保持網(wǎng)絡(luò)暢通,下載依賴還需要幾分鐘,看右下角的進(jìn)度條就可以了。

當(dāng)提示,Plugins Suggestion,選擇Enable plugins。

當(dāng)提示,Maven projects need to be imported,選擇Enable Auto-Import,如果之前已經(jīng)配置了maven的自動導(dǎo)入,這步就不會提示了。










三、完善工程結(jié)構(gòu)

創(chuàng)建完的工程長這個樣子。










我們按照規(guī)范給它完善一下。

需要添加controller層、dao層、model層、service層(內(nèi)有Impl層)。

在com.history.gismap上右鍵——new——package,依次新建package,注意層級關(guān)系。







Package建立完,長這樣。

注意層級關(guān)系,impl是service下的package,package的命名都需要小寫。

點擊小齒輪,取消勾選Compack Middle Packages,勾選上的話,會把空的package給折疊了。










創(chuàng)建完package之后,在各個層(package)上,右鍵——new——Java Class,新建類。

創(chuàng)建完長這樣,接口(interface)和類(class)命名要符合駝峰規(guī)則。







其中MapService是接口(interface),MapServiceImpl是它的實現(xiàn)類(class)。

MapService長這樣。

package com.history.gismap.service;

public interface MapService {
}

MapServiceImpl長這樣,繼承(implements)了Map Service。

package com.history.gismap.service.impl;

import com.history.gismap.service.MapService;

public class MapServiceImpl implements MapService {
}







四、完善依賴

把pom.xml打開,缺依賴加依賴,缺配置加配置。

主要改了properties,標(biāo)注一下編碼為utf8。

和dependencies,把缺的一些依賴加上了。

把postgresql的版本號<version>42.2.2</version>加上,注釋掉<!--<scope>runtime</scope>-->,因為我不只是運(yùn)行的時候用這個jar包,我測試的時候也要用。

加上lombok的版本號<version>1.18.8</version>。

加上apache的commons-lang3的jar包,解析json的包,分頁插件pagehelper-spring-boot-starter,數(shù)據(jù)庫連接池druid-spring-boot-starter、解析幾何geometry對象用的jts包。

現(xiàn)在pom文件長這樣,文件路徑:D:/gismap/java/gismap/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.history</groupId>
<artifactId>gismap</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gismap</name>
<description>Demo project for Spring Boot</description>

<properties>
<!-- 標(biāo)注一下編碼為utf8,jdk版本為1.8-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
<!-- <scope>runtime</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<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數(shù)據(jù)庫連接池 -->
<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>



</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

保持網(wǎng)絡(luò)暢通,靜待maven依賴下載。

查看maven的地方,Project——External Libraries,Maven——Dependencies。
















五、連接數(shù)據(jù)庫

把D:/gismap/java/gismap/src/main/resources/application.properties刪掉,properties有時候連接不上datasource。

新建D:/gismap/java/gismap/src/main/resources/application.yml文件。




server:
port: 8080

#數(shù)據(jù)庫配置
spring:
datasource:
#alibaba數(shù)據(jù)連接池
type: com.alibaba.druid.pool.DruidDataSource
#postgresql驅(qū)動
driverClassName: org.postgresql.Driver
#數(shù)據(jù)庫地址、賬號、密碼
url: jdbc:postgresql://127.0.0.1:5432/postgres
username: postgres
password: 123456
druid:
#初始化連接大小
initial-size: 8
#最小空閑連接數(shù)
min-idle: 5
#最杭州接數(shù)
max-active: 10
#查詢超時時間
query-timeout: 6000
#事務(wù)查詢超時時間
transaction-query-timeout: 6000
#關(guān)閉空閑連接超時時間
remove-abandoned-timeout: 1800
filters: stat,config

mybatis:
#sql映射文件
mapper-locations: classpath:mapper/*.xml
#model
type-aliases-package: com.history.gismap.model




其中數(shù)據(jù)庫url、username、password根據(jù)自己的寫。

Mybatis是映射配置,sql語句都寫在xml文件中。

在application.yml的同路徑下,新建一個文件夾mapper,右鍵——New——Directory,D:/gismap/java/gismap/src/main/resources/mapper

在mapper下新建一個文件HistoryGISMapper.xml,右鍵——New——File,D:/gismap/java/gismap/src/main/resources/mapper/HistoryGISMapper.xml

如下所示。













六、完善代碼

開始逐一完善代碼。

1.model類

首先是PointModel,先加兩個參數(shù),與數(shù)據(jù)庫對應(yīng)上,先把流程跑通,springboot要善用注解,@Getter和@Setter,就不用寫getter和setter了。

D:/gismap/java/gismap/src/main/java/com/history/gismap/model/PointModel.java

package com.history.gismap.model;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class PointModel {
private Integer gId;
private String nameCh;
}




2.Dao類

注意把class改成了interface,加了@Service注釋,只新建了一個getCntyPoint的方法,跑下流程,加@Param注釋,注釋里的gId是sql中的#{gId}。

D:/gismap/java/gismap/src/main/java/com/history/gismap/dao/MapDao.java

package com.history.gismap.dao;

import com.history.gismap.model.PointModel;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface MapDao {
List<PointModel> getCntyPoint(@Param("gId") Integer gId);
}




3.mapper配置

注意mapper 的namespace是dao,查詢語句中#{gId}的內(nèi)容是dao中方法@Param("gId")注釋的內(nèi)容,保持兩者一致。




<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//http://mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.history.gismap.dao.MapDao" >
<resultMap id="pointModelResult" type="com.history.gismap.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>
</mapper>




4.service類

接口

D:/gismap/java/gismap/src/main/java/com/history/gismap/service/MapService.java

package com.history.gismap.service;

import com.history.gismap.model.PointModel;

import java.util.List;

public interface MapService {
List<PointModel> getCntyPointByGid(Integer gId);
}

實現(xiàn)類,注意注釋@Service

D:/gismap/java/gismap/src/main/java/com/history/gismap/service/impl/MapServiceImpl.java




package com.history.gismap.service.impl;

import com.history.gismap.dao.MapDao;
import com.history.gismap.model.PointModel;
import com.history.gismap.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@Service

public class MapServiceImpl implements MapService {
@Autowired
private MapDao mapDao;
@Override
public List<PointModel> getCntyPointByGid(Integer gId){
return mapDao.getCntyPoint(gId);
}
}




5. Controller類

獲取一條測試下。

D:/gismap/java/gismap/src/main/java/com/history/gismap/controller/MapController.java

package com.history.gismap.controller;

import com.history.gismap.model.PointModel;
import com.history.gismap.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/history")
public class MapController {
@Autowired
private MapService mapService;
@ResponseBody
@GetMapping("/pointmodel")
public PointModel getPoint(@RequestParam("gid") Integer gId){
return mapService.getCntyPointByGid(gId).get(0);
}
}




6.啟動類

注意加上@MapperScan("com.history.gismap.dao"),啟動的時候掃描dao包,否則服務(wù)沒法加載。啟動的時候,運(yùn)行它就行。

D:/gismap/java/gismap/src/main/java/com/history/gismap/GismapApplication.java




package com.history.gismap;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.history.gismap.dao")

public class GismapApplication {

public static void main(String[] args) {
SpringApplication.run(GismapApplication.class, args);
}

}







7.測試一下

用IDEA自帶的工具測就行,Tools——HTTP Client——Test RESTful Web Service。




HTTP method選get,Host/port和Path照著寫就行。點擊運(yùn)行下,就出現(xiàn)了結(jié)果,{"nameCh":"保德州","gid":1}。

大功告成。










七、增刪改查

說寫代碼就是增刪改查,

那就把增刪改查都搞一下吧。




1.dao類

package com.history.gismap.dao;

import com.history.gismap.model.PointModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public interface MapDao {
List<PointModel> getCntyPoint(@Param("gId") Integer gId);
int insertCntyPoint(PointModel pointModel);
int updateCntyPoint(PointModel pointModel);
int deleteCntyPoint(@Param("gId") Integer gId);
}










2.mapper配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//http://mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.history.gismap.dao.MapDao" >
<resultMap id="pointModelResult" type="com.history.gismap.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.history.gismap.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.history.gismap.model.PointModel">
UPDATE
<include refid="BASE_TABLE"/>
SET
name_ch=#{nameCh}
WHERE
gid=#{gId}
</update>
<delete id="deleteCntyPoint" parameterType="com.history.gismap.model.PointModel">
DELETE FROM
<include refid="BASE_TABLE"/>
WHERE
gid=#{gId}
</delete>
</mapper>







3.service類

package com.history.gismap.service;

import com.history.gismap.model.PointModel;
import org.springframework.stereotype.Service;

import java.util.List;
public interface MapService {
List<PointModel> getCntyPointByGid(Integer gId);
int addCntyPoint(PointModel pointModel);
int modifyCntyPoint(PointModel pointModel);
int removeCntyPoint(Integer gId);

}







impl




package com.history.gismap.service.impl;

import com.history.gismap.dao.MapDao;
import com.history.gismap.model.PointModel;
import com.history.gismap.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MapServiceImpl implements MapService {
@Autowired
private MapDao mapDao;
@Override
public List<PointModel> getCntyPointByGid(Integer gId){
return mapDao.getCntyPoint(gId);
}
@Override
public int addCntyPoint(PointModel pointModel){
return mapDao.insertCntyPoint(pointModel);
}
@Override
public int modifyCntyPoint(PointModel pointModel){
return mapDao.updateCntyPoint(pointModel);
}
@Override
public int removeCntyPoint(Integer gId){
return mapDao.deleteCntyPoint(gId);
}
}




4.Controller類




package com.history.gismap.controller;

import com.history.gismap.model.PointModel;
import com.history.gismap.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(value = "/history")
public class MapController {
@Autowired
private MapService mapService;
@ResponseBody
@GetMapping("/pointmodel")
public PointModel getPoint(@RequestParam("gid") Integer gId){
return mapService.getCntyPointByGid(gId).get(0);
}
@ResponseBody
@PostMapping("/add")
public int addPoint(PointModel pointModel){
return mapService.addCntyPoint(pointModel);
}
@ResponseBody
@PostMapping("/modify")
public int update(PointModel pointModel){
return mapService.modifyCntyPoint(pointModel);
}
@ResponseBody
@GetMapping("/remove")
public int removetPoint(@RequestParam("gid") Integer gId){
return mapService.removeCntyPoint(gId);
}
}







5.測試一下

運(yùn)行GismapApplication。













查:

http://localhost:8080/history/pointmodel?gid=1

瀏覽器訪問這個網(wǎng)頁就行。

增:

用postman,post請求,寫好params后,點擊send。










改:















刪:就把我測試這條臟數(shù)據(jù)刪掉。




http://localhost:8080/history/remove?gid=14353




八、上傳代碼




不留源碼都是耍流氓。




源碼地址:https://github.com/yimengyao13/gismap.git

直接在IDEA中下下來看,F(xiàn)ile——New——Project from Version Controll——Git







填好url,選好Directory(文件夾名稱gismap與工程名稱gismap保持一致),點擊clone下載就成了。













接下來還有g(shù)eometry幾何對象的增刪改查,這個就需要mybatis自定義類型了。下篇繼續(xù)。







關(guān)鍵詞:地圖,電子

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉