添加依賴
<!--mybatis-plus的springboot支持-->
<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>
<version>5.1.47</version>
</dependency>
創建數據庫和表
CREATE TABLE `user` (`id` BIGINT(20) not null AUTO_INCREMENT comment '主鍵ID', `name` VARCHAR(30) DEFAULT null comment '姓名', `age` int(11) DEFAULT null comment '年齡', `email` VARCHAR(50) DEFAULT null comment '郵箱', PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8;
insert into
user
(id
,name
,age
,
insert intouser
(id
,name
,age
,
insert intouser
(id
,name
,age
,
insert intouser
(id
,name
,age
,
insert intouser
(id
,name
,age
,
配置 application.yml
spring:
application:
name: spring-boot-mybatis-plus
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mysqlName?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
username: root
password: root
創建pojo類
public class User {
@TableId(value = "ID",type = IdType.AUTO) //設置id為自增長
private Long id;
private String name;
private Integer age;
private String email;
//( set,get,toString方法自行添加 )
}
創建 UserMapper接口
public interface UserMapper extends BaseMapper<User> {
}
注意:BaseMapper接口引入的包是
com.baomidou.mybatisplus.core.mapper.BaseMapper
編寫 SpringBoot 啟動類
@MapperScan("cn.mybatisplus.mapper")
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
@MapperScan("cn.mybatisplus.mapper"). 掃描 mapper 包
編寫測試代碼
@SpringBootTest @RunWith(SpringRunner.class) public class UserMapperTest {
@Autowired private UserMapper userMapper; @Test public void testList(){ List<User> users = this.userMapper.selectList(null); for (User user: users) { System.out.println(user); } } @Test public void testSelectById(){ User user = this.userMapper.selectById(3L); //根據id查數據 System.out.println(user); } @Test public void testSelectByLike(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.like("name","o"); //查詢 name字段中包含‘o’的數據 List<User> users = this.userMapper.selectList(wrapper);//返回值是一個list集合 for (User u : users) { System.out.println(u); } } @Test public void testSelectByLe(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); /** * eq : 等於 * ne : 不等於 * gt : 大於 * ge : 大於等於 * lt : 小於 * le : 小於等於 * 更多查看 https://mp.baomidou.com/guide/wrapper.html#abstractwrapper */ wrapper.le("age","20"); //設置年齡小於等於20 List<User> users = this.userMapper.selectList(wrapper); for (User u : users) { System.out.println(u); } } @Test public void testSave() { User user = new User(); user.setAge(25); user.setEmail("zhangsan@qq.com"); user.setName("zhangsan"); int count = this.userMapper.insert(user); System.out.println("新增數據成功 count ==> "+count); } @Test public void testDelete(){ this.userMapper.deleteById(6L); System.out.println("刪除成功"); } @Test public void testUpdate(){ User user = new User(); user.setId(5L); user.setName("Lisi"); int i = this.userMapper.updateById(user); System.out.println(" 有"+i+"條數據被修改"); }
}
分頁查詢
需要增加分頁插件配置(這里就簡單在啟動類加 Bean)
@MapperScan("cn.itcast.mybatisplus.mapper") @SpringBootApplication public class MyApplication {
/* * 分頁插件 */ @Bean public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); } public static void main(String[] args) { SpringApplication.run(MyApplication.class,args); }
}
測試
@Test
public void testSelectPage() {
Page<User> page = new Page<>(2, 2);
IPage<User> userIPage = this.userMapper.selectPage(page, null);
System.out.println("總頁數 ----> " + userIPage.getTotal());
System.out.println("當前頁數 ----> " + userIPage.getCurrent());
System.out.println("當前每頁顯示數 ----> " + userIPage.getSize());
List<User> records = userIPage.getRecords();
for (User u: records) {
System.out.println(u);
}
}
運行結果
總頁數 ----> 5
當前頁數 ----> 2
當前每頁顯示數 ----> 2
User(id=3, name=Tom, age=28, email=test3@qq.com)
User(id=4, name=Sandy, age=21, email=test4@qq.com)