项目结构
一、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
本文转载自: https://blog.csdn.net/m0_63615119/article/details/140560590
版权归原作者 小天博客 所有, 如有侵权,请联系我们删除。
版权归原作者 小天博客 所有, 如有侵权,请联系我们删除。