当前位置: 首页 > news >正文

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 {@Autowiredprivate TUserService tUserService;@PostMapping("/api/userLogin")public String login(@RequestBody TUser tUser) {// 实际业务逻辑处理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> { //参数为实体类}

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")   //指定数据库表
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 {@Autowiredprivate TUserMapper tUserMapper;@Overridepublic 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 mapperPUBLIC "-//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 configurationPUBLIC "-//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/myjdusername: postgrespassword: 123456driver-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 TransferSource Server         : myPgSQLSource Server Type    : PostgreSQLSource Server Version : 160003 (160003)Source Host           : localhost:5432Source Catalog        : myjdSource Schema         : publicTarget Server Type    : PostgreSQLTarget Server Version : 160003 (160003)File Encoding         : 65001Date: 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入门案例

项目结构 一、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://mave…...

vue3里将table表格中的数据导出为excel

想要实现前端对表格中的数据进行导出&#xff0c;这里推荐使用xlsx这个依赖库实现。 1、安装 pnpm install xlsx 2、使用 import * as XLSX from "xlsx"; 直接在组件里导入XLSX库&#xff0c;然后给表格table通过ref创建响应式数据拿到table实例&#xff0c;将实…...

【算法】分布式共识Paxos

一、引言 在分布式系统中&#xff0c;一致性是至关重要的一个问题。Paxos算法是由莱斯利兰伯特&#xff08;Leslie Lamport&#xff09;在1990年提出的一种解决分布式系统中一致性问题的算法。 二、算法原理 Paxos算法的目标是让一个分布式系统中的多个节点就某个值达成一致。算…...

软考:软件设计师 — 5.计算机网络

五. 计算机网络 1. OSI 七层模型 层次名称主要功能主要设备及协议7应用层实现具体的应用功能 POP3、FTP、HTTP、Telent、SMTP DHCP、TFTP、SNMP、DNS 6表示层数据的格式与表达、加密、压缩5会话层建立、管理和终止会话4传输层端到端的连接TCP、UDP3网络层分组传输和路由选择 三…...

C++ //练习 15.28 定义一个存放Quote对象的vector,将Bulk_quote对象传入其中。计算vector中所有元素总的net_price。

C Primer&#xff08;第5版&#xff09; 练习 15.28 练习 15.28 定义一个存放Quote对象的vector&#xff0c;将Bulk_quote对象传入其中。计算vector中所有元素总的net_price。 环境&#xff1a;Linux Ubuntu&#xff08;云服务器&#xff09; 工具&#xff1a;vim 代码块&am…...

Midjourney绘画提示词精选

Midjourney绘画提示词精选 在探索Midjourney这一强大的AI绘画工具时&#xff0c;选择合适的提示词是创作出令人惊艳作品的关键。这些提示词不仅能够帮助Midjourney理解你的创作意图&#xff0c;还能引导它生成出符合你期望的图像。以下是对Midjourney绘画提示词的精选与解析&a…...

Kylin中的RBAC:为大数据安全加把锁

Kylin中的RBAC&#xff1a;为大数据安全加把锁 Apache Kylin是一个开源的分布式分析引擎&#xff0c;旨在为Hadoop平台提供快速的大数据量SQL查询能力。随着企业对数据安全和访问控制需求的增加&#xff0c;基于角色的访问控制&#xff08;Role-Based Access Control&#xff…...

DDoS 攻击下的教育网站防护策略

随着互联网的普及&#xff0c;教育网站成为学生和教师获取信息、进行在线学习的重要平台。然而&#xff0c;这些网站也成为了网络攻击的目标&#xff0c;尤其是分布式拒绝服务&#xff08;DDoS&#xff09;攻击。本文将探讨DDoS攻击对教育网站的影响&#xff0c;并提出一系列有…...

Android13以太网静态IP不保存的问题

最近在做Amlogic T982的样机&#xff0c;关于以太网部分&#xff0c;系统Settings只有一个Ethernet的条目&#xff0c;没有其他任何信息&#xff0c;什么以太网mac地址&#xff0c;开关&#xff0c;IP地址&#xff0c;子网掩码&#xff0c;默认网关&#xff0c;dns, 设置代理&a…...

Redis 7.x 系列【31】LUA 脚本

有道无术&#xff0c;术尚可求&#xff0c;有术无道&#xff0c;止于术。 本系列Redis 版本 7.2.5 源码地址&#xff1a;https://gitee.com/pearl-organization/study-redis-demo 文章目录 1. 概述2. 常用命令2.1 EVAL2.2 SCRIPT LOAD2.3 EVALSHA2.4 SCRIPT FLUSH2.5 其他 3. …...

mysql中You can’t specify target table for update in FROM clause错误

mysql中You can’t specify target table for update in FROM clause错误 You cannot update a table and select directly from the same table in a subquery. mysql官网中有这句话&#xff0c;我们不能在一个语句中先在子查询中从某张表查出一些值&#xff0c;再update这张表…...

Linux Vim最全面的教程

Vim 是一个非常强大的文本编辑器&#xff0c;它在 Linux 环境中尤其受欢迎。Vim 支持高度定制&#xff0c;并且拥有丰富的功能&#xff0c;包括多级撤销、宏、脚本语言支持等。下面是关于 Vim 的一个较为全面的教程。 Vim 的启动 要启动 Vim&#xff0c;你可以在终端中输入 v…...

setsockopt选项对tcp速度

GPT-4 (OpenAI) 每个setsockopt调用都涉及到一个套接字描述符&#xff0c;一个指定网络层的常数&#xff08;如IPPROTO_IP, IPPROTO_TCP, IPPROTO_IPV6, SOL_SOCKET等&#xff09;&#xff0c;一个指定需配置的选项的常数&#xff0c;一个指向配置值的指针&#xff0c;以及那个…...

HarmonyOS应用开发者高级认证,Next版本发布后最新题库 - 多选题序号3

基础认证题库请移步&#xff1a;HarmonyOS应用开发者基础认证题库 注&#xff1a;有读者反馈&#xff0c;题库的代码块比较多&#xff0c;打开文章时会卡死。所以笔者将题库拆分&#xff0c;单选题20个为一组&#xff0c;多选题10个为一组&#xff0c;题库目录如下&#xff0c;…...

bool数组的理解和应用[C++]

文章目录 bool数组的用法bool数组的定义声明bool数组的初始化访问和修改数组元素遍历数组 运用bool数组简单代码 在今天做题中发现了bool类不仅能用于函数类型还能用于数组类型&#xff0c;好奇查了查发现bool还有很多用处&#xff1a;基本变量&#xff0c;在枚举类型中会用到&…...

JavaScript模拟滑动手势

双击回到顶部 左滑动 右滑动 代码展示 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta name"viewport" content"widthdevice-width, initial-scale1.0" /><title>Gesture…...

Text Control 控件教程:使用 .NET C# 中的二维码和条形码增强文档

QR 码和条形码非常适合为文档和 PDF 文件增加价值&#xff0c;因为它们提供轻松的信息访问、验证信息、跟踪项目和提高交互性。条形码可以弥补纸质或数字人类可读文档与网络门户或网络应用程序中的数字信息之间的差距。大多数用户都熟悉 QR 码和条形码&#xff0c;它们在许多过…...

最新爆火的开源AI项目 | LivePortrait 本地安装教程

LivePortrait 本地部署教程&#xff0c;强大且开源的可控人像AI视频生成 1&#xff0c;准备工作&#xff0c;本地下载代码并准备环境&#xff0c;运行命令前需安装git 以下操作不要安装在C盘和容量较小的硬盘&#xff0c;可以找个大点的硬盘装哟 2&#xff0c;需要安装FFmp…...

揭秘Django与Neo4j:构建智能知识图谱的终极指南

揭秘Django与Neo4j:构建智能知识图谱的终极指南 前言 图是一种用于对象之间的成对关系进行建模的数学结构。 它由两个主要元素组成:节点和关系。 节点:节点可以看作是传统数据库中的记录。每个节点代表一个对象或实体,例如一个人或一个地方。节点按标签分类,这有助于根…...

项目一缓存商品

文章目录 概要整体架构流程技术细节小结 概要 因为商品是经常被浏览的,所以数据库的访问量就问大大增加,造成负载过大影响性能,所以我们需要把商品缓存到redis当中,因为redis是存在内存中的,所以效率会比MySQL的快. 整体架构流程 技术细节 我们在缓存时需要保持数据的一致性所…...

STM8/STM32 GPIO触摸按键实现与优化

基于STM8/STM32的GPIO触摸按键实现技术解析1. 触摸按键技术概述1.1 传统方案与MCU实现对比在消费类电子产品中&#xff0c;触摸按键的实现通常有两种主流方案&#xff1a;专用触摸IC方案&#xff1a;集成度高但成本较高MCU GPIO方案&#xff1a;利用通用微控制器实现&#xff0…...

FastAPI CSP哈希:nonce与sha256的终极安全防护指南

FastAPI CSP哈希&#xff1a;nonce与sha256的终极安全防护指南 【免费下载链接】fastapi FastAPI framework, high performance, easy to learn, fast to code, ready for production 项目地址: https://gitcode.com/GitHub_Trending/fa/fastapi FastAPI作为一款高性能、…...

开源工具权限重置指南:跨平台AI编程助手试用限制解决方案

开源工具权限重置指南&#xff1a;跨平台AI编程助手试用限制解决方案 【免费下载链接】go-cursor-help 解决Cursor在免费订阅期间出现以下提示的问题: Youve reached your trial request limit. / Too many free trial accounts used on this machine. Please upgrade to pro. …...

YOLOFuse镜像亮点解析:环境零配置与多种融合策略详解

YOLOFuse镜像亮点解析&#xff1a;环境零配置与多种融合策略详解 1. 引言&#xff1a;多模态检测的工程挑战 在智能安防和自动驾驶领域&#xff0c;工程师们经常面临一个现实问题&#xff1a;白天表现优秀的目标检测系统&#xff0c;到了夜间或恶劣天气环境下性能急剧下降。传…...

OpenClaw从入门到应用——安装:更新OpenClaw

通过OpenClaw实现副业收入&#xff1a;《OpenClaw赚钱实录&#xff1a;从“养龙虾“到可持续变现的实践指南》 推荐方式&#xff1a;重新运行网站安装程序&#xff08;原地升级&#xff09; 首选的更新方式是重新运行官网提供的安装脚本。该脚本会自动检测现有安装&#xff0…...

OpCore-Simplify:如何用零代码工具在15分钟内完成黑苹果配置

OpCore-Simplify&#xff1a;如何用零代码工具在15分钟内完成黑苹果配置 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 对于想要在PC上安装macOS的用…...

背单词花园:把单词种进长期记忆,告别背了就忘

为什么背单词花园抗遗忘效果出众&#xff1f;因为它把艾宾浩斯遗忘曲线&#xff0c;变成了看得见、好坚持的种花流程。一、新学单词 收获种子&#xff0c;记忆从第一步就扎根每次领取种子&#xff0c;就是开启一次新单词学习。用趣味场景完成初次编码&#xff0c;让单词不再是…...

基于Python的物流管理系统毕业设计源码

博主介绍&#xff1a;✌ 专注于Java,python,✌关注✌私信我✌具体的问题&#xff0c;我会尽力帮助你。一、研究目的本研究旨在开发一套基于Python的物流管理系统&#xff0c;以提升物流企业的运营效率和管理水平。具体而言&#xff0c;研究目的可从以下几个方面进行阐述&#x…...

技能组合艺术:OpenClaw串联QwQ-32B实现复杂工作流

技能组合艺术&#xff1a;OpenClaw串联QwQ-32B实现复杂工作流 1. 为什么需要工作流串联 当我第一次接触OpenClaw时&#xff0c;最让我兴奋的不是它能完成某个单一任务&#xff0c;而是它能够将多个技能像乐高积木一样组合起来。这种能力让我想到了现实工作中的场景——很少有…...

OpenClaw技能市场巡礼:ollama-QwQ-32B支持的10个高效自动化模块

OpenClaw技能市场巡礼&#xff1a;ollama-QwQ-32B支持的10个高效自动化模块 1. 为什么需要技能市场&#xff1f; 当我第一次接触OpenClaw时&#xff0c;最让我惊喜的不是它能操控鼠标键盘的能力&#xff0c;而是它背后那个充满可能性的技能市场。作为一个长期被重复性工作困扰…...