mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實(shí)現(xiàn)
1.pom文件寫(xiě)法
pom文件需要寫(xiě)入springboot依賴、mybatis依賴和postgresql依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<!-- postgresql依賴 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.1</version>
</dependency>
</dependencies>問(wèn)題:Invalid bound statement (not found)
問(wèn)題實(shí)質(zhì)就是mapper接口和mapper.xml文件沒(méi)有映射起來(lái)。
常見(jiàn)的錯(cuò)誤如下:
1.mapper.xml中的namespace和實(shí)際的mapper文件不一致
這個(gè)問(wèn)題其實(shí)很好解決,瞪大眼睛,仔仔細(xì)細(xì)看看,到底對(duì)不對(duì)應(yīng)不就好了嘛
2.mapper接口中的方法名和mapper.xml中的id標(biāo)簽不一致
這個(gè)問(wèn)題和上個(gè)問(wèn)題解決方法一樣,仔細(xì)對(duì)對(duì)嘛,這個(gè)再對(duì)不出來(lái),面壁思過(guò)吧。
3.上兩步的問(wèn)題都沒(méi)有,但是還是不行,可能原因就是,沒(méi)有構(gòu)建進(jìn)去,打開(kāi)target看看對(duì)應(yīng)的mapper.xml文件在不在

