报名立减200元。暑假直降6888。

遇到的异常。

1.这是在使用mybatis成功连接数据库后,通过service层调用dao层出现的异常。

异常原因:在启动类上面的注解@MapperScan没有指定到dao层的包名上。

错误写法:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
//开启通用注解扫描
@MapperScan(basePackages = {"com.chenxin.springboot_0702"})  //因为这里没有指定到dao层的包名上,所以报错 invalid bound statement (not found).
@EnableAutoConfiguration
public class Run { public static void main(String[] args) {
SpringApplication.run(Run.class, args);
}
}

正确写法:

@SpringBootApplication
//开启通用注解扫描
@MapperScan(basePackages = {"com.chenxin.springboot_0702.dao"})  //指定到dao层的包名。
@EnableAutoConfiguration
public class Run { public static void main(String[] args) {
SpringApplication.run(Run.class, args);
}
}

2、定义了相同的Controller类名。是之前学习时,添加的类。

3、单元测试是更新到相同记录。

4、使用pagehelper时,浏览器测试路径没有传参。

正确写法:http://localhost:8080/getAll?pageNum=1&pageSize=5。参数名要和方法参数名相同。

*****************************************异常就这些了。***************************************************

正题代码:

接口中分页查询的方法:

import com.chenxin.springboot_0702.model.User;
import java.util.List; public interface UserService { //分页查询方法
public List<User> find(int pageNum, int pageSize) throws Exception; //查询
public List<User> findAll() throws Exception; public User findById(long id) throws Exception; public List<User> findByPhone(String phone) throws Exception; //新增
public long save(User user) throws Exception; //删除
public boolean delete(long id) throws Exception; //更新
public boolean update(User user) throws Exception;
}

实现类的方法:

import com.chenxin.springboot_0702.dao.UserMapper;
import com.chenxin.springboot_0702.model.User;
import com.chenxin.springboot_0702.service.UserService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper; import javax.annotation.Resource;
import java.util.List; @Service
public class UserServiceImpl implements UserService { @Resource
UserMapper userMapper;
@Override
public List<User> find(int pageNum, int pageSize) throws Exception {
//分页查询:查询第一页,每页10行。
PageHelper.startPage(pageNum, pageSize);
List<User> users =userMapper.findAll();
return users;
} //查询
@Override
public List<User> findAll() throws Exception {
return userMapper.findAll();
} @Override
public User findById(long id) throws Exception {
return userMapper.findById(id);
} @Override
public List<User> findByPhone(String phone) throws Exception {
return userMapper.findByPhone(phone);
} //新增
@Override
public long save(User user) throws Exception {
userMapper.save(user);
return user.getId();
} //删除
@Override
public boolean delete(long id) throws Exception {
return userMapper.delete(id);
} //更新
@Override
public boolean update(User user) throws Exception {
return userMapper.update(user);
}
}

controller调用:

import com.chenxin.springboot_0702.model.User;
import com.chenxin.springboot_0702.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class HelloController2 { @Autowired
private UserService userService; @RequestMapping("/getAll")
public List<User> getAll(int pageNum, int pageSize) throws Exception{
return userService.find(pageNum,pageSize);
}
}

启动类:

package com.chenxin.springboot_0702;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
//开启通用注解扫描
@MapperScan(basePackages = {"com.chenxin.springboot_0702.dao"})
@EnableAutoConfiguration
public class Run { public static void main(String[] args) {
SpringApplication.run(Run.class, args);
}
}

配置文件application.properties:

#数据库访问配置
#主数据源
#配置mysql的连接配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/testx?useSSL=false
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver #连接池设置
#初始化大小,最大,最小值
spring.datasource.initialize=true
spring.datasource.tomcat.min-idle=5
spring.datasource.tomcat.max-active=20
spring.datasource.tomcat.max-wait=60000 logging.level.com.chenxin.springboot_0702.Run=debug
logging.path=logs pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true

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.chenxin</groupId>
<artifactId>springboot_0702</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot_0702</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--junit不需要springboot已经自动加载了。-->
<!--spring-boot-starter-jdbc已经包含在了mybatis-spring-boot-starter中了。--> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency> <!--引入mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!--mybatis分页插件-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

好了结束了。最后运行启动类。

浏览器:http://localhost:8080/getAll?pageNum=1&pageSize=5

运行结果:

报名立减200元。暑假直降6888。

