1. 程式人生 > 實用技巧 >Spring整合MyBatis-Plus

Spring整合MyBatis-Plus

Spring整合MyBatis-Plus

maven依賴

<dependencies>
    <!-- 單元測試 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
    </dependency>
    <!-- spring start -->
    <!--Spring核心依賴 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- Spring beans包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!--Spring aspects依賴 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- 切入點表示式解析 -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.6</version>
    </dependency>
    <!-- aopalliance -->
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>
    <!--Spring aop依賴 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- Spring 容器包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- Spring 事務管理 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- spring end -->
    <!-- mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
    </dependency>
    <!-- mybatis-plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus</artifactId>
        <version>3.4.1</version>
    </dependency>
    <!-- 資料庫連線驅動 -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3.0</version>
    </dependency>
    <!-- Druid資料庫連線池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.3</version>
    </dependency>
    <!-- startLog -->
    <!-- slf4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.0-alpha1</version>
    </dependency>
    <!-- logback -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.3.0-alpha5</version>
    </dependency>
    <!-- endLog -->
</dependencies>

<!-- 指定java版本 -->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <encoding>UTF-8</encoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

配置檔案

logback.xml

我這裡使用的日誌是logback

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoder 預設配置為PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

jdbc.properties

使用的資料庫連線池是Druid

jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
jdbc.username=ycy
jdbc.password=root
# 初始化連線數量
jdbc.initialSize=5
# 最大連線數
jdbc.maxActive=10
# 最大等待時間
jdbc.maxWait=3000

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 掃描service包下的註解 -->
    <context:component-scan base-package="cn.yecaiyu.bill.service"/>

    <!-- 匯入外部資料庫配置 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 建立Druid資料來源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="maxWait" value="${jdbc.maxWait}"/>
    </bean>

    <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <!-- 引入資料來源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 引入mybatisConfig -->
        <property name="configuration" ref="configuration"/>
        <!-- MyBaits 別名包掃描路徑,通過該屬性可以給包中的類註冊別名,註冊後在 Mapper 對應的 XML 檔案中可以直接使用類名,而不用使用全限定的類名(即 XML 中呼叫的時候不用包含包名) -->
        <property name="typeAliasesPackage" value="cn.yecaiyu.*.pojo"/>
        <!-- 掃描xml檔案 -->
        <property name="mapperLocations">
            <list>
                <value>classpath:com/zte/bill/mapper/*.xml</value>
            </list>
        </property>
        <!-- 配置分頁外掛(已廢棄) -->
        <!-- <property name="plugins">
             <array>
                 <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/>
             </array>
         </property>-->
        <property name="plugins">
            <array>
                <!-- 注入MybaitisPlus外掛-->
                <bean class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
                    <property name="interceptors">
                        <list>
                            <!-- 分頁外掛 -->
                            <bean class="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"/>
                        </list>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!-- 配置mybatisConfig裡的Setting -->
    <bean id="configuration" class="com.baomidou.mybatisplus.core.MybatisConfiguration">
        <!-- 	指定 MyBatis 應如何自動對映列到欄位或屬性。
                    NONE 表示關閉自動對映;
                    PARTIAL 只會自動對映沒有定義巢狀結果對映的欄位。
                    FULL 會自動對映任何複雜的結果集(無論是否巢狀)。
        -->
        <property name="autoMappingBehavior" value="FULL"/>
        <!-- 是否開啟駝峰命名自動對映,即從經典資料庫列名 A_COLUMN 對映到經典 Java 屬性名 aColumn。 -->
        <property name="mapUnderscoreToCamelCase" value="true"/>
        <!-- Oracle一定要設定為NULL 預設:OTHER,Oracle識別不出OTHER -->
        <property name="jdbcTypeForNull" value="NULL"/>
    </bean>

    <!-- 掃描mapper介面 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.yecaiyu.*.mapper"/>
    </bean>

    <!-- 配置事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 註解方式配置事物 -->
    <tx:annotation-driven/>
</beans>

測試

資料庫表

使用的是Oracle資料庫

create table SMBMS_BILL
(
  id           NUMBER(20) not null,
  billcode     VARCHAR2(20),
  productname  VARCHAR2(30),
  productdesc  VARCHAR2(50),
  productunit  VARCHAR2(10),
  productcount NUMBER(20,2),
  totalprice   NUMBER(20,2),
  ispayment    NUMBER(10),
  createdby    NUMBER(20),
  creationdate DATE,
  modifyby     NUMBER(20),
  modifydate   DATE,
  providerid   NUMBER(20)
)
tablespace SYSTEM
  pctfree 10
  pctused 40
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
comment on column SMBMS_BILL.id
    is '主鍵ID';
comment on column SMBMS_BILL.billcode
    is '賬單編碼';
comment on column SMBMS_BILL.productname
    is '商品名稱';
comment on column SMBMS_BILL.productdesc
    is '商品描述';
comment on column SMBMS_BILL.productunit
    is '商品單位';
comment on column SMBMS_BILL.productcount
    is '商品數量';
comment on column SMBMS_BILL.totalprice
    is '商品總額';
comment on column SMBMS_BILL.ispayment
    is '是否支付(1:未支付 2:已支付)';
comment on column SMBMS_BILL.createdby
    is '建立者(userId)';
comment on column SMBMS_BILL.creationdate
    is '建立時間';
comment on column SMBMS_BILL.modifyby
    is '更新者(userId)';
comment on column SMBMS_BILL.modifydate
    is '更新時間';
comment on column SMBMS_BILL.providerid
    is '供應商ID';
alter table SMBMS_BILL
    add primary key (ID)
        using index
            tablespace SYSTEM
            pctfree 10
            initrans 2
            maxtrans 255
            storage
            (
                initial 64K
                next 1M
                minextents 1
                maxextents unlimited
            );

insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (1, 'BILL2016_001', '洗髮水、護髮素', '日用品-洗髮、護髮', '瓶', 500, 25000, 2, 1, to_date('14-12-2014 13:02:03', 'dd-mm-yyyy hh24:mi:ss'), null, null, 13);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (2, 'BILL2016_002', '香皂、肥皂、藥皂', '日用品-皁類', '塊', 1000, 10000, 2, 1, to_date('23-03-2016 04:20:40', 'dd-mm-yyyy hh24:mi:ss'), null, null, 13);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (3, 'BILL2016_003', '大豆油', '食品-食用油', '斤', 300, 5890, 2, 1, to_date('14-12-2014 13:02:03', 'dd-mm-yyyy hh24:mi:ss'), null, null, 6);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (4, 'BILL2016_004', '橄欖油', '食品-進口食用油', '斤', 200, 9800, 2, 1, to_date('10-10-2013 03:12:13', 'dd-mm-yyyy hh24:mi:ss'), null, null, 7);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (5, 'BILL2016_005', '洗潔精', '日用品-廚房清潔', '瓶', 500, 7000, 2, 1, to_date('14-12-2014 13:02:03', 'dd-mm-yyyy hh24:mi:ss'), null, null, 9);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (6, 'BILL2016_006', '美國大杏仁', '食品-堅果', '袋', 300, 5000, 2, 1, to_date('14-04-2016 06:08:09', 'dd-mm-yyyy hh24:mi:ss'), null, null, 4);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (7, 'BILL2016_007', '沐浴液、精油', '日用品-沐浴類', '瓶', 500, 23000, 1, 1, to_date('22-07-2016 10:10:22', 'dd-mm-yyyy hh24:mi:ss'), null, null, 14);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (8, 'BILL2016_008', '不鏽鋼盤碗', '日用品-廚房用具', '個', 600, 6000, 2, 1, to_date('14-04-2016 05:12:13', 'dd-mm-yyyy hh24:mi:ss'), null, null, 14);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (9, 'BILL2016_009', '塑料杯', '日用品-杯子', '個', 350, 1750, 2, 1, to_date('04-02-2016 11:40:20', 'dd-mm-yyyy hh24:mi:ss'), null, null, 14);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (10, 'BILL2016_010', '豆瓣醬', '食品-調料', '瓶', 200, 2000, 2, 1, to_date('29-10-2013 05:07:03', 'dd-mm-yyyy hh24:mi:ss'), null, null, 8);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (11, 'BILL2016_011', '海之藍', '飲料-國酒', '瓶', 50, 10000, 1, 1, to_date('14-04-2016 16:16:00', 'dd-mm-yyyy hh24:mi:ss'), null, null, 1);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (12, 'BILL2016_012', '芝華士', '飲料-洋酒', '瓶', 20, 6000, 1, 1, to_date('09-09-2016 17:00:00', 'dd-mm-yyyy hh24:mi:ss'), null, null, 1);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (13, 'BILL2016_013', '長城紅葡萄酒', '飲料-紅酒', '瓶', 60, 800, 2, 1, to_date('14-11-2016 15:23:00', 'dd-mm-yyyy hh24:mi:ss'), null, null, 1);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (14, 'BILL2016_014', '泰國香米', '食品-大米', '斤', 400, 5000, 2, 1, to_date('09-10-2016 15:20:00', 'dd-mm-yyyy hh24:mi:ss'), null, null, 3);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (15, 'BILL2016_015', '東北大米', '食品-大米', '斤', 600, 4000, 2, 1, to_date('14-11-2016 14:00:00', 'dd-mm-yyyy hh24:mi:ss'), null, null, 3);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (16, 'BILL2016_016', '可口可樂', '飲料', '瓶', 2000, 6000, 2, 1, to_date('27-03-2012 13:03:01', 'dd-mm-yyyy hh24:mi:ss'), null, null, 2);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (17, 'BILL2016_017', '脈動', '飲料', '瓶', 1500, 4500, 2, 1, to_date('10-05-2016 12:00:00', 'dd-mm-yyyy hh24:mi:ss'), null, null, 2);
insert into SMBMS_BILL (id, billcode, productname, productdesc, productunit, productcount, totalprice, ispayment, createdby, creationdate, modifyby, modifydate, providerid)
values (18, 'BILL2016_018', '哇哈哈', '飲料', '瓶', 2000, 4000, 2, 1, to_date('24-11-2015 15:12:03', 'dd-mm-yyyy hh24:mi:ss'), null, null, 2);
commit;

alter table SMBMS_BILL enable all triggers;

實體類

package cn.yecaiyu.bill.pojo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.util.Date;

/**
 * @author YeCaiYu
 * @date 2020-11-19 15:13:09
 */
