springboot 整合mybatis,pagehelper。测试类。

报名立减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

posted @ 2018-07-05 01:15  JasonChen92  阅读(693)  评论(0编辑  收藏  举报