SpringBoot连接PostgreSQL+MybatisPlus入门案例

news2024/9/20 18:41:25

项目结构

一、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) {
        // 实际业务逻辑处理
        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 {

    @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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1936007.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于秒杀系统的企业开发设计思考

一、需求分析 需求描述为实现某商品秒杀活动&#xff0c;结果为商品库存为0&#xff0c;订单数量和商品原有库存数量相等&#xff0c;即保障系统数据一致性同时&#xff0c;保障系统稳定性 二、流程设计 三、数据库设计 本次示例仅涉及商品表、订单表&#xff0c;这里分享数…

2024可信数据库发展大会:TDengine CEO 陶建辉谈“做难而正确的事情”

在当前数字经济快速发展的背景下&#xff0c;可信数据库技术日益成为各行业信息化建设的关键支撑点。金融、电信、能源和政务等领域对数据处理和管理的需求不断增加&#xff0c;推动了数据库技术的创新与进步。与此同时&#xff0c;人工智能与数据库的深度融合、搜索与分析型数…

《Towards Black-Box Membership Inference Attack for Diffusion Models》论文笔记

《Towards Black-Box Membership Inference Attack for Diffusion Models》 Abstract 识别艺术品是否用于训练扩散模型的挑战&#xff0c;重点是人工智能生成的艺术品中的成员推断攻击——copyright protection不需要访问内部模型组件的新型黑盒攻击方法展示了在评估 DALL-E …

AI算法18-最小角回归算法Least Angle Regression | LARS

​​​ 最小角回归算法简介 最小角回归&#xff08;Least Angle Regression, LAR&#xff09;是一种用于回归分析的统计方法&#xff0c;它在某些方面类似于最小二乘回归&#xff0c;但提供了一些额外的优点。最小角回归由Bradley Efron等人提出&#xff0c;主要用于处理具有…

【鸿蒙学习笔记】舜和酒店项目开发

这里写目录标题 前期准备1. 环境准备2. 开发工具准备 创建项目1. 使用 deveco-studio 创建 ShunHeHotel 项目2. 把ShunHeHotel 项目使用git进行版本控制3. 提交第1个commit&#xff0c;Alt0 → 输入commit message → 提交4. 查看已经提交的第一个提交5. gitcode 创建同名远程项…

数据库系统概论:关系型数据库系统

引言 如今最为重要的数据模型便是关系模型。关系数据库就是支持关系模型的数据库系统&#xff08;Relational Database Management System, RDBMS&#xff09; 关系模型可以简单理解为二维表格模型&#xff0c;一个关系型数据库就是由二维表及其之间的关系组成的一个数据组织。…

1、常用模块讲解(simulink仿真)

1、常用模块讲解&#xff08;simulink仿真&#xff09; Simulate有很多库 进入simulink 命令行simulink再回车 &#xff0c;或点击菜单栏 Export model to 可以将高版本保存为低版本 1&#xff0c;在MATLAB主界面&#xff0c;新建编辑器&#xff0c; a1:100; bsin(a*0.2); pl…

argparse部分用法

文章目录 一、一个简单的示例1.导入模块2.创建解析器3.添加参数4.解析参数5.使用解析的参数6.完整程序7.运行 二、更多用法1.ArgumentParser对象创建时的参数2.add_argument()的参数&#xff08;1&#xff09;name&#xff1a;名称1&#xff09;位置参数2&#xff09;可选参数3…

priority_queue的使用与模拟实现

目录 priority_queue的使用 priority_queue的介绍 priority_queue的定义方式 priority_queue成员函数的介绍 priority_queue的模拟实现 1&#xff1a;堆的向上调整算法 2&#xff1a;堆的向下调整算法 两种算法的比较与各自最佳使用 priority_queue的模拟实现 priorit…

使用VMware虚拟机安装kali 2019

一、下载kali linux镜像 下载 kali Linux的ISO镜像文件 网盘链接&#xff1a;https://pan.baidu.com/s/1GRtJxGBlqFfmU24HLEy3-g?pwd57u3 提取码&#xff1a;57u3 二、安装并配置 Kali Linux 新建虚拟机 在虚拟机创建向导中&#xff0c;选择经典配置 选择下载好的kali镜…

细说MCU用定时器控制ADC采样频率的实现方法并通过Simulink查看串口输出波形

目录 一、硬件工程 二、建立Simulink模型 1.安装MATLAB和Simulink 2.建立Simulink模型 三、代码修改 1.修改回调函数 2.产看结果 3.完整的main.c 本文作者的文章 细说MCU用定时器控制ADC采样频率的实现方法-CSDN博客 https://wenchm.blog.csdn.net/article/details/…

WSL-Ubuntu20.04环境使用YOLOv8 TensorRT推理加速

在阅读本章内容之前,需要把部署环境以及训练环境都安装好。 1.TensorRTX下载 这里使用Wang-xinyu大佬维护的TensorRTX库来对YOLOv8进行推理加速的演示,顺便也验证一下前面环境配置的成果。 github地址:GitHub - wang-xinyu/tensorrtx,下载后放到wsl的路径下,我这里放在/h…

JavaScript日期对象倒计时案例

思路&#xff1a;1.先求出当前时间的总毫秒数 2.再求出所需要求的时间的总毫秒数 3.用所求时间的减去当前时间的可得到倒计时剩余时间 4.最后将所求的倒计时剩余时间转换为天&#xff0c;小时&#xff0c;分钟&#xff0c;秒即可 <!DOCTYPE html> <html lang"en…

Java并发04之线程同步机制

文章目录 1 线程安全1.1 线程安全的变量1.2 Spring Bean1.3 如果保证线程安全 2 synchronized关键字2.1 Java对象头2.1.1 对象组成部分2.1.2 锁类型2.1.3 锁对象 2.2 synchronized底层实现2.2.1 无锁状态2.2.2 偏向锁状态2.2.3 轻量级锁状态2.2.4 重量级锁2.2.5 锁类型总结2.2.…

【动态规划】力扣2266.统计打字方案数

Alice 在给 Bob 用手机打字。数字到字母的 对应 如下图所示。在这里插入图片描述 为了 打出 一个字母&#xff0c;Alice 需要 按 对应字母 i 次&#xff0c;i 是该字母在这个按键上所处的位置。 比方说&#xff0c;为了按出字母 ‘s’ &#xff0c;Alice 需要按 ‘7’ 四次。…

C++:类的默认成员函数

默认成员函数就是⽤⼾没有显式实现&#xff0c;编译器会⾃动⽣成的成员函数称为默认成员函数。⼀个类&#xff0c;我们不写的情况下编译器会默认⽣成以下6个默认成员函数&#xff0c;需要注意的是这6个中最重要的是前4个。 定义一个空类&#xff1a; class A { }; 经过编译器…

Etsy开店指南:分步指南与防封技巧

您的Etsy帐户在注册后不久就被封了吗&#xff1f;如果是这样&#xff0c;您在设置Etsy帐户时就已经错误了&#xff0c;其实这其中还是有很多细节需要注意&#xff1b;本文全面讲解了如何逐步创建帐户&#xff0c;如果你也正在准备&#xff0c;那就继续看吧&#xff01; 一、在开…

基于Java的汽车租赁管理系统设计(含文档、源码)

本篇文章论述的是基于Java的汽车租赁管理系统设计的详情介绍&#xff0c;如果对您有帮助的话&#xff0c;还请关注一下哦&#xff0c;如果有资源方面的需要可以联系我。 目录 摘 要 系统运行截图 系统总体设计 系统论文 资源下载 摘 要 近年来&#xff0c;随着改革开放…

元宇宙:科技巨头的下一个战场

热门标题&#xff1a; “元宇宙&#xff1a;科技巨头的下一个战场” 相关文章问题&#xff1a; 问题&#xff1a; 在科技巨头纷纷布局元宇宙的背景下&#xff0c;元宇宙将如何影响未来的科技产业和日常生活&#xff1f; 文章概要&#xff1a; 随着Facebook更名为Meta&…

Unity XR Interaction Toolkit的安装(二)

提示&#xff1a;文章有错误的地方&#xff0c;还望诸位大神不吝指教&#xff01; 文章目录 前言一、安装1.打开unity项目2.打开包管理器&#xff08;PackageManage&#xff09;3.导入Input System依赖包4.Interaction Layers unity设置总结 前言 安装前请注意&#xff1a;需要…