SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
更新時間:2024年07月24日 14:28:37 作者:小天博客
這篇文章主要介紹了SpringBoot連接PostgreSQL+MybatisPlus入門案例,本文通過實例代碼圖文相結合給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
項目結構
一、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) { // 實際業(yè)務邏輯處理 String username = tUser.getUsername(); String password = tUser.getPassword(); // 這里可以進行用戶名密碼驗證等操作 System.out.println("========Login successful====="); System.out.println(username + "," + password); return "Login successful"; // 返回登錄成功信息或者其他響應 } @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ù)為實體類 }
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ù)庫表 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啟動類
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層對應的接口名--> <mapper namespace="org.example.jd.mapper.TUserMapper"> <!--id是mapper層對應的接口名中對應的方法名--> <!-- <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> <!--設置駝峰匹配,MybatisPlus默認開啟駝峰匹配--> <!-- <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 #設置mybatis-plus mybatis-plus: config-location: classpath:mybatis/mybatis-config.xml #配置文件 mapper-locations: classpath:mybatis/mapper/*.xml #設置mapper層對應的接口對應的mapper.xml的路徑 type-aliases-package: org.example.jd.pojo #設置別名,mapper.xml中resultType="org.example.jd.pojo.TUser"可省去包,即resultType="TUser" #開啟自動駝峰映射,注意:配置configuration.map-underscore-to-camel-case則不能配置config-location,可寫到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");
三、運行
瀏覽器或者postman請求地址。
注意:瀏覽器只能測試get請求,如果有put、post、delete請使用postman測試。
http://localhost:8090/api/getUserList
到此這篇關于SpringBoot連接PostgreSQL+MybatisPlus入門案例的文章就介紹到這了,更多相關SpringBoot連接PostgreSQL內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在SpringBoot 中從application.yml中獲取自定義常量方式
這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04詳解SpringBoot Mybatis如何對接多數(shù)據(jù)源
這篇文章主要為大家介紹了SpringBoot Mybatis如何對接多數(shù)據(jù)源實現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09spring boot 2.x html中引用css和js失效問題及解決方法
這篇文章主要介紹了spring boot 2.x html中引用css和js失效,需要的朋友可以參考下2018-11-11