springboot2.x已经发布一段时间,博主在这里使用springboot2.1.7整合mybatis3.5.2,使用的数据库为mysql8.0.13

1. 导入依赖

<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--mybatis-spring-boot-starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>

2. 在application.properties配置数据源(此文件会被springboot自动扫描)

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=*********
#mysql8以上版本使用这个
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis读取根目录下mapper文件夹下的所有以.xml结尾的文件
mybatis.mapper-locations=classpath:/mapper/*.xml

3. 编写controller,service,dao,mapper.xml

AdminController:

package com.gl.pin.web.controller;

import com.gl.pin.service.api.system.entity.AdminEntity;
import com.gl.pin.service.api.system.res.DubboRes;
import com.gl.pin.service.api.system.service.AdminServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import java.util.ArrayList;
import java.util.List; /**
* @Auther: zjk
* @Date: 2019/9/3
* @Description:
*/ @ResponseBody
@Controller
@RequestMapping(value = "/admin")
public class AdminController { @Autowired
AdminServiceI adminServiceI; @PostMapping(value = "/login")
public AdminEntity login(@RequestParam(value = "userName") String userName , @RequestParam (value = "password") String password){
return adminServiceI.login(userName,password);
} }

AdminService:(AdminServiceI 自行脑补)

package com.gl.pin.service.system.service;

import com.gl.pin.service.api.system.entity.AdminEntity;
import com.gl.pin.service.api.system.res.DubboRes;
import com.gl.pin.service.api.system.service.AdminServiceI;
import com.gl.pin.service.system.dao.AdminDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @Auther: zjk
* @Date: 2019/9/4
* @Description:
*/
@Service
public class AdminService implements AdminServiceI { @Autowired
AdminDao adminDao; @Override
public AdminEntity login(String userName , String password) {
return adminDao.login(userName,password);
}
}

AdminDao:

package com.gl.pin.service.system.dao;

import com.gl.pin.service.api.system.entity.AdminEntity;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository; /**
* @Auther: zjk
* @Date: 2019/9/4
* @Description:
*/
@Repository
@Mapper
public interface AdminDao { /**
* 管理员登录
* @param userName
* @param password
* @return AdminEntity
*/
AdminEntity login(String userName,String password);
}

resoures/mapper/AdminDao.xml(映射文件):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gl.pin.service.system.dao.AdminDao">
<!--AdminEntity与数据库中的列的映射-->
<resultMap id="adminDao" type="com.gl.pin.service.api.system.entity.AdminEntity">
<id property="id" column="a_id"/>
<result property="userName" column="a_userName"/>
<result property="password" column="a_password"/>
</resultMap> <!--admin login -->
<select id="login1" parameterType="java.lang.String" resultMap="adminDao">
SELECT a_id,a_userName,a_password FROM sys_admin WHERE a_userName=#{userName} AND a_password=#{password}
</select>
</mapper>

4. 如何将dao装配在spring容器中?

方法一:在dao接口类上加上注解@Mapper
@Repository
@Mapper
public interface AdminDao { /**
* 管理员登录
* @param userName
* @param password
* @return AdminEntity
*/
AdminEntity login(String userName,String password); }
方法二:在springboot启动类上加注解@MapperScan
@SpringBootApplication
@MapperScan("com.gl.pin.service.system.dao")
public class PinServiceSystemApplication { public static void main(String[] args) {
SpringApplication.run(PinServiceSystemApplication.class, args);
} }

5. 报错总结

1.springboot启动失败---找不到接口类

错误详情:

Description:

Field adminDao in com.gl.pin.service.system.service.AdminService required a bean of type 'com.gl.pin.service.system.dao.AdminDao' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.gl.pin.service.system.dao.AdminDao' in your configuration.

解决方案:没有使用@Mapper或@MapperScan,或者错误使用(@MapperScan扫描包的路径不对等等)

2. springboot启动成功,发送请求报错---dao和mapper.xml绑定失败,找不到对应方法

错误详情:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.gl.pin.service.system.dao.AdminDao.login

解决方案:

2.1 dao接口所在包与mapper.xml映射文件中的namespace不一致

<mapper namespace="com.gl.pin.service.system.dao.AdminDao">

2.2 dao中有的方法,但mapper.xml中没有
2.3 dao中的方法名和mapper.xml中id的不一样

<select id="login" parameterType="java.lang.String" resultMap="adminDao">

 2.4 mapper.xml文件压根没被解析,需要在application.properties中配置

