【Mybatis Plus-01】CRUD简单使用

一、引入maven依赖
<!--mybatis-plus-->
<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.5.1</version>
</dependency>
二、创建数据库表
CREATE TABLE `boot_user` (
  `id` int NOT NULL COMMENT '主键id',
  `username` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '用户名',
  `password` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '密码',
  `avatar` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '头像',
  `nickName` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '昵称',
  `address` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '地址',
  `email` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '邮箱',
  `birthday` date DEFAULT NULL COMMENT '生日',
  `age` int DEFAULT NULL COMMENT '年龄',
  `height` double(10,2) DEFAULT NULL COMMENT '身高',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
三、在spring boot工程中创建
User.java
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "boot_user") //@TableName注解用来指定表名
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("用户id")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty("用户名")
    private String username;

    @ApiModelProperty("密码")
    private String password;

    @ApiModelProperty("头像")
    private String avatar;

    @ApiModelProperty("昵称")
    //字段如果和数据库不匹配,需要使用@TableField注解指定字段名称
    @TableField(value = "nick_name")
    private String nickName;

    @ApiModelProperty("地址")
    private String address;

    @ApiModelProperty("邮箱")
    private String email;

    @ApiModelProperty("生日")
    private LocalDateTime birthday;

    @ApiModelProperty("年龄")
    private Integer age;

    @ApiModelProperty("身高")
    private Double height;

    @ApiModelProperty("创建时间")
    @TableField(value = "create_time")
    private LocalDateTime createTime;

    @ApiModelProperty("修改时间")
    @TableField(value = "update_time")
    private LocalDateTime updateTime;
UserMapper.java
public interface UserMapper extends BaseMapper<User> {

}
IUserService.java
public interface IUserService extends IService<User> {
   
}
UserServiceImpl.java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

}
UserMapper.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">
<mapper namespace="com.cy.springboot.mapper.UserMapper">

</mapper>

四、通用mapper的CRUD

MapperTests.java

@SpringBootTest
@RunWith(SpringRunner.class)
public class MapperTests {
    @Resource
    private UserMapper userMapper;
	
	//增:C
	@Test
    public void c(){
        User user=new User();
        user.setUsername("account");
        user.setPassword("qwe123");
        user.setNickName("普通用户");
        user.setAddress("河北");
        user.setEmail("dsad@123.com");
        user.setBirthday(new Date());
        user.setAge(10);
        user.setHeight(180.88);
        int insert = userMapper.insert(user);
        System.out.println(insert);
    }
}

打印sql
在这里插入图片描述

@SpringBootTest
@RunWith(SpringRunner.class)
public class MapperTests {
    @Resource
    private UserMapper userMapper;
	
	//删:D
	@Test
    public void d(){
		//根据id删除一条数据库数据,返回受影响的行数
        int i = userMapper.deleteById(3);
        System.out.println(i);
    }
}

打印sql

在这里插入图片描述

@SpringBootTest
@RunWith(SpringRunner.class)
public class MapperTests {
    @Resource
    private UserMapper userMapper;
	
	//改:U
	@Test
    public void u(){
        User user = new User();
        user.setId(2);
        user.setAddress("天津");
        int i = userMapper.updateById(user);
        System.out.println(i);
    }
}

打印sql

在这里插入图片描述

@SpringBootTest
@RunWith(SpringRunner.class)
public class MapperTests {
    @Resource
    private UserMapper userMapper;
	
	//查:r
    @Test
    public void r(){
        List<User> users = userMapper.selectList(null);
        System.out.println(users);
    }

打印sql

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蜜蜂127

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值