报名立减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. 2018-4-25 1.如何在GitHub上新建一个新的项目并下载该项目及如何提交新的文件

  2. murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法

    murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法 1.DJB HASH算法 1 2 3 4 5 6 7 8 9 10 11 /* the famous DJB Hash ...

  3. Win10《芒果TV - Preview》官方指定预览版 - 重要使用注意事项

    Win10<芒果TV - Preview>官方指定预览版,最新的改进和功能更新将会此版本优先体验. 重要使用注意事项: 1.因为方便过审核,默认将会员相关的操作提示简化: 2.使用中务必手 ...

  4. 什么是TOML?

    配置文件的使用由来已久,从.ini.XML.JSON.YAML再到TOML,语言的表达能力越来越强,同时书写便捷性也在不断提升. TOML是前GitHub CEO, Tom Preston-Werne ...

  5. 跨进程访问VCL的一个用例(Delphi6、TurboDelphi测试通过)

    Controls.pas单元中有一个FindControl函数,通过句柄获得对应的TWinControl对象. function FindControl(Handle: HWnd): TWinCont ...

  6. 用友的BS专用浏览器方案

    T+的这个BS中的B是自己的专用浏览器,这样有以下好处 1.避免了公用浏览器比如IE 里的其它插件的干扰2.避免了各个操作系统不同版本和不同种类浏览器的兼容问题,且只需要维护一个版本3.避免了共用浏览 ...

  7. 介绍两种Timer定时器的使用

    第一种, 直接实例化Timer类,设置时间间隔,到达时间后执行想要执行的事件.代码示例: using System; using System.Collections.Generic; using S ...

  8. Android WebView设置背景透明

    Adndroid 2.x的设置 在Android 2.x下,设置webview背景为透明的方法: wvContent.setBackgroundColor(0); Adndroid 4.0 由于硬件加 ...

  9. Hadoop 学习之路(一)—— 分布式文件系统 HDFS

    一.介绍 HDFS (Hadoop Distributed File System)是Hadoop下的分布式文件系统,具有高容错.高吞吐量等特性,可以部署在低成本的硬件上. 二.HDFS 设计原理 2 ...

  10. jsp路径兼容

    jsp头 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&q ...