在学习springboot时,发现一个比较好用的东西:通用mapper
一、简介
通用Mapper都可以极大的方便开发人员。可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法。
极其方便的使用MyBatis单表的增删改查。
支持单表操作,不支持通用的多表联合查询。
通用 Mapper 支持 Mybatis-3.2.4 及以上版本。
注:项目地址
使用通用mapper可以减少写很多的简单查询,感觉挺有用的也感觉挺简单的所以就去试了试,不试不知道,一试全是问题 >.<
在一开始写的时候很快就搭建好项目需要的依赖,写完一句简单的查询就急忙去试试效果。
将结果输出到控制台,返现结果null,没有任何的报错(几乎没有提示信息)
出现这个问题就想到了接下来的几个步骤。
- 赶紧查一下是不是字段没对上
- 是不是主键查询的注解写错了
- 查询语句有问题(看不见)
- 依赖有问题?(渐渐远去)
- jar包冲突(渐渐远去)
- 。。。。
仔细检查了一圈感觉没有什么问题啊
开始面向百度编程
没怎么找到类似的问题,也可能是百度水平不够
想了想字段很简单啊,没有理由错啊,那些注解都是跟着教程敲的也应该没有问题啊
于是重点落在了第三个上
想看看是不是语句有什么问题,没有输出也不知道怎么看,突然想到前几天学习的日志等级配置,在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>
一些重复的依赖可以去掉,避免冲突
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 还是挺香的
规则