成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

SpringBoot集成Mybatis 自動生成實(shí)體類和Mapper

codercao / 674人閱讀

摘要:優(yōu)化當(dāng)我們在數(shù)據(jù)庫中增加字段時,需要在對應(yīng)的實(shí)體類中增加字段,中也需要去增加字段,去維護(hù),會消耗大量的時間我們可以讓接口去繼承,刪除接口中的所有方法,因?yàn)橹卸家呀?jīng)實(shí)現(xiàn)了。遇到這里問題不會報(bào)錯,只要注意打印出來的語句即可。

SpringBoot集成Mybatis 自動生成實(shí)體類和Mapper 1.使用IDEA創(chuàng)建一個空的SpringBoot項(xiàng)目 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:
  # 數(shù)據(jù)庫相關(guān)配置
  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文件夾下創(chuàng)建generatorConfig.xml文件




    

    
        
            
            
        
        
        
        
            
        
        
            
            
            
            
            
            
            
            
        

        
            
        

        
            
        
        
        
5. 執(zhí)行mybatis-generator-maven-plugin插件

執(zhí)行完畢后會在 com.jiafly.libra.entity中生成實(shí)體類,com.jiafly.libra.mapper中生成dao層接口 resources.mapper中生成xml文件。

6. 優(yōu)化

當(dāng)我們在數(shù)據(jù)庫中增加字段時,需要在對應(yīng)的實(shí)體類中增加字段,xml中也需要去增加字段,去維護(hù),會消耗大量的時間

我們可以讓接口去繼承 Mapper ,刪除接口中的所有方法,因?yàn)镸apper中都已經(jīng)實(shí)現(xiàn)了。對于的xml中的sql也一并刪除,之后增加字段就不需要再去維護(hù)

優(yōu)化后的文件如下;

實(shí)體類

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遇到的坑

需要在實(shí)體的主鍵上增加@Id注解,否則通用mapper無法識別主鍵,在是使用mapper的方法做更新查找等操作的時候會帶上所有的條件,造成找不到數(shù)據(jù)。

遇到這里問題不會報(bào)錯,只要注意打印出來的sql語句即可。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/76840.html

相關(guān)文章

  • 一起來學(xué)SpringBoot | 第八篇:通用Mapper與分頁插件的集成

    摘要:通用是為了解決使用中的基本操作,使用它可以很方便的進(jìn)行開發(fā),可以節(jié)省開發(fā)人員大量的時間。當(dāng)該參數(shù)設(shè)置為時,時會查詢第一頁,超過總數(shù)時,會查詢最后一頁。 SpringBoot 是為了簡化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕易的搭建出一個 WEB 工...

    韓冰 評論0 收藏0
  • springboot+mybatis+mybatis-plus分頁查詢(簡單實(shí)現(xiàn))

    摘要:讀取控制臺內(nèi)容請輸入請輸入正確的代碼生成器全局配置實(shí)體屬性注解數(shù)據(jù)源配置包配置這里有個模塊名的配置,可以注釋掉不用。 最近在研究mybatis,然后就去找簡化mybatis開發(fā)的工具,發(fā)現(xiàn)就有通用Mapper和mybatis-plus兩個比較好的可是使用,可是經(jīng)過對比發(fā)現(xiàn)還是mybatis-plus比較好,個人覺得,勿噴。。。 集成還是非常簡單的,然后就在研究怎么分頁,開始研究通用ma...

    Pocher 評論0 收藏0
  • springboot(二)——springboot自動配置解析

    摘要:前言用過的肯定很熟悉,它其中有個重要的特性,就是自動配置平時習(xí)慣的一些設(shè)置的配置作為默認(rèn)配置。提倡無配置文件的理念,使用生成的應(yīng)用完全不會生成任何配置代碼與配置文件。 前言 用過springboot的肯定很熟悉,它其中有個重要的特性,就是自動配置(平時習(xí)慣的一些設(shè)置的配置作為默認(rèn)配置)。springboot提倡無XML配置文件的理念,使用springboot生成的應(yīng)用完全不會生成任何配...

    張率功 評論0 收藏0
  • Springboot項(xiàng)目搭建(四)整合MySQL數(shù)據(jù)庫(MyBatis + 分頁配置)

    springboot整合MySQL數(shù)據(jù)庫(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

發(fā)表評論

0條評論

閱讀需要支付1元查看
<