springboot整合mybatis及通用mapper

前言:

求求了求求了 不要再使用逆向工程了 虽然 逆向工程可以帮你生成数据库表对应的pojo用起来也特别的爽 基本上单表不用自己写sql 但 生成的文件, mapper.java,mapper.xml,pojo,pojoExample 是不是有点繁杂 用了 通用mapper 可以使你的项目更加的简洁 

使用:

 1、创建springboot工程

 2、引用依赖

 3、配置application.yml文件

 4、创建IBaseDao公用mapper接口

 5、创建pojo,mapper

创建springboot工程

File------->New---------->Project------>Spring Initializr-------->修改项目名,组名 next----->

选择依赖

在pom文件中引用通用mapper依赖


        <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>

配置yml后缀文件(application.yml)

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost/db_news?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123
#mybatis配置
mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml
  type-aliases-package: com.shenwang.pojo
#通用mapper类配置:
mapper:
  mappers: com.shenwang.baseDao.IBaseDao
  identity: MYSQL

创建pojo

这里要添加 通用mapper的注解  不然不好做映射

package com.shenwang.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Repository;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @author: shenwang
 * Date: 2021/6/9
 */
@Repository
//mybatis通用接口mapper依赖JPA实体类采用JPA
@Table(name = "tb_news")
public class News implements Serializable {
    /**
     * 新闻编号 主键 自动递增
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY )
    private Integer id;
    /**
     * 新闻标题
     */
    @Column(name = "title")
    private String title;
    /**
     * 新闻内容
     */
    @Column(name = "content")
    private String content;
    /**
     * 创建时间
     */
    @Column(name = "createTime")
    //格式化时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", createTime=" + createTime +
                '}';
    }
}

创建mapper 

mapper 需要继承我们的公用mapper Mapper

package com.shenwang.mapper;

import tk.mybatis.mapper.common.Mapper;
import com.shenwang.pojo.News;

/**
 * @Description 新闻通用接口继承公共接口,注意泛型
 * @author: shenwang
 * Date: 2021/6/9
 */
public interface NewsMapper extends Mapper<News> {

}

其他写法 比如service,controller层写法 并没有改动 但需要注意的是 我们的启动类Applicaton 扫描mapper层的注解@MapperScan 要引用  tk.mybatis.spring.annotation 的注解 不然会出现异常

package com.shenwang;


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

/**
 * @author: shenwang
 * Date: 2021/6/9
 */
@SpringBootApplication
@MapperScan("com.shenwang.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值