资讯专栏INFORMATION COLUMN

SpringBoot集成Mybatis 自动生成实体类和Mapper

codercao / 358人阅读

摘要:优化当我们在数据库中增加字段时,需要在对应的实体类中增加字段,中也需要去增加字段,去维护,会消耗大量的时间我们可以让接口去继承,删除接口中的所有方法,因为中都已经实现了。遇到这里问题不会报错,只要注意打印出来的语句即可。

SpringBoot集成Mybatis 自动生成实体类和Mapper 1.使用IDEA创建一个空的SpringBoot项目 2.在pom.xml中引入以下配置
    
        UTF-8
        UTF-8
        1.8
        
        
        ${basedir}/src/main/java
        
        com.jiafly.falsework.dao
        
        com.jiafly.falsework.entity
        
        ${basedir}/src/main/resources
        mapper
    

        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            RELEASE
        
        
        
            mysql
            mysql-connector-java
            5.1.39
            runtime
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            1.2.3
        

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    ${basedir}/src/main/resources/generatorConfig.xml
                    false
                    true
                
                
                    
                        mysql
                        mysql-connector-java
                        5.1.39
                    
                
            
        
    
3. 在application.yml中配置
spring:
  # 数据库相关配置
  datasource:
    url: jdbc:mysql://localhost:3306/jiafly?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useTimezone=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    initialSize: 1
    minIdle: 1
    maxActive: 5
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 180000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false

# mybaties 配置
mybatis:
  mapperLocations: classpath:mapper/*.xml
  typeAliasesPackage: com.jiafly.libra.entity
# 通用mapper 配置 自动生成dao层代码
mapper:
  plugin: tk.mybatis.mapper.generator.MapperPlugin
  Mapper: tk.mybatis.mapper.common.Mapper
  not-empty: false
  identity: MYSQL
4. 在src/main/resources文件夹下创建generatorConfig.xml文件




    

    
        
            
            
        
        
        
        
            
        
        
            
            
            
            
            
            
            
            
        

        
            
        

        
            
        
        
        
5. 执行mybatis-generator-maven-plugin插件

执行完毕后会在 com.jiafly.libra.entity中生成实体类,com.jiafly.libra.mapper中生成dao层接口 resources.mapper中生成xml文件。

6. 优化

当我们在数据库中增加字段时,需要在对应的实体类中增加字段,xml中也需要去增加字段,去维护,会消耗大量的时间

我们可以让接口去继承 Mapper ,删除接口中的所有方法,因为Mapper中都已经实现了。对于的xml中的sql也一并删除,之后增加字段就不需要再去维护

优化后的文件如下;

实体类

package com.jiafly.libra.entity;

import java.util.Date;

public class UserInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String userId;

    private String mobile;

    private String email;

    private String openId;

    private String unionId;

    private String userName;

    private String nickName;

    private Byte gender;

    private Integer age;

    private String city;

    private String province;

    private String country;

    private String password;

    private Byte vip;

    private Byte delFlag;

    private Date createTime;

    private Date updateTime;

}

接口

package com.jiafly.libra.mapper;

import com.jiafly.libra.entity.UserInfo;
import tk.mybatis.mapper.common.Mapper;

public interface UserInfoMapper extends Mapper {

}

xml






7.继承Mapper遇到的坑

需要在实体的主键上增加@Id注解,否则通用mapper无法识别主键,在是使用mapper的方法做更新查找等操作的时候会带上所有的条件,造成找不到数据。

遇到这里问题不会报错,只要注意打印出来的sql语句即可。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76840.html

相关文章

  • 一起来学SpringBoot | 第八篇:通用Mapper与分页插件的集成

    摘要:通用是为了解决使用中的基本操作,使用它可以很方便的进行开发,可以节省开发人员大量的时间。当该参数设置为时,时会查询第一页,超过总数时,会查询最后一页。 SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工...

    韩冰 评论0 收藏0
  • springboot+mybatis+mybatis-plus分页查询(简单实现)

    摘要:读取控制台内容请输入请输入正确的代码生成器全局配置实体属性注解数据源配置包配置这里有个模块名的配置,可以注释掉不用。 最近在研究mybatis,然后就去找简化mybatis开发的工具,发现就有通用Mapper和mybatis-plus两个比较好的可是使用,可是经过对比发现还是mybatis-plus比较好,个人觉得,勿喷。。。 集成还是非常简单的,然后就在研究怎么分页,开始研究通用ma...

    luffyZh 评论0 收藏0
  • springboot+mybatis+mybatis-plus分页查询(简单实现)

    摘要:读取控制台内容请输入请输入正确的代码生成器全局配置实体属性注解数据源配置包配置这里有个模块名的配置,可以注释掉不用。 最近在研究mybatis,然后就去找简化mybatis开发的工具,发现就有通用Mapper和mybatis-plus两个比较好的可是使用,可是经过对比发现还是mybatis-plus比较好,个人觉得,勿喷。。。 集成还是非常简单的,然后就在研究怎么分页,开始研究通用ma...

    Pocher 评论0 收藏0
  • springboot(二)——springboot自动配置解析

    摘要:前言用过的肯定很熟悉,它其中有个重要的特性,就是自动配置平时习惯的一些设置的配置作为默认配置。提倡无配置文件的理念,使用生成的应用完全不会生成任何配置代码与配置文件。 前言 用过springboot的肯定很熟悉,它其中有个重要的特性,就是自动配置(平时习惯的一些设置的配置作为默认配置)。springboot提倡无XML配置文件的理念,使用springboot生成的应用完全不会生成任何配...

    张率功 评论0 收藏0
  • Springboot项目搭建(四)整合MySQL数据库(MyBatis + 分页配置)

    springboot整合MySQL数据库(MyBatis + 分页配置) 一、POM文件添加依赖 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 com.github.pagehelper pagehelper 4.1.0 mysql mysql-connec...

    Alex 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<