<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.2version>
dependency>
<dependency>
<groupId>com.oracle.database.nlsgroupId>
<artifactId>orai18nartifactId>
<version>19.7.0.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>com.oracle.database.jdbcgroupId>
<artifactId>ojdbc8artifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
spring.datasource.driver-class-name= oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:服务名
spring.datasource.username=username
spring.datasource.password=password
mybatis-plus.mapper-locations=classpath*:/mapper/**/*Mapper.xml
logging.level.com.ycz.mapper=debug
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
package com.ycz.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author alex
* @date 2023/01/13 1:44
* @Version 1.0
*/
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 数据库的配置DbType.ORACLE 根据自己的数据库选择
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
return interceptor;
}
}
package com.ycz.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
@Data
@TableName("PRODUCT")
public class Product {
@TableField(value = "ID")
@TableId(type = IdType.ASSIGN_ID)
private String id;
@TableField(value = "NAME")
private String name;
@TableField(value = "PRICE")
private BigDecimal price;
@TableField(value = "CREATE_TIME")
private Date createTime;
}
package com.ycz.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycz.entity.Product;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductMapper extends BaseMapper<Product> {
}
package com.ycz.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ycz.entity.Product;
import com.ycz.params.ProductParams;
public interface ProductService {
IPage<Product> page(ProductParams params);
}
package com.ycz.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycz.entity.Product;
import com.ycz.mapper.ProductMapper;
import com.ycz.params.ProductParams;
import com.ycz.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper mapper;
@Override
public IPage<Product> page(ProductParams params) {
QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(params.getId()!=null , "ID" , params.getId());
queryWrapper.eq(params.getName()!=null , "NAME" , params.getName());
return mapper.selectPage(new Page<>(params.getCurrent(),params.getSize()), queryWrapper);
}
}
@Test
void page(){
ProductParams params = new ProductParams();
params.setCurrent(1);
params.setSize(2);
service.page(params).getRecords().forEach(System.out::println);
}
日志是这样的:
2023-01-14 20:57:32.793 DEBUG 13800 --- [ main] c.y.m.ProductMapper.selectPage_mpCount : ==> Preparing: SELECT COUNT(*) FROM PRODUCT
2023-01-14 20:57:32.964 DEBUG 13800 --- [ main] c.y.m.ProductMapper.selectPage_mpCount : ==> Parameters:
2023-01-14 20:57:33.105 DEBUG 13800 --- [ main] c.y.m.ProductMapper.selectPage_mpCount : <== Total: 1
2023-01-14 20:57:33.124 DEBUG 13800 --- [ main] com.ycz.mapper.ProductMapper.selectPage : ==> Preparing: SELECT * FROM ( SELECT TMP.*, ROWNUM ROW_ID FROM ( SELECT id,NAME,PRICE,CREATE_TIME FROM PRODUCT ) TMP WHERE ROWNUM <=?) WHERE ROW_ID > ?
2023-01-14 20:57:33.128 DEBUG 13800 --- [ main] com.ycz.mapper.ProductMapper.selectPage : ==> Parameters: 2(Long), 0(Long)
2023-01-14 20:57:33.154 DEBUG 13800 --- [ main] com.ycz.mapper.ProductMapper.selectPage : <== Total: 2
@Test
void insert(){
Product product = new Product();
product.setPrice(new BigDecimal("50.0"));
product.setName("可口可乐");
product.setCreateTime(new Date());
mapper.insert(product);
}
@Test
void update(){
Product product = new Product();
product.setId("1614246758689378305");
product.setPrice(new BigDecimal("50.0"));
product.setName("百事可乐");
mapper.updateById(product);
}