Springboot 2.1.7 integrates mybati 3.5.2 with MySQL 8.0.13

Keywords: Java Mybatis Spring xml MySQL

Springboot 2.x has been released for some time, where bloggers use Springboot 2.1.7 to integrate mybatis 3.5.2 and use MySQL 8.0.13 as the database.

1. Import dependency

<!--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. Configure the data source in application.properties (this file will be automatically scanned by spring boot)

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=*********
#This is used in versions above MySQL 8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis reads all files ending in. xml under the mapper folder in the root directory
mybatis.mapper-locations=classpath:/mapper/*.xml

3. Write 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: (AdminService I self-brain-filling)

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 {

    /**
     * Administrator login
     * @param userName
     * @param password
     * @return AdminEntity
     */
    AdminEntity login(String userName,String password);
}

Resoures/mapper/AdminDao.xml (mapping file):

<?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 Mapping with columns in the database-->
    <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. How to assemble dao in spring container?

Method 1: Add an annotation @Mapper to the dao interface class
@Repository
@Mapper
public interface AdminDao {

    /**
     * Administrator login
     * @param userName
     * @param password
     * @return AdminEntity
     */
    AdminEntity login(String userName,String password);

}
Method 2: Annotate @MapperScan on spring boot startup class
@SpringBootApplication
@MapperScan("com.gl.pin.service.system.dao")
public class PinServiceSystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(PinServiceSystemApplication.class, args);
    }

}

5. Summary of Error Reporting

1.springboot startup failed - - no interface class was found

Error details:

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.

Solution: Not using @Mapper or @MapperScan, or using it incorrectly (@MapperScan scans the package with the wrong path, etc.)

2. springboot starts successfully and sends an error message - - dao and mapper.xml bindings fail and no corresponding method can be found

Error details:

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

Solutions:

2.1 The package where the Dao interface resides is inconsistent with the namespace in the mapper.xml Mapping File

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

2.2 There are methods in dao, but not in mapper.xml
2.3 The method name in Dao is different from the id in mapper.xml

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

2.4 mapper.xml file is not parsed at all and needs to be configured in application.properties

#It's better to use "/" and "." in case of multi-level files, there may be problems.
mybatis.mapper-locations=classpath:/mapper/*.xml

springboot integration mybatis is bound to have such and such problems, these errors are summarized as bloggers stepped on the pit, especially posted for your reference.

Posted by mullz on Mon, 30 Sep 2019 08:58:24 -0700