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

转载于:https://www.cnblogs.com/JasonChen92/p/9266206.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告型管理页面显示所有公告型,在此页面既可以让管理员添加新的公告信息型,也能对已有的公告型信息执行编辑更新,失效的公告型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品型管理页面,此页面提供给管理员的功能有:新增药品型,修改药品型,删除药品型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值