亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • mybatis-plus如何禁用一級緩存的方法

    mybatis-plus如何禁用一級緩存的方法

    這篇文章主要介紹了mybatis-plus如何禁用一級緩存的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • Java Spring AOP詳解

    Java Spring AOP詳解

    這篇文章主要介紹了Java的Spring框架中的AOP實現(xiàn)實例,AOP面向切面編程其實也可以被看作是一個設計模式去規(guī)范項目的結構,需要的朋友可以參考下
    2021-09-09
  • 2024最新版JDK安裝保姆級教程

    2024最新版JDK安裝保姆級教程

    這篇文章主要介紹了2024最新版JDK安裝保姆級教程,JDK是Java開發(fā)的核心工具包,包含編譯器、運行時環(huán)境和標準庫等,安裝JDK首先要從官網(wǎng)下載合適版本,如JDK17,然后進行安裝,安裝完成后需配置環(huán)境變量JAVA_HOME和Path,確保系統(tǒng)可以正確找到Java命令,需要的朋友可以參考下
    2024-09-09
  • Java異常堆棧打印次數(shù)限制機制用法詳解

    Java異常堆棧打印次數(shù)限制機制用法詳解

    在Java開發(fā)中,異常處理是保證程序健壯性的重要手段,但當同一個異常被頻繁拋出時,日志可能會被大量重復的堆棧信息淹沒,影響問題排查效率,所以本文給大家介紹了Java異常堆棧打印次數(shù)限制機制的用法,需要的朋友可以參考下
    2025-04-04
  • 在SpringBoot 中從application.yml中獲取自定義常量方式

    在SpringBoot 中從application.yml中獲取自定義常量方式

    這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • 詳解SpringBoot Mybatis如何對接多數(shù)據(jù)源

    詳解SpringBoot Mybatis如何對接多數(shù)據(jù)源

    這篇文章主要為大家介紹了SpringBoot Mybatis如何對接多數(shù)據(jù)源實現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • spring boot 2.x html中引用css和js失效問題及解決方法

    spring boot 2.x html中引用css和js失效問題及解決方法

    這篇文章主要介紹了spring boot 2.x html中引用css和js失效,需要的朋友可以參考下
    2018-11-11
  • SpringBoot2.0 ZipKin示例代碼

    SpringBoot2.0 ZipKin示例代碼

    這篇文章主要介紹了SpringBoot2.0 ZipKin示例代碼,詳細的介紹了什么是ZipKin以及SpringBoot2.0 ZipKin示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Mybatis中#{}與${}的區(qū)別詳解

    Mybatis中#{}與${}的區(qū)別詳解

    這篇文章主要介紹了Mybatis中#{}與${}的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • eclipse中自動生成javadoc文檔的方法

    eclipse中自動生成javadoc文檔的方法

    這篇文章主要介紹了eclipse中自動生成javadoc文檔的方法,是實用eclipse開發(fā)Java程序時非常實用的技巧,對于進行Java項目開發(fā)具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12

最新評論