赞
踩
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.school</groupId>
<artifactId>study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>study</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 不加这个就报Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.1</version>
</plugin>
</plugins>
</build>
</project>
spring:
profiles:
active: dev
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 9000
package com.school.study.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author 又菜又爱玩
* @version 1.0
* @date 2021/12/10 14:15
* @blog https://blog.csdn.net/biancheng4/article/details/121727982
*/
@ApiModel("用户实体类") //swagger-ui
@Data //等同于set/get方法
@AllArgsConstructor //构造函数
@NoArgsConstructor // 生成一个无参数的构造方法
public class user {
@ApiModelProperty(value = "用户id唯一主键")
@TableId(type = IdType.AUTO)
private Integer id;
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private Integer password;
}
package com.school.study.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.school.study.pojo.user;
import org.apache.ibatis.annotations.Mapper;
/**
* @author 又菜又爱玩
* @version 1.0
* @date 2021/12/10 14:27
* @blog https://blog.csdn.net/biancheng4/article/details/121727982
*/
@Mapper
public interface userMapper extends BaseMapper<user> {
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.baomidou.mybatisplus.core.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
public interface BaseMapper<T> {
int insert(T var1);
int deleteById(Serializable var1);
int deleteByMap(@Param("cm") Map<String, Object> var1);
int delete(@Param("ew") Wrapper<T> var1);
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> var1);
int updateById(@Param("et") T var1);
int update(@Param("et") T var1, @Param("ew") Wrapper<T> var2);
T selectById(Serializable var1);
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> var1);
List<T> selectByMap(@Param("cm") Map<String, Object> var1);
T selectOne(@Param("ew") Wrapper<T> var1);
Integer selectCount(@Param("ew") Wrapper<T> var1);
List<T> selectList(@Param("ew") Wrapper<T> var1);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> var1);
List<Object> selectObjs(@Param("ew") Wrapper<T> var1);
IPage<T> selectPage(IPage<T> var1, @Param("ew") Wrapper<T> var2);
IPage<Map<String, Object>> selectMapsPage(IPage<T> var1, @Param("ew") Wrapper<T> var2);
}
package com.school.study.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.school.study.pojo.user;
import org.springframework.stereotype.Service;
/**
* @author 又菜又爱玩
* @version 1.0
* @date 2021/12/10 14:32
* @blog https://blog.csdn.net/biancheng4/article/details/121727982
*/
public interface userService extends IService<user> {
//增加
boolean addUser(user user);
//删除
boolean deleteUser(int id);
//更新
boolean updateUser(user user);
//查找
user findUserById(int id);
}
package com.school.study.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.school.study.mapper.userMapper;
import com.school.study.pojo.user;
import com.school.study.service.userService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 又菜又爱玩
* @version 1.0
* @date 2021/12/10 14:43
* @blog https://blog.csdn.net/biancheng4/article/details/121727982
*/
@Service
public class userServiceImpl extends ServiceImpl<userMapper, user> implements userService {
//增删改返回布尔值,判断是否成功
@Override
public boolean addUser(user user) {
int i = baseMapper.insert(user);
return i > 0;
}
@Override
public boolean deleteUser(int id) {
int i = baseMapper.deleteById(id);
return i > 0;
}
@Override
public boolean updateUser(user user) {
int i = baseMapper.updateById(user);
return i > 0;
}
@Override
public user findUserById(int id) {
return baseMapper.selectById(id);
}
}
package com.school.study.controller;
import com.school.study.pojo.user;
import com.school.study.response.CommonResponse;
import com.school.study.service.userService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @author 又菜又爱玩
* @version 1.0
* @date 2021/12/10 15:00
* @blog https://blog.csdn.net/biancheng4/article/details/121727982
*/
@Api(tags = "用户信息类")
@RestController
public class userController {
@Autowired
private userService service;
@ApiOperation("用Id获取用户信息")
@GetMapping("/findById")
public CommonResponse findById(int id){
return CommonResponse.ok().data("items", service.findUserById(id));
}
//要写RequestBody
@ApiOperation("插入数据")
@PostMapping("/insert")
public CommonResponse insert(@RequestBody user user){
boolean flag = service.addUser(user);
if (flag){
return CommonResponse.ok().data("items", "插入成功");
} else {
return CommonResponse.error().data("items", "插入失败");
}
}
@ApiOperation("删除数据")
@PostMapping("/delete")
public CommonResponse delete(int id){
boolean flag = service.deleteUser(id);
if (flag){
return CommonResponse.ok().data("items", "删除成功");
} else {
return CommonResponse.error().data("items", "删除失败");
}
}
@ApiOperation("更新数据")
@PostMapping("/upDate")
public CommonResponse upDate(@RequestBody user user){
boolean flag = service.updateUser(user);
if (flag){
return CommonResponse.ok().data("items", "更新成功");
} else {
return CommonResponse.error().data("items", "更新失败");
}
}
}
我的id使用的是自增的,保证唯一性,剩下的就不演示了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。