-
数据库准备
CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION=‘N‘ */;
USE `mybatis_plus`;
/*Table structure for table `user` */
CREATE TABLE `user` (
`id` bigint NOT NULL COMMENT ‘主键ID‘,
`name` varchar(30) DEFAULT NULL COMMENT ‘姓名‘,
`age` int DEFAULT NULL COMMENT ‘年龄‘,
`email` varchar(50) DEFAULT NULL COMMENT ‘邮箱‘,
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`version` int DEFAULT NULL,
`deleted` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `user` */
insert into `user`(`id`,`name`,`age`,`email`,`create_time`,`update_time`,`version`,`deleted`) values (1,‘Jone‘,18,‘test1@baomidou.com‘,NULL,NULL,NULL,NULL),(2,‘Jack‘,80,‘test2@baomidou.com‘,NULL,‘2021-07-07 17:21:34‘,NULL,NULL),(3,‘Tom‘,28,‘test3@baomidou.com‘,NULL,NULL,NULL,NULL),(4,‘Sandy‘,21,‘test4@baomidou.com‘,NULL,NULL,NULL,NULL),(5,‘Billie‘,24,‘test5@baomidou.com‘,NULL,NULL,NULL,NULL),(1412694920736972801,‘lucy‘,30,‘lucy@QQ.com‘,NULL,NULL,NULL,NULL),(1412703613004230658,‘lucy1‘,30,‘lucy@QQ.com‘,‘2021-07-07 17:22:43‘,‘2021-07-07 17:22:43‘,NULL,NULL),(1412729620788572161,‘功放不败‘,200,‘lucy@QQ.com‘,‘2021-07-07 19:06:04‘,‘2021-07-07 19:08:41‘,2,NULL),(1412743075298000898,‘erhuo‘,30,‘lucy@QQ.com‘,‘2021-07-07 19:59:31‘,‘2021-07-07 19:59:31‘,1,1),(1412746872967577602,‘我是Helen‘,18,‘helen@sina.com‘,‘2021-07-07 20:14:37‘,‘2021-07-07 20:14:37‘,1,0);
-
基本环境搭建
新建一个Springboot项目,导入pom依赖(版本:2.2.1.RELEASE)
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--lombok用来简化实体类--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies>
建立基本结构和配置框架
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=1234 #mybatis日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0 spring.profiles.active=dev
实体类
user.java,这里使用了Lombok,会自动生成get、set方法和toString方法,以及有参无参构造器
import com.baomidou.mybatisplus.annotation.*; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @NoArgsConstructor @AllArgsConstructor @Data public class User { //@TableId(type = IdType.ID_WORKER)//mp自带策略,生成19位数值,数字使用这个 private Long id; //@TableId(type = IdType.ID_WORKER_STR)//字符串使用这个 private String name; private Integer age; private String email; @TableField(fill = FieldFill.INSERT) private Date createTime; @TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime; @Version @TableField(fill = FieldFill.INSERT) private Integer version; @TableLogic @TableField(fill = FieldFill.INSERT) private Integer deleted; }
mapper
UserMapper.java,这里实体类的mapper不需要去写接口方法,只需要去继承BaseMapper<User>这个类,就可以了,通过@Repository将实体类接口注册到Spring中。
import com.atguigu.entity.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; @Repository public interface UserMapper extends BaseMapper<User> { }
-
测试
测试之前要讲实体类接口注入到测试方法中。
查询操作
查询所有user信息,通过UserMapper调用selectList方法,这个方法是Mybatis-plus提供的,这个方法是用来查询多个用户的,参数部分就是要填写的条件,因为要查询所有user,所以条件null,如果要填写条件需要实现wrapper的子类,一般会实现QueryWrapper这个子类,然后调用wrapper中的方法,将条件设置到wrapper中,然后将wrapper放到selectList中,这样就设置好了条件查询。
@Autowired private UserMapper userMapper; @Test void contextLoads() { List<User> users = userMapper.selectList(null); for (User user : users) { System.out.println(user); } }
插入操作
@Autowired private UserMapper userMapper; @Test public void testInsert(){ User user = new User(); user.setName("Helen"); user.setAge(18); user.setEmail("55317332@qq.com"); int result = userMapper.insert(user); System.out.println(result); //影响的行数 System.out.println(user); //id自动回填 }
更新操作
@Test public void testUpdateById(){ User user = new User(); user.setId(1L); user.setAge(28); int result = userMapper.updateById(user); System.out.println(result); }
删除操作
@Test public void testSelectById(){ User user = userMapper.selectById(1L); System.out.println(user); }