通用 Mapper

一、简介

使用通用 Mapper 无需自己创建 Mapper 接口内部方法和 Mapper 映射配置文件,只需要定义 Mapper 接口,并继承【 tk.mybatis.mapper.common.Mapper 】接口即可。

根据业务要求,在其 Service 类中调用对应的方法即可,但是 Mapper 只适合使用单表查询的情况。

二、使用步骤

1、创建项目并引入依赖

1.1 创建项目

本示例项目使用 SpringBoot 框架进行创建,项目名为:gmall,项目结构如下图所示:
在这里插入图片描述

1.2 引入 Maven 依赖

<!-- 引入通用 mapper 依赖 -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>

<!-- 热部署 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

<!-- 数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.9</version>
    <scope>runtime</scope>
</dependency>

<!-- SpringBoot WEB 模块 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 数据库连接驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

2、配置项目

主配置文件:application.yml

spring:
  profiles:
    active: dev

开发环境配置文件:application-dev.yml

# 配置服务器端口号
server:
  port: 8081
  context-path: /
---
# 配置数据源
spring:
  datasource:
    name: datasource
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://192.168.80.128:3306/gmall?serverTimezone=Asia/Shanghai
    username: root
    password: 123123
    driver-class-name: com.mysql.jdbc.Driver
---
# 枚举按简单类型处理,如果有枚举字段则需要加上该配置才会做映射,另外一种方法是在枚举类型属性上添加注解,详见4.1
mapper:
  enum-as-simple-type: true

3、配置 SpringBoot 主启动程序

GmallUserManagerApplication:

import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.atguigu.gmall.gmallusermanager.mapper")
public class GmallUserManagerApplication {
    public static void main(String[] args) {
        SpringApplication.run(GmallUserManagerApplication.class, args);
    }
}

注:@MapperScan 注解不是 MyBatis 提供(org.mybatis.~)的配置,而是由通用 Mapper 依赖提供(tk.mybatis.~)的配置,不要导错包。两个 @MapperScan 的作用相同,都是用来扫描 Mapper 接口的。


4、创建 Java 类

4.1 创建 Bean

根据数据库表(user_info)结构,创建相应的 Bean 实体类:UserInfo

注:使用通用Mapper,Java Bean 的命名必须是对应表明的驼峰式命名,例如:表名(user_info)==> Bean 名(UserInfo)

user_info 表结构:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ssIcPbmc-1583321988454)(C:\Users\BigBox\AppData\Roaming\Typora\typora-user-images\image-20200226171558922.png)]

UserInfo Bean:必须实现 Serializable 接口

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;

public class UserInfo implements Serializable {

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String loginName;

    @Column
    private String nickName;

    @Column
    private String passwd;

    @Column
    private String name;

    // 其他字段对应的属性省略
    
    // set 和 get 方法省略
}

注:通用 Mapper 注解

  • @Column:注解注释属性为表中的一个列;
  • @Id:注释注解属性为主键;
  • @GeneratedValue:注释注解主键的创建方式,根据创建方式,当数据保存到数据库成功后,自动将生成的 id 值添加到该对象中;
  • @Transient:表名该属性不对应表中字段,为该对象特有属性。
  • @ColumnType:在实体类的枚举字段加注解

4.2 创建 Controller

UserInfoController:使用 REST 风格

@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService user;

    /**
     * 添加用户
     *
     * @param userInfo
     */
    @PostMapping("/user")
    public void addUser(UserInfo userInfo) {
        user.addUser(userInfo);
    }

    /**
     * 根据 id 查询用户
     *
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public Object getUser(@PathVariable("id")Long id) {
        return user.getUserById(id);
    }

    /**
     * 根据 id 移除用户
     *
     * @param id
     */
    @DeleteMapping("/user/{id}")
    public void removeUser(@PathVariable("id")Long id) {
        user.removeUser(id);
    }

    /**
     * 更新用户信息
     * 
     * @param userInfo
     */
    @PutMapping("/user")
    public void updateUser(UserInfo userInfo) {
        user.updateUser(userInfo);
    }
}

4.3 创建 Service

UserInfoServiceImpl:其接口省略

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UsserInfoMapper user;

    @Override
    public void updateUser(UserInfo userInfo) {
        user.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public void removeUser(Long id) {
        user.deleteByPrimaryKey(id);
    }

    @Override
    public void addUser(UserInfo userInfo) {
        user.insertSelective(userInfo);
    }

    @Override
    public UserInfo getUserById(Long id) {
        return user.selectByPrimaryKey(id);
    }
}

注:在 Service中,根据业务需求调用其相应的方法即可

4.4 创建 Mapper

UsserInfoMapper:

import tk.mybatis.mapper.common.Mapper;

public interface UsserInfoMapper extends Mapper<UserInfo> {
}

注:使用通用 Mapper 时,Mapper 接口要继承【 tk.mybatis.mapper.common.Mapper 】接口,但是其内部不用定义任何方法,所需的方法在父类接口中均已定义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值