报名立减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. 深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数

    原文:深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数 概述 COLUMNS_UPDATED函数能够出现在INSERT或UPDATE触发器中AS关键字后的任何位置,用来 ...

  2. PowerDesigner 在通过jdbc连接数据库时 Could not Initialize JavaVM!

    最近用到PowerDesigner的逆向工程,从数据库中逆向生成模型,本想使用odbc连接的,但是需要安装驱动,mysql的还好弄,oracle对我来讲实在是有些麻烦,看到能用jdbc连接,就想试试, ...

  3. flume本地调试

    本机idea远程调试flume:https://blog.csdn.net/u012373815/article/details/60601118 遇到 [root@hadoop02 bin]# ./ ...

  4. 海康SDK编程指南

    转至心澄欲遣 目前使用的海康SDK包括IPC_SDK(硬件设备),Plat_SDK(平台),其中两套SDK都需单独调用海康播放库PlayCtrl.dll来解码视频流,返回视频信息和角度信息.本文仅对视 ...

  5. delphi如何输出当前堆栈

    想实现,输出当前运行的堆栈,有会的吗?给点思路 方法很多,参考: https://bitbucket.org/shadow_cs/delphi-leakcheck/ 的 https://bitbuck ...

  6. Delphi子类调用祖父类的虚函数

    因为看Delphi书的时候,就产生了疑惑.老讲调用父类虚函数,但是万一需要调用祖父虚函数怎么办?后来又经常在C++里看到,就更疑惑了 type TA = class procedure ShowMsg ...

  7. 闰平年简介及计算过程描述 - Java代码实现

    import java.util.Scanner; /** * @author Shelwin Wei * 分析过程请参照<闰平年简介及计算过程描述>,网址 http://www.cnbl ...

  8. JAVA 拼接了一个sql 语句,但是最后运行报错——SQL 命令未正确结束

    错误原因: 拼接的时候因为引号里的部分是直接引起来的,所以将这些语句整个拼接起来的时候就会成为一个“没有断句”的sql语句,如下面我的错误 将整句话拼接起来就相当于   select * from B ...

  9. STL-空间配置器、迭代器、traits编程技巧

    目录 内存分配和释放 对象的构造和析构 traits要解决的问题 内嵌类别声明解决非指针迭代器的情况 使用模板特例化解决普通指针的情况 迭代器相应类别 内存分配和释放 STL中有两个分配器,一级分配器 ...

  10. linux上java和golang环境变量的设置

    JAVA环境变量   (1).打开~/.bashrc完成环境配置( 作用类似于/etc/bashrc, 只是针对用户自己而言,不对其他用户生效.)       文件追加            expo ...