如果沒(méi)有構(gòu)建dao層里的xml文件,則需要在pom文件的build節(jié)點(diǎn)里寫(xiě)入下列依賴:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>接著clean一下,maven項(xiàng)目,然后再啟動(dòng),這樣可以實(shí)現(xiàn)構(gòu)建時(shí)將.xml文件構(gòu)建到target文件夾里面
2.application.yml和mybatis-config.xml
application.yml 是 Spring Boot 應(yīng)用程序的配置文件,用于配置 Spring Boot 應(yīng)用程序的各種屬性和特性。它包含了應(yīng)用程序的所有配置信息,例如數(shù)據(jù)庫(kù)連接信息、日志配置、緩存配置、端口號(hào)等
server:
#端口號(hào)
port: 8088
#項(xiàng)目名,如果不設(shè)定,默認(rèn)是 /
spring:
datasource:
url: jdbc:postgresql://192.168.10.12:5432/wuxue_argculture
username: postgres
password: postgres
driver-class-name: org.postgresql.Driver
logging:
level:
com.demo.mapper: debug
mybatis:
#標(biāo)注mybatis配置文件的位置
config-location: classpath:mybatis-config.xml
#標(biāo)注待解析的mapper的xml文件位置
mapper-locations: classpath:demo/dao/*.xml
#標(biāo)注實(shí)體類位置
type-aliases-package: demo.entitymybatis-config.xml 是 MyBatis 的配置文件,用于配置 MyBatis 的全局屬性、類型別名、映射器等。它是 MyBatis 框架的核心配置文件,必須存在并且必須正確配置,否則 MyBatis 將無(wú)法正常工作。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- #開(kāi)啟mybatis駝峰式命名規(guī)則自動(dòng)轉(zhuǎn)換 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<!-- 類型別名配置 -->
<typeAliases>
<typeAlias type="demo.entity.StandardYard" alias="StandardYard"/>
<!-- 添加其他類型別名 -->
</typeAliases>
</configuration><setting name="mapUnderscoreToCamelCase" value="true" /> 是用來(lái)開(kāi)啟 MyBatis 的駝峰式命名規(guī)則自動(dòng)轉(zhuǎn)換功能。在數(shù)據(jù)庫(kù)中,有些表或者列使用下劃線(例如 first_name)來(lái)命名,而在 Java 中更傾向于使用駝峰式命名(例如 firstName)。通過(guò)設(shè)置 mapUnderscoreToCamelCase 為 true,MyBatis 將會(huì)自動(dòng)將數(shù)據(jù)庫(kù)中帶下劃線的命名規(guī)則轉(zhuǎn)換為駝峰式命名規(guī)則,這樣在編寫(xiě) SQL 映射文件時(shí)就可以直接使用駝峰式的命名方式,而不必?fù)?dān)心與數(shù)據(jù)庫(kù)命名不一致的問(wèn)題
通過(guò)XML的方式配置Mybatis
在 /src/main/resource下創(chuàng)建Mybatis配置文件 mybatis-config.xml 和 映射文件目錄mapper
在application.yml指定Mybatis配置文件、映射文件的位置
- mybatis.config-location:配置 mybatis-config.xml 路徑mybatis-config.xml 中配置 MyBatis 基礎(chǔ)屬性
- mybatis.mapper-locations:配置 Mapper 對(duì)應(yīng)的 XML 文件路徑
- mybatis.type-aliases-package:配置項(xiàng)目中實(shí)體類包路徑
注意:
可以在appllication.yml中直接配置Mybatis,不通過(guò)mybatis-config.xml
##指定mybatis輸出日志的位置, 輸出控制臺(tái) #mybatis: # configuration: # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
兩種配置方式只能二選一,不能同時(shí)使用application.yml中的configuration和mybatis-config.xml文件配置mybabis
即: application.yml中configuration 和 configLocation 兩個(gè)屬性不能同時(shí)存在,否則會(huì)報(bào)錯(cuò)
3.Mapper層xml寫(xiě)法
在 MyBatis 的 Mapper 層 XML 文件中,通常定義了 SQL 映射語(yǔ)句和與之相關(guān)的一些參數(shù)。下面是幾個(gè)常見(jiàn)的參數(shù)及其含義:
- namespace:用于指定該 XML 文件對(duì)應(yīng)的 Mapper 接口的完全限定名。通過(guò)設(shè)置 namespace,可以將 XML 文件與對(duì)應(yīng)的 Java 接口關(guān)聯(lián)起來(lái)
- resultMap:指定結(jié)果集映射關(guān)系的標(biāo)識(shí)符,用于將數(shù)據(jù)庫(kù)查詢結(jié)果映射到 Java 對(duì)象??梢允且讯x的 的 id,或者是內(nèi)聯(lián)的結(jié)果映射定義。這里可以創(chuàng)造字段映射,實(shí)現(xiàn)實(shí)體類屬性名稱和表字段的解耦
- resultType:指定單個(gè)結(jié)果對(duì)象的類型??梢允?Java 對(duì)象的完全限定名(例如:com.example.User),也可以是基本數(shù)據(jù)類型。
<?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="demo.dao.StandardYardMapper">
<!-- 定義 ResultMap -->
<resultMap id="standardYardResultMap" type="StandardYard">
<id property="id" column="id" />
<result property="townName" column="town_name" />
<result property="community" column="community" />
<result property="yardName" column="yard_name" />
<!-- 其他字段映射 -->
<result property="longitude" column="lon" />
<result property="latitude" column="lat" />
</resultMap>
<select id="getStandardYardById" resultMap="standardYardResultMap">
SELECT a.id AS standard_yard_id,a.*, b.lon, b.lat
FROM standard_yard_2022 a
LEFT JOIN village_random b ON a.community LIKE '%' || b.cjqymc || '%'
WHERE a.id = #{id}
</select>
<select id="getStandardYards" resultMap="standardYardResultMap">
SELECT a.*, b.lon, b.lat
FROM standard_yard_2022 a
LEFT JOIN village_random b ON a.community LIKE '%' || b.cjqymc || '%'
</select>
</mapper>注意:
LEFT JOIN 是 SQL 中的一種連接(Join)操作,它將兩個(gè)表中的記錄按照指定的條件聯(lián)接在一起。
具體來(lái)說(shuō),LEFT JOIN 會(huì)返回左側(cè)表(即第一個(gè)表)的所有記錄,以及與右側(cè)表(即第二個(gè)表)滿足連接條件的記錄。如果右側(cè)表中沒(méi)有與左側(cè)表匹配的記錄,那么對(duì)應(yīng)的結(jié)果列會(huì)顯示為 NULL。
SELECT * FROM 表1 LEFT JOIN 表2 ON 表1.字段 = 表2.字段
在這個(gè)示例中,表1 和 表2 是要連接的兩個(gè)表名。表1.字段 和 表2.字段 是用于連接的列名,它們應(yīng)該具有相同的值。
LEFT JOIN 會(huì)根據(jù)連接條件將兩個(gè)表中的記錄進(jìn)行匹配,并將匹配成功的記錄組合在一起。如果在 表2 中找不到與 表1 匹配的記錄,那么對(duì)應(yīng)的結(jié)果列將會(huì)顯示為 NULL。
總結(jié)起來(lái),LEFT JOIN 可以保留左側(cè)表中的所有記錄,并將右側(cè)表中與之匹配的記錄進(jìn)行合并。這個(gè)操作可以幫助我們根據(jù)某些條件關(guān)聯(lián)和組合兩個(gè)表的數(shù)據(jù)。
4.Controller層寫(xiě)法
@RestController
@RequestMapping("/wuXue/StandardYard")
public class StandardYardController {
@Autowired
private StandardYardService standardYardService;
@GetMapping("/getList")
public Result list() {
//1. 調(diào)用service層, 獲取數(shù)據(jù)
List<StandardYard> StandardYardList = standardYardService.getStandardYards();
//3. 響應(yīng)數(shù)據(jù)
return Result.success(StandardYardList);
}
@GetMapping("/getById")
public Result getById(@RequestParam(value="id")int id) {
//1. 調(diào)用service層, 獲取數(shù)據(jù)
StandardYard standardYard = standardYardService.getStandardYardById(id);
//3. 響應(yīng)數(shù)據(jù)
return Result.success(standardYard);
}
}@RestController:這個(gè)注解是 Spring MVC 提供的,它表示該類是一個(gè)控制器,并且所有處理請(qǐng)求的方法都會(huì)返回 JSON/XML 或其他格式的響應(yīng)數(shù)據(jù)。相比于普通的 @Controller 注解,@RestController 注解還省略了在每個(gè)處理方法上添加 @ResponseBody 的步驟。
@RequestMapping("/wuXue/StandardYard"):這個(gè)注解用來(lái)映射請(qǐng)求路徑。在這個(gè)示例中,所有的請(qǐng)求路徑都以 "/wuXue/StandardYard" 開(kāi)頭。例如,"/getList" 請(qǐng)求會(huì)被映射到 list() 方法。
@Autowired:這個(gè)注解是 Spring Framework 提供的,它用于自動(dòng)裝配依賴關(guān)系。在這個(gè)示例中,private StandardYardService standardYardService 字段通過(guò)自動(dòng)裝配得到了一個(gè) StandardYardService 對(duì)象,使得可以直接調(diào)用其方法。
@GetMapping("/getList"):這個(gè)注解是 Spring MVC 提供的,它將 HTTP GET 請(qǐng)求映射到 list() 方法。當(dāng)客戶端發(fā)送一個(gè) GET 請(qǐng)求到 "/wuXue/StandardYard/getList" 路徑時(shí),該方法將被調(diào)用。
@RequestParam(value="id") int id:這個(gè)注解用于將請(qǐng)求參數(shù)綁定到方法參數(shù)。在這個(gè)示例中,請(qǐng)求參數(shù)名為 "id",它會(huì)被綁定到 int id 參數(shù)上。
到此這篇關(guān)于mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)mybatis+springboot發(fā)布postgresql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot3集成PostgreSQL的詳細(xì)過(guò)程
- SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù)
- SpringBoot整合PostgreSQL的示例代碼
- SpringBoot項(xiàng)目配置postgresql數(shù)據(jù)庫(kù)完整步驟(配置多數(shù)據(jù)源)
- springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置
- SpringBoot連接使用PostgreSql數(shù)據(jù)庫(kù)的方法
- Springboot中MyBatisplus使用IPage和Page分頁(yè)的實(shí)例代碼
- SpringBoot+MybatisPlus+代碼生成器整合示例
- springboot集成mybatisplus實(shí)例詳解
- SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
相關(guān)文章
java 多線程實(shí)現(xiàn)在線咨詢(udp)
這篇文章主要介紹了java 多線程實(shí)現(xiàn)在線咨詢(udp)的示例,幫助大家更好的理解和學(xué)習(xí)Java 網(wǎng)絡(luò)編程的相關(guān)內(nèi)容,感興趣的朋友可以了解下2020-11-11
SpringBoot 入門教程之引入數(shù)據(jù)傳輸層的方法
這篇文章主要介紹了SpringBoot 入門教程之引入數(shù)據(jù)傳輸層的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Spring Boot Admin監(jiān)控服務(wù)如何使用
這篇文章主要介紹了Spring Boot Admin監(jiān)控服務(wù)如何使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
springboot Actuator的指標(biāo)監(jiān)控可視化功能詳解
SpringBoot Actuator是springboot為簡(jiǎn)化我們對(duì)微服務(wù)項(xiàng)目的監(jiān)控功能抽取出來(lái)的模塊,使得我們每個(gè)微服務(wù)快速引用即可獲得生產(chǎn)界別的應(yīng)用監(jiān)控、審計(jì)等功能。這篇文章主要介紹了springboot Actuator的指標(biāo)監(jiān)控可視化,需要的朋友可以參考下2021-08-08
spring cloud zuul 與 sentinel的結(jié)合使用操作
這篇文章主要介紹了spring cloud zuul 與 sentinel 的結(jié)合使用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
JAVA使用HtmlUnit爬蟲(chóng)工具模擬登陸CSDN案例
今天小編就為大家分享一篇關(guān)于JAVA使用HtmlUnit爬蟲(chóng)工具模擬登陸CSDN案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Mybatis的特點(diǎn)及優(yōu)點(diǎn)
Mybatis 本是apache的一個(gè)開(kāi)源項(xiàng)目iBatis, 2010年這個(gè)項(xiàng)目由apache software foundation 遷移到了google code,并且改名為MyBatis。mybatis有哪些特點(diǎn)和優(yōu)點(diǎn)呢?通過(guò)本文一起學(xué)習(xí)吧2016-12-12