#最好使用"/",用"."可能在多级文件情况下有问题
mybatis.mapper-locations=classpath:/mapper/*.xml

springboot整合mybatis肯定会有这样那样的问题,这些错误总结就是博主踩过的大坑,特别贴出来供大家参考。

springboot2.1.7整合mybati3.5.2与mysql8.0.13的更多相关文章

  1. SpringBoot2.1.6 整合CXF 实现Webservice

    SpringBoot2.1.6 整合CXF 实现Webservice 前言 最近LZ产品需要对接公司内部通讯工具,采用的是Webservice接口.产品框架用的SpringBoot2.1.6,于是采用 ...

  2. SpringBoot2.2.5整合ElasticSearch7.9.2

    1:前言 为什么是SpringBoot2.2.5,不是其他的SpringBoot版本,原因有两个: 1:SpringBoot2.2.0以上才能支持ElasticSearch7.x版本. 2:Sprin ...

  3. SpringBoot整合MyBatis与MySql8.0

    一.前言 之前已经有一篇文章讨论过SpringBoot整合MyBatis,因而此篇不在重复累赘,本文主要是最新版的SpringBoot2.0与MyBatis.最新MySQL8.0整合过程中遇到的问题进 ...

  4. 在OneThink(ThinkPHP3.2.3)中整合阿里云OSS的PHP-SDK2.0.4,实现Web端直传,服务端签名直传并设置上传回调的实现流程

    在OneThink(ThinkPHP3.2.3)中整合阿里云OSS的PHP-SDK2.0.4,实现本地文件上传流程 by shuijingwan · 2016/01/13 1.SDK安装 github ...

  5. springboot2.x版本整合redis(单机/集群)(使用lettuce)

    在springboot1.x系列中,其中使用的是jedis,但是到了springboot2.x其中使用的是Lettuce. 此处springboot2.x,所以使用的是Lettuce.关于jedis跟 ...

  6. SpringBoot2.x版本整合SpringSecurity、Oauth2进行password认证

    很多人在进行项目开发时都会用到Oauth2.0结合SpringSecurity或者Shiro进行权限拦截以及用户验证,网上也有很多的案例,前几天项目里边需要用到,顺便整合了进来,特此写篇博客,记录下过 ...

  7. springboot2+shiro+jwt整合

    参考:https://www.jianshu.com/p/ef0a82d471d2 https://www.jianshu.com/p/3c51832f1051 https://blog.csdn.n ...

  8. springboot2.1.7整合swagger2.9.2

    什么是swagger? swagger是用于定义API文档的一个框架. 为什么要使用swagger? 当下项目开发时前后端是分离的,那么接口就成了前后端唯一的纽带.前端工程师如何知道哪个接口是干嘛的? ...

  9. springboot2.1.7整合Druid

    一.maven的依赖:文中就贴重点的, 其他依赖就不贴了 <dependency> <groupId>com.alibaba</groupId> <artif ...

随机推荐

  1. blessed-contrib 开发终端dashboard 的几点说明

    以前有说过blessed-contrib 这个很不错的终端dashboard 开发框架,以下是使用中的一些问题 中文编码 模式是不支持中文编码的,但是 我们可以在初始化的时候指定unicode编码 s ...

  2. [PHP] Laravel 5.5 的 BCrypt对密码进行加密及密码验证

    Laravel 5.5 的 BCrypt对密码进行加密及密码验证 一.加密 方法1) $password= Hash::make('密码'); 方法2) /也可直接使用 bcrypt 的 functi ...

  3. CTSC 2017 游戏[概率dp 线段树]

    小 R 和室友小 B 在寝室里玩游戏.他们一共玩了 $n$ 局游戏,每局游戏的结果要么是小 R 获胜,要么是小 B 获胜. 第 $1$ 局游戏小 R 获胜的概率是 $p_1$,小 B 获胜的概率是 $ ...

  4. mpvue图片上传

    mpvue小程序项目中的图片上传 我的csdn博客地址:https://blog.csdn.net/zmkyf1993 一般我是优先更新csdn内容,然后在拷过来的. 效果图 通过mpvue文档得知他 ...

  5. 通过shell脚本查看python版本并比较

    a.py import sys print(].split(])) test.sh #!/bin/sh zero= x=`python a.py` y="3.6" status=` ...

  6. js正则表达式之解决html解析<>标签问题

    应用场景:以博客写文章为例,有的时候我们不经意间写的字符串带标签,然后浏览器将其解析了,实际上我们并不希望其被解析,于是可通过核心代码解决该问题. 核心代码如下: data.codeSource.re ...

  7. BIND配置

    一,简介 相对于存储和大数据领域,CDN是一个相对小的领域,但行行出状元,BIND就是CDN领域的蝉联N届的状元郎.BIND是一款非常常用的DNS开源服务器,全球有90%的DNS用BIND实现.值得一 ...

  8. js - 常用工具集功能函数

    Note [普通JSON数组插入指定位置并且合并] let arr = [{ a: 11 }, { a: 11 }, { a: 11 }, { a: 117 }, { a: 11 }, { a: 11 ...

  9. Java NIO 文件通道使用

    读取一个文件的内容,然后写入另外一个文件 public class NioTest4 { public static void main(String[] args) throws Exception ...

  10. VUE-013-为elementUI 设置 tootip 宽度

    在表格显示列表中,通常添加 :show-overflow-tooltip="true" 以显示不能完全展示的单元格文案提示.单通常显示为全屏宽度,不易查看,可通过设置全局的样式,进 ...