1.搭建springboot工程
略.
2.引入相应依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
</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>
3.properties文件配置
server.port=8088
spring.datasource.name=demo
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123
#sql语句打印
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4.mybatis 分页配置
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
5.创建mapper以及实体类
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
@Data
public class User {
@TableId(value = "id",type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date createTime;
}
建表语句
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) not null auto_increment COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(30) NULL DEFAULT NULL COMMENT '邮箱',
create_time DATETIME(0) NULL DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (id)
);
6.分页带条件查询
备注:增删改比较简单,不做演示,附上测试代码
@SpringBootTest
public class MybatisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void queryUserForPage() {
IPage<User> userPage = new Page<>(1, 10);//参数一是当前页,参数二是每页个数
userPage = userMapper.selectPage(userPage, null);
List<User> list = userPage.getRecords();
for (User user : list) {
System.out.println(user);
}
}
@Test
public void insert() {
for (int i = 0; i <= 35; i++) {
User queryInfo = new User();
queryInfo.setName("wang" + (i + 1));
queryInfo.setAge(Integer.parseInt(i + "") + 1);
queryInfo.setEmail("1021558365@qq.com ---> " + (i + 1));
userMapper.insert(queryInfo);
}
}
}
controller代码
@RestController
public class MyBatisController {
@Resource
private UserMapper userMapper;
@RequestMapping("/list")
public Object list(@RequestParam("pageNo") Integer pageNo,
@RequestParam("pageSize") Integer pageSize){
// 参数一是当前页,参数二是每页个数
IPage<User> userPage = new Page<>(pageNo, pageSize);
QueryWrapper<User> queryWrapper = new QueryWrapper();
// queryWrapper.and(wrapper -> wrapper.eq("name","wang1").
// or().eq("name", "wang2").
// or().eq("name", "wang3"));
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,5);
queryWrapper.gt("create_time",new Date());
queryWrapper.lt("create_time",calendar.getTime());
userPage = userMapper.selectPage(userPage, queryWrapper);
List<User> list = userPage.getRecords();
return list;
}
@RequestMapping("/insert")
public Object insert(){
int num = 0;
for (int i = 0; i <= 35; i++) {
Calendar calendar = Calendar.getInstance();
User queryInfo = new User();
queryInfo.setName("wang" + (i + 1));
queryInfo.setAge(Integer.parseInt(i + "") + 1);
queryInfo.setEmail("1021558365@qq.com ---> " + (i + 1));
calendar.add(Calendar.DAY_OF_MONTH,(i + 1));
queryInfo.setCreateTime(calendar.getTime());
num += userMapper.insert(queryInfo);
}
return num;
}
}
使用restclient测试
1.带时间条件分页查询
2. and or eq 条件查询