执行selectPage()方法时,控制台日志里面只执行了一条Sql语句输出结果也是空的

执行selectPage()方法时,控制台日志里面只执行了一条Sql语句输出结果也是空的

这个是我的输出结果

https://img1.sycdn.imooc.com//climg/6173be4809775a6319091031.jpg

这个是老师的输出结果

https://img1.sycdn.imooc.com//climg/6173bfa60994932013960739.jpg

老师的里面在执行selectPage() 方法时一共执行了两条sql语句,而我自己的只有一条,输出的结果也是空的,我在网上搜了一下,说没有配置 

PaginationInnerInterceptor
但是我是按照老师的步骤来了,请帮忙看看那出错了,怎么解决一下这个问题

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.imooc</groupId>
    <artifactId>imooc-reader</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.8</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.mybatis</groupId>-->
<!--            <artifactId>mybatis</artifactId>-->
<!--            <version>3.5.7</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.4.3.1</version>
        </dependency>
        <!-- mybatis与spring整合组件-->
<!--        <dependency>-->
<!--            <groupId>org.mybatis</groupId>-->
<!--            <artifactId>mybatis-spring</artifactId>-->
<!--            <version>2.0.6</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.8</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>
</project>
package com.imooc.reader.entity;

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

@TableName("test")
public class TestTable {
    @TableId(type = IdType.AUTO)
    @TableField("id")
    private Integer id;
    @TableField("content")
    private String content;

    public Integer getId() {
        return id;
    }

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "TestTable{" +
                "id=" + id +
                ", content='" + content + '\'' +
                '}';
    }
}
package com.imooc.reader.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imooc.reader.entity.TestTable;

public interface TestMapper extends BaseMapper<TestTable> {
    public void insertSample();
}
<?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="com.imooc.reader.mapper.TestMapper">
    <insert id="insertSample" >
        insert into test(content) values('测试类容')
    </insert>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:task="http://www.springframework.org/schema/task"
       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
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:component-scan base-package="com.imooc"/>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <mvc:default-servlet-handler/>
    <!-- Mybatis与Spring的整合配置-->
    <!--1.配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://localhost:3306/imooc_reader?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <property name="initialSize" value="5"/>
        <property name="maxActive" value="20"/>
    </bean>
    <!--2.配置SessionFactoryBean-->
    <!--SqlsessionFactoryBean用于根据配置信息创建SqlSessionFactory,不在需要自己编码创建-->
    <!--<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
    <bean id="sessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--Mybatis 插件-->
        <!--Mybatis Plus 3.4 以后版本需要在 MybatisSqlSessionFactoryBean.pluings属性中进行设置-->
        <property name="plugins">
            <array>
                <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>
    <!--3.配置Mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.imooc.reader.mapper"/>
    </bean>
    <!--声明式事务配置-->
    <!--定义事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--Mybatis Plus 3.4版本以前配置分页插件 -->
<!--    <plugins>-->
<!--        <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"></plugin>-->
<!--    </plugins>-->
</configuration>
package com.imooc.reader.mapper;

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.imooc.reader.entity.TestTable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestMapperTest {
    @Resource
    private TestMapper testMapper;

    @Test
    public void insertSample() {
        testMapper.insertSample();
    }

    @Test
    public void testInsert() {
        TestTable test = new TestTable();
        test.setContent("xxxxx");
        int count = testMapper.insert(test);
        System.out.println("本次新增" + count + "条数据");
    }

    @Test
    public void testUpdate() {
        TestTable test = new TestTable();
        test.setId(21);
        test.setContent("xxxx1");
        int i = testMapper.updateById(test);
        System.out.println("本次更新" + i + "条数据");
    }

    @Test
    public void testDelete() {
        testMapper.deleteById(21);
    }

    @Test
    public void testSelectById() {
        TestTable test = testMapper.selectById(17);
        System.out.println(test.getContent());
    }

    @Test
    public void testSelectList() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.lt("id", 20);
        wrapper.eq("content", "测试类容");
        List<TestTable> list = testMapper.selectList(wrapper);
        System.out.println(list);
        System.out.println(list.size());
    }

    @Test
    public void testPagination(){
        IPage page = new Page(1, 2);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.lt("id", 20);
        wrapper.eq("content", "测试内容");
        page = testMapper.selectPage(page, wrapper);
        System.out.println(page);
        System.out.println("总页数:" + page.getPages());
        System.out.println("总记录数:" + page.getTotal());
        System.out.println("当前页数据:" + page.getRecords());
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,在源码中进行测试,是可以查询出效果并输出两条sql语句的,如下所示:

https://img1.sycdn.imooc.com//climg/6173cd5a09eb9ec313240352.jpg

同学没有对应内容,可能是数据库中没有数据导致的,则建议同学下载资料区提供的sql文件,创建新的数据库试一下。如下所示:

https://img1.sycdn.imooc.com//climg/6173cd7e0946158804670471.jpg

祝学习愉快!

  • 九商华云 提问者 #1

    谢谢老师,执行sql文件后问题解决了。

    还有个小问题:test表一共就两个字段,id 和 content,主键自增,我用navicat创建的表,向里面执行增删改查都没问题,但执行selectPage方法就出现问题了,我建的表结构和sql文件里的表结构并没有差异,为什么会出现这样的问题

    2021-10-23 17:42:41
  • 好帮手慕小尤 回复 提问者 九商华云 #2

    同学你好,可能是数据表结构或数据的问题,同学可以先使用课程中提供的。

    祝学习愉快!

    2021-10-23 18:28:56
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师