springboot2.1.7整合mybati3.5.2与mysql8.0.13

2019-09-08 09:44:52来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

springboot2.1.7整合mybati3.5.2与mysql8.0.13

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肯定会有这样那样的问题,这些错误总结就是博主踩过的大坑,特别贴出来供大家参考。


原文链接:https://www.cnblogs.com/zjk-main/p/11468614.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:你真的了解Mybatis的${}和#{}吗?是否了解应用场景?

下一篇:面试官:判断一个数是否为2的整数次幂