@TableName(value = "SMBMS_BILL")
public class Bill {
    /**
     * 主鍵ID
     */
    @TableId
    private Integer id;

    /**
     * 賬單編碼
     */
    @TableField(value = "BILLCODE")
    private String billCode;
    /**
     * 商品名稱
     */
    @TableField(value = "PRODUCTNAME")
    private String productName;

    /**
     * 商品描述
     */
    @TableField(value = "PRODUCTDESC")
    private String productDesc;

    /**
     * 商品單位
     */
    @TableField(value = "PRODUCTUNIT")
    private String productUnit;

    /**
     * 商品數量
     */
    @TableField(value = "PRODUCTCOUNT")
    private Integer productCount;

    /**
     * 商品總額
     */
    @TableField(value = "TOTALPRICE")
    private Integer totalPrice;

    /**
     * 是否支付(1:未支付 2:已支付)
     */
    @TableField(value = "ISPAYMENT")
    private Integer isPayment;

    /**
     * 建立者(userId)
     */
    @TableField(value = "CREATEDBY")
    private Integer createdBy;

    /**
     * 建立時間
     */
    @TableField(value = "CREATIONDATE")
    private Date creationDate;

    /**
     * 更新者(userId)
     */
    @TableField(value = "MODIFYBY")
    private Integer modifyBy;

    /**
     * 更新時間
     */
    @TableField(value = "MODIFYDATE")
    private Date modifyDate;

    /**
     * 供應商ID
     */
    @TableField(value = "PROVIDERID")
    private Integer providerId;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBillCode() {
        return billCode;
    }

    public void setBillCode(String billCode) {
        this.billCode = billCode;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }

    public String getProductUnit() {
        return productUnit;
    }

    public void setProductUnit(String productUnit) {
        this.productUnit = productUnit;
    }

    public Integer getProductCount() {
        return productCount;
    }

    public void setProductCount(Integer productCount) {
        this.productCount = productCount;
    }

    public Integer getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Integer totalPrice) {
        this.totalPrice = totalPrice;
    }

    public Integer getIsPayment() {
        return isPayment;
    }

    public void setIsPayment(Integer isPayment) {
        this.isPayment = isPayment;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Integer getModifyBy() {
        return modifyBy;
    }

    public void setModifyBy(Integer modifyBy) {
        this.modifyBy = modifyBy;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    public Integer getProviderId() {
        return providerId;
    }

    public void setProviderId(Integer providerId) {
        this.providerId = providerId;
    }

    @Override
    public String toString() {
        return "Bill{" +
                "id=" + id +
                ", billCode='" + billCode + '\'' +
                ", productName='" + productName + '\'' +
                ", productDesc='" + productDesc + '\'' +
                ", productUnit='" + productUnit + '\'' +
                ", productCount=" + productCount +
                ", totalPrice=" + totalPrice +
                ", isPayment=" + isPayment +
                ", createdBy=" + createdBy +
                ", creationDate=" + creationDate +
                ", modifyBy=" + modifyBy +
                ", modifyDate=" + modifyDate +
                ", providerId=" + providerId +
                '}';
    }
}

Mapper

介面

package cn.yecaiyu.bill.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import cn.yecaiyu.bill.pojo.Bill;

/**
 * @author YeCaiYu
 * @date 2020-11-19 15:13:09
 */
public interface IBillMapper  extends BaseMapper<Bill> {
}

xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.yecaiyu.bill.mapper.IBillMapper">
    <resultMap id="BaseResultMap" type="cn.yecaiyu.bill.pojo.Bill">
        <[email protected]>
        <!--@Table SMBMS_BILL-->
        <id column="ID" jdbcType="DECIMAL" property="id"/>
        <result column="BILLCODE" jdbcType="VARCHAR" property="billCode"/>
        <result column="PRODUCTNAME" jdbcType="VARCHAR" property="productName"/>
        <result column="PRODUCTDESC" jdbcType="VARCHAR" property="productDesc"/>
        <result column="PRODUCTUNIT" jdbcType="VARCHAR" property="productUnit"/>
        <result column="PRODUCTCOUNT" jdbcType="DECIMAL" property="productCount"/>
        <result column="TOTALPRICE" jdbcType="DECIMAL" property="totalPrice"/>
        <result column="ISPAYMENT" jdbcType="DECIMAL" property="isPayment"/>
        <result column="CREATEDBY" jdbcType="DECIMAL" property="createdBy"/>
        <result column="CREATIONDATE" jdbcType="TIMESTAMP" property="creationDate"/>
        <result column="MODIFYBY" jdbcType="DECIMAL" property="modifyBy"/>
        <result column="MODIFYDATE" jdbcType="TIMESTAMP" property="modifyDate"/>
        <result column="PROVIDERID" jdbcType="DECIMAL" property="providerId"/>
    </resultMap>
    <sql id="Base_Column_List">
        <[email protected]>
        ID, BILLCODE, PRODUCTNAME, PRODUCTDESC, PRODUCTUNIT, PRODUCTCOUNT, TOTALPRICE, ISPAYMENT,
        CREATEDBY, CREATIONDATE, MODIFYBY, MODIFYDATE, PROVIDERID
    </sql>
</mapper>

service

介面

package cn.yecaiyu.bill.service;

import com.baomidou.mybatisplus.extension.service.IService;
import cn.yecaiyu.bill.pojo.Bill;

/**
 * @author YeCaiYu
 * @date 2020-11-19 15:29:42
 */
public interface IBillService extends IService<Bill> {
}

實現類

package cn.yecaiyu.bill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.yecaiyu.bill.mapper.IBillMapper;
import cn.yecaiyu.bill.pojo.Bill;
import cn.yecaiyu.bill.service.IBillService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author YeCaiYu
 * @date 2020-11-19 15:29:56
 */
@Service
public class BillService extends ServiceImpl<IBillMapper, Bill> implements IBillService {

}

測試類

package cn.yecaiyu.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.yecaiyu.bill.pojo.Bill;
import cn.yecaiyu.bill.service.IBillService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author YeCaiYu
 * @date 2020-11-19 18:53:34
 */
public class TestMain {
    @Test
    public void test01() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        IBillService billService = (IBillService) context.getBean("billService");
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
//        wrapper.eq("ID", 1);
//        List<Bill> bills = billService.list(wrapper);
//        bills.forEach(System.out::println);
        Page<Bill> page = new Page<>(1, 5);
        Page<Bill> billPage = billService.page(page, wrapper);
        billPage.getRecords().forEach(System.out::println);
    }
}

目錄結構

  • src
    • main
      • java
        • cn.yecaiyu.bill
          • mapper
            • IBillMapper.java
          • pojo
            • Bill.java
          • service
            • impl
              • BillService.java
            • IBillService.java
      • resources
        • cn/yecaiyu/bill/mapper
          • IBillMapper.xml
        • applicationContext.xml
        • jdbc.properties
        • logback.xml
    • test
      • cn.yecaiyu.bill.test
        • TestMain.java