Python 官方文档:入门教程 => 点击学习
实现步骤: 工具:idea 数据库版本:Mysql5.7 一、环境搭建 1.创建SpringBoot项目 pom.xml 2.pom.xml : spring WEB、lombo
1.创建SpringBoot项目
pom.xml
2.pom.xml : 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.3.1.tmp</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.driver-class-name=com.mysql.cj.jdbc.Driver
#配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
数据库数据
#建表
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)
);
#插入数据
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
代码详解
1.新建实体类User
lombok注解详解:
@Data:自动生成get\set方法
@AllArgsConstructor :有参方法
@NoArgsConstructor:无参方法
@TableId(value = “id”,type = IdType.AUTO) :给实体设置自增
package com.sh.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Primary;
import java.util.Date;
@Data
@AllArgsConstructor
@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 com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sh.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
3.创建控制层controller:
新建UserController类
package com.sh.controller;
import com.sh.entity.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;
}
//根据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;
}
}
项目结构
注意:添加,修改操作需要数据库和实体设置自增
到此这篇关于MybatisPlus实现简单增删改查的文章就介绍到这了,更多相关MybatisPlus增删改查内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: MybatisPlus实现简单增删改查功能
本文链接: https://lsjlt.com/news/122720.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0