MybatisPlus实现简单增删改查功能实现步骤:
⼯具:IDEA
数据库版本:mysql5.7
⼀、环境搭建
1.创建springboot项⽬
l : spring web、lombok mysql
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.添加mybatisplus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.p</version>
</dependency>
application.properties
4.application.properties配置⽂件:⽤户名、密码、连接驱动、配置⽇志
注意:mysql8 需要增加时区的配置 serverTimezone=GMT%2B8 东⼋区
#mysql5 驱动不同
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?useSSL=false&useUnicode=true&characterEncoding=utf-8 spring.datasource.sql.cj.jdbc.Driver
#配置⽇志
数据库数据
#建表
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);web开发源码
#插⼊数据
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou'),
(2, 'Jack', 20, 'test2@baomidou'),
(3, 'Tom', 28, 'test3@baomidou'),
(4, 'Sandy', 21, 'test4@baomidou'),
(5, 'Billie', 24, 'test5@baomidou');
mysql面试题 增删改查代码详解
1.新建实体类User
lombok注解详解:
@Data:⾃动⽣成get\set⽅法
@AllArgsConstructor :有参⽅法
@NoArgsConstructor:⽆参⽅法
@TableId(value = “id”,type = IdType.AUTO) :给实体设置⾃增package ity;
import batisplus.annotation.IdType;
import batisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
女switch什么意思
import lombok.NoArgsConstructor;
import t.annotation.Primary;
import java.util.Date;
@Data
@AllArgsConstructor
sql字符串函数@NoArgsConstructor
public class User {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private String email;
private Date createTime;
private Date updateTime;
}
2. 新建mapper包
在mapper包⾥建UserMappe接⼝r 继承BaseMapper<;实体类> package com.sh.mapper;
import apper.BaseMapper;
import ity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
3.创建控制层controller:
新建UserController类
package ller;
import ity.User;
import com.sh.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
//查询查询所以 null 就是没有条件
@GetMapping("/selectUser")
public List<User> getAll(){
List<User> user = userMapper.selectList(null);
return user;
}html颜表示方法
//根据id查询
@GetMapping("/selectById")
public User selectUserById(){
User user = userMapper.selectById(1);
return user;
}
//根据多个id查询 Arrays.asList集合
@GetMapping("/selectByIds")
public List<User> selectUserByIds(){
List<User> user = userMapper.selectBatchIds(Arrays.asList(1,2,3));  return user;
}
//添加
@PostMapping("/insertUser")
public Integer insertUsers(User user){
Integer result = userMapper.insert(user);
return result;
}
//修改根据id
@PutMapping("/updateUser")
public Integer updateUsers(User user){
Integer result = userMapper.updateById(user);
return result;
}
//删除根据id
@DeleteMapping("/deleteUser")
public Integer deleteUsers(Integer id){
Integer result = userMapper.deleteById(id);
return result;
}
可以白嫖java项目代码的网站}
项⽬结构
注意:添加,修改操作需要数据库和实体设置⾃增
到此这篇关于MybatisPlus实现简单增删改查的⽂章就介绍到这了,更多相关MybatisPlus增删改查内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。