springboot中使用通用mapper时遇到的坑:查询结果为null

在学习springboot时,发现一个比较好用的东西:通用mapper

一、简介
通用Mapper都可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。

极其方便的使用MyBatis单表的增删改查。

支持单表操作,不支持通用的多表联合查询。

通用 Mapper 支持 Mybatis-3.2.4 及以上版本。
注:项目地址

使用通用mapper可以减少写很多的简单查询,感觉挺有用的也感觉挺简单的所以就去试了试,不试不知道,一试全是问题 >.<
在一开始写的时候很快就搭建好项目需要的依赖,写完一句简单的查询就急忙去试试效果。
将结果输出到控制台,返现结果null,没有任何的报错(几乎没有提示信息)
出现这个问题就想到了接下来的几个步骤。

  1. 赶紧查一下是不是字段没对上
  2. 是不是主键查询的注解写错了
  3. 查询语句有问题(看不见)
  4. 依赖有问题?(渐渐远去)
  5. jar包冲突(渐渐远去)
  6. 。。。。

仔细检查了一圈感觉没有什么问题啊
开始面向百度编程
没怎么找到类似的问题,也可能是百度水平不够

想了想字段很简单啊,没有理由错啊,那些注解都是跟着教程敲的也应该没有问题啊
于是重点落在了第三个上
想看看是不是语句有什么问题,没有输出也不知道怎么看,突然想到前几天学习的日志等级配置在yml配置文件中加上日志,logging项目路径: debug
发现查询的语句根本不是根据我的主键id查询的,在语句的后面还加上了其他条件
后来发现那个通用mapper不认识int类型,要使用Integer包装类,直接跳过了我的id,根据我后面参数进行查询,而我输入的是id,所以查询结果一直是null

项目结构
结构

需要的依赖pom.xml

   <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 通用Mapper ,包含了很多依赖 -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

一些重复的依赖可以去掉,避免冲突
通用mapper依赖

application.yml配置文件

mybatis:
  type-aliases-package: com.example.springboot03.pojo
#默认开启驼峰映射
spring:
  datasource:
    url: jdbc:mysql:///yiju?useSSL=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456
logging:
  level:
    com.example.springboot03: debug

实体类

package com.example.springboot03.pojo;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.*;

/**
 * date:2020-04-18
 * author:zhangxs
 */
@Data
@Table(name = "tb_user")
public class UserInfo {
    @Id
//    自增
    @KeySql(useGeneratedKeys = true)
    private Integer userId;
    private String phone;
    private String password;
    private String email;
    private String nickname;
    private String truename;
//   临时的 不作为字段,数据库中没有
    @Transient
    private String more;

}

mapper接口(继承通用mapper接口,里面有很多方法)

package com.example.springboot03.mapper;

import com.example.springboot03.pojo.UserInfo;
import tk.mybatis.mapper.common.Mapper;
/**
 * date:2020-04-18
 * author:zhangxs
 */

public interface UserMapper extends Mapper<UserInfo> {

}

启动类(扫描mapper中接口)

package com.example.springboot03;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.example.springboot03.mapper")
public class Springboot03Application {

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

}

测试类

package com.example.springboot03;


import com.example.springboot03.mapper.UserMapper;
import com.example.springboot03.pojo.UserInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * date:2020-04-18
 * author:zhangxiaoshuai
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;
    @Test
    public void test01(){
        UserInfo userInfo=userMapper.selectByPrimaryKey(10005);
        System.out.println("userInfo:"+userInfo);
    }
}

不得不说 通用mapper 还是挺香的
规则
规则

在Spring Boot中使用Mapper需要进行以下步骤: 1. 在启动类上添加`@MapperScan`注解来扫描Mapper包,并将继承的Mapper接口添加到MyBatis配置中。如下所示: ```java @SpringBootApplication @MapperScan("com.cloudsw.mapper") public class MapperApplication { public static void main(String[] args) { SpringApplication.run(MapperApplication.class, args); } } ``` 这样就可以将Mapper接口添加到MyBatis的配置中。 2. 配置数据库和MyBatis。在`application.yml`配置文件中添加数据库和MyBatis的配置。示例如下: ```yaml spring: datasource: name: clodsw-portal-dev url: jdbc:mysql://localhost:3306/cloudsw_portal?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&autoReconnect=true&allowPublicKeyRetrieval=true username: 123456 password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.cloudsw.dto mapper: mappers: com.cloudsw.base.IBaseMapper identity: MYSQL ``` 在上述配置中,配置了数据库连接信息和MyBatis的一些配置项。 3. 使用通用Mapper通用Mapper是一个简化数据访问操作的工具,可以减少大量的重复代码。具体使用方法可以参考官方文档。 总结起来,在Spring Boot中使用Mapper需要在启动类上添加`@MapperScan`注解,配置数据库和MyBatis的相关信息,以及按需使用通用Mapper来简化数据访问操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [一看就会一学就废之SpringBoot整合通用Mapper以及常用方法](https://blog.csdn.net/qq_45675378/article/details/115324620)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值