SpringBoot連接PostgreSQL+MybatisPlus入門(mén)案例(代碼詳解)
項(xiàng)目結(jié)構(gòu)
一、Java代碼
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> <groupId>org.example</groupId> <artifactId>jdservice</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.6.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <!--mybatis‐plus的springboot支持--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency> </dependencies> </project>
controller層
package org.example.jd.controller; import org.example.jd.pojo.TUser; import org.example.jd.service.TUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @CrossOrigin(origins = "http://localhost:8080") @RestController public class UserController { @Autowired private TUserService tUserService; @PostMapping("/api/userLogin") public String login(@RequestBody TUser tUser) { // 實(shí)際業(yè)務(wù)邏輯處理 String username = tUser.getUsername(); String password = tUser.getPassword(); // 這里可以進(jìn)行用戶(hù)名密碼驗(yàn)證等操作 System.out.println("========Login successful====="); System.out.println(username + "," + password); return "Login successful"; // 返回登錄成功信息或者其他響應(yīng) } @GetMapping("/api/getUserList") public List<TUser> getUserList() { List<TUser> tUserList = tUserService.getUserList(); return tUserList; } }
mapper層
package org.example.jd.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.example.jd.pojo.TUser; public interface TUserMapper extends BaseMapper<TUser> { //參數(shù)為實(shí)體類(lèi) }
pojo層
package org.example.jd.pojo; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDate; @Data @AllArgsConstructor @NoArgsConstructor @TableName("tuser") //指定數(shù)據(jù)庫(kù)表 public class TUser { @TableId(value = "uid") //指定主鍵 private int uid; private String username; private String password; private int age; private String gender; private LocalDate birthday; private String address; }
service層
TUserService
package org.example.jd.service; import org.example.jd.pojo.TUser; import java.util.List; public interface TUserService { List<TUser> getUserList(); }
TUserServiceImp
package org.example.jd.service; import org.example.jd.mapper.TUserMapper; import org.example.jd.pojo.TUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class TUserServiceImp implements TUserService { @Autowired private TUserMapper tUserMapper; @Override public List<TUser> getUserList() { return tUserMapper.selectList(null); } }
Application啟動(dòng)類(lèi)
package org.example.jd; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("org.example.jd.mapper") //掃描mapper層 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
TUserMapper.xml
<?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"> <!--namespace是mapper層對(duì)應(yīng)的接口名--> <mapper namespace="org.example.jd.mapper.TUserMapper"> <!--id是mapper層對(duì)應(yīng)的接口名中對(duì)應(yīng)的方法名--> <!-- <select id="myFindUserById" resultType="User">--> <!-- select *--> <!-- from tb_user--> <!-- where id = #{id}--> <!-- </select>--> </mapper>
mybatis-config.xml
<?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> <!--設(shè)置駝峰匹配,MybatisPlus默認(rèn)開(kāi)啟駝峰匹配--> <!-- <settings>--> <!-- <setting name="mapUnderscoreToCamelCase" value="true"/>--> <!-- </settings>--> </configuration>
application.yml配置文件
server: port: 8090 spring: datasource: url: jdbc:postgresql://localhost:5432/myjd username: postgres password: 123456 driver-class-name: org.postgresql.Driver #設(shè)置mybatis-plus mybatis-plus: config-location: classpath:mybatis/mybatis-config.xml #配置文件 mapper-locations: classpath:mybatis/mapper/*.xml #設(shè)置mapper層對(duì)應(yīng)的接口對(duì)應(yīng)的mapper.xml的路徑 type-aliases-package: org.example.jd.pojo #設(shè)置別名,mapper.xml中resultType="org.example.jd.pojo.TUser"可省去包,即resultType="TUser" #開(kāi)啟自動(dòng)駝峰映射,注意:配置configuration.map-underscore-to-camel-case則不能配置config-location,可寫(xiě)到mybatis-config.xml中 # configuration: # map-underscore-to-camel-case: true
二、SQL
/* Navicat Premium Data Transfer Source Server : myPgSQL Source Server Type : PostgreSQL Source Server Version : 160003 (160003) Source Host : localhost:5432 Source Catalog : myjd Source Schema : public Target Server Type : PostgreSQL Target Server Version : 160003 (160003) File Encoding : 65001 Date: 19/07/2024 22:15:18 */ -- ---------------------------- -- Table structure for tuser -- ---------------------------- DROP TABLE IF EXISTS "public"."tuser"; CREATE TABLE "public"."tuser" ( "uid" int4 NOT NULL, "username" varchar(255) COLLATE "pg_catalog"."default", "age" int4, "gender" varchar(1) COLLATE "pg_catalog"."default", "birthday" date, "address" varchar(255) COLLATE "pg_catalog"."default", "password" varchar(255) COLLATE "pg_catalog"."default" ) ; -- ---------------------------- -- Records of tuser -- ---------------------------- INSERT INTO "public"."tuser" VALUES (1, 'jack', 24, '男', '2021-10-19', '深圳', '123'); -- ---------------------------- -- Primary Key structure for table tuser -- ---------------------------- ALTER TABLE "public"."tuser" ADD CONSTRAINT "user_pkey" PRIMARY KEY ("uid");
三、運(yùn)行
瀏覽器或者postman請(qǐng)求地址。
注意:瀏覽器只能測(cè)試get請(qǐng)求,如果有put、post、delete請(qǐng)使用postman測(cè)試。
http://localhost:8090/api/getUserList
到此這篇關(guān)于SpringBoot連接PostgreSQL+MybatisPlus入門(mén)案例的文章就介紹到這了,更多相關(guān)SpringBoot連接PostgreSQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Flink CDC實(shí)現(xiàn)實(shí)時(shí)追蹤mysql數(shù)據(jù)變動(dòng)
- Springboot整合Mybatis和SQLite的詳細(xì)過(guò)程
- SpringBoot項(xiàng)目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫(kù)的過(guò)程
- SpringBoot整合Druid實(shí)現(xiàn)SQL監(jiān)控和數(shù)據(jù)庫(kù)密碼加密
- SpringBoot集成Druid監(jiān)控慢SQL的詳細(xì)過(guò)程
- Springboot中分析SQL性能的兩種方式詳解
相關(guān)文章
在SpringBoot 中從application.yml中獲取自定義常量方式
這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04詳解SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源
這篇文章主要為大家介紹了SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源實(shí)現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09spring boot 2.x html中引用css和js失效問(wèn)題及解決方法
這篇文章主要介紹了spring boot 2.x html中引用css和js失效,需要的朋友可以參考下2018-11-11eclipse中自動(dòng)生成javadoc文檔的方法
這篇文章主要介紹了eclipse中自動(dòng)生成javadoc文檔的方法,是實(shí)用eclipse開(kāi)發(fā)Java程序時(shí)非常實(shí)用的技巧,對(duì)于進(jìn)行Java項(xiàng)目開(kāi)發(fā)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12