oracle+mybatis-plus+springboot项目整合

oracle+mybatis-plus+springboot项目整合

1、导包

简单项目结构:
oracle+mybatis-plus+springboot项目整合_第1张图片

    <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>

2、配置

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

3、mybatis-plus的分页配置

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;
    }

}

4、写entity,mapper,service,impl

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);
    }
}

5、测试

分页

    @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);
    }

oracle+mybatis-plus+springboot项目整合_第2张图片

你可能感兴趣的:(笔记,mybatis,spring,boot,oracle)