邀请链接:http://www.jnshu.com/login/1/20535344

邀请码:20535344

springboot 整合mybatis,pagehelper。测试类。的更多相关文章

  1. springboot整合mybatis+pageHelper

    springboot整合mybatis+pageHelper 〇.搭建sporingboot环境,已经整合mybatis环境,本篇主要是添加pageHelper工具 一.添加依赖 <!-- 分页 ...

  2. SpringBoot 整合Mybatis + PageHelper 实现分页

    前言: 现在公司大多数都实现了前后端分离,前端使用Vue.React.AngularJS 等框架,不用完全依赖后端.但是如果对于比较小型的项目,没必要前后端分离,而SpringBoot也基本抛弃了Js ...

  3. 学习springboot整合mybatis并编写测试类

    报名立减200元.暑假直降6888. 邀请链接:http://www.jnshu.com/login/1/20535344 邀请码:20535344 遇到的问题: 1.原因是在启动类上只有一个@Map ...

  4. SpringBoot整合mybatis使用pageHelper插件进行分页操作

    SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看 ...

  5. springboot整合mybatis(SSM开发环境搭建)

    0.项目结构: ---------------------方法一:使用mybatis官方提供的Spring Boot整合包实现--------------------- 1.application.p ...

  6. SpringBoot整合Mybatis关于分页查询的方法

    最近公司在用到SpringBoot整合Mybatis时当web端页面数据增多时需要使用分页查询以方便来展示数据.本人对分页查询进行了一些步骤的总结,希望能够帮助到有需要的博友.如有更好的方式,也希望评 ...

  7. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  8. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  9. SpringBoot整合系列-PageHelper分页插件

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971043.html SpringBoot整合MyBatis分页插件PageHelper ...

  10. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

随机推荐

  1. delphi 判断目录是否可写

    FUNCTION WritableDir(CONST Dir : STRING) : BOOLEAN; VAR FIL : FILE; N : STRING; I : Cardinal; BEGIN ...

  2. Win8 Metro(C#)数字图像处理--2.53图像傅立叶变换

    原文:Win8 Metro(C#)数字图像处理--2.53图像傅立叶变换  [函数名称] 1,一维FFT变换函数         Complex[] FFT(Complex[] sourceDat ...

  3. Visual Studio 2017报表RDLC设计器与工具箱中Report Viewer问题

    原文:VS2017入门 RDLC入门之01 本系列所有内容为网络收集转载,版权为原作者所有. VS2017初始安装后和VS2015一样,都没有ReportDesigner/ReportViewer R ...

  4. C# 设置IP地址及设置自动获取IP

    原文:C# 设置IP地址及设置自动获取IP </pre><pre name="code" class="csharp">1.添加引用&q ...

  5. Qt的QWaitCondition(允许线程在一定条件下唤醒其他线程,这样对不间断上传可能比较适用)

    对生产者和消费者问题的另一个解决办法是使用QWaitCondition,它允许线程在一定条件下唤醒其他线程.其中wakeOne()函数在条件满足时随机唤醒一个等待线程,而wakeAll()函数则在条件 ...

  6. web的seo

    摘要:搜索引擎优化是一种具有很高技术性的活动,也是一种营销功能,必须将它作为营销活动处理.SEO从业者必须理解公司的服务.产品.总体业务战略.竞争形势.品牌.未来网站开发目标和相关的业务构成. SEO ...

  7. java关键字-interface

    1:是用关键字interface定义的. 2:接口中包含的成员,最常见的有全局常量.抽象方法. 注意:接口中的成员都有固定的修饰符. 成员变量:public static final 成员方法:pub ...

  8. Python字典的合并与拆分

    1.字典的合并 dict1={1:[1,11,111],2:[2,22,222]} dict2={3:[3,33,333],4:[4,44,444]} dictMerged2=dict(dict1, ...

  9. MySQL如何转SQL server

    MySql的数据如何导入到Sql Server数据库中?(转载)   本文咱讨论的重点是如何将MySql数据导入到Sql Server数据库中,所以就需要机器上同时安装了MySql和Sql Serve ...

  10. Kali Linux Web渗透测试手册(第二版) - 1.3 - 靶机的安装

    Kali Linux Web渗透测试手册(第二版) - 1.3 - 靶机的安装  一.配置KALI Linux和渗透测试环境 在这一章,我们将覆盖以下内容: 在Windows和Linux上安装Virt ...