一、引入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