spring-boot集成tkmybatis通用mapper和逆向工程mybatis-generator-core生成实体

一,引入依赖


        
            mysql
            mysql-connector-java
            5.1.47
        


        
        
        
            com.alibaba
            druid
            1.1.7
        


        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
        
            tk.mybatis
            mapper-spring-boot-starter
            2.1.5
        
        
            tk.mybatis
            mapper
            4.0.4
        
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.7
        
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.10
        

二,pom引入逆向工程mybatis-generator插件


        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.0.3.RELEASE
            
            

            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.7
                
                    
                        mysql
                        mysql-connector-java
                        5.1.47
                    
                    
                        tk.mybatis
                        mapper
                        4.0.4
                    
                
                
                    
                    true
                    
                    true
                    
                    
                        src/main/resources/generatorConfig.xml
                    
                
            
        
    

三,application.properties配置文件

##################### mybatis ###############################
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.typeAliasesPackage=com.entity
mybatis.mapperLocations=classpath*:/com/mapper/**/*.xml
mybatis.configuration.use-generated-keys=true


pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql


##################### Druid ##################################

spring.datasource.url=jdbc:mysql://localhost:3306/weixin?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 下面为连接池的补充设置,应用到上面所有数据源中
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
#这个过滤器有个小坑,会影响自定义批量sql,相关可以看我的另外一篇文章https://blog.csdn.net/cmw1085215666/article/details/101380207
spring.datasource.filters=stat,wall,log4j
spring.datasource.logSlowSql=true


四,创建generatorConfig.xml配置文件





    
    
    

    
        
        

        
            
            
            
        

        
        
        
        
        
            
        

        
        
            
            
        
        
        
            
        
        
        
            
        

        
        

五,运行mybatis-generator:generate生成实体及相关文件
dao

package com.dao;

import com.entity.WeixinUser;
import tk.mybatis.mapper.common.Mapper;

public interface WeixinUserMapper extends Mapper {
}

entity

package com.entity;

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

@Table(name = "weixin_user")
public class WeixinUser {
    /**
     * 主键自增ID
     */
    @Id
    @Column(name = "customer_id")
    private Integer customerId;

    @Column(name = "open_id")
    private String openId;

    private String unionid;

    /**
     * 昵称
     */
    @Column(name = "nike_name")
    private String nikeName;

    /**
     * 头像
     */
    private String photo;

    /**
     * 个性签名
     */
    private String motto;

    /**
     * 最后使用时间
     */
    @Column(name = "last_login_time")
    private Date lastLoginTime;

    @Column(name = "create_time")
    private Date createTime;

    @Column(name = "update_time")
    private Date updateTime;

    /**
     * 获取主键自增ID
     *
     * @return customer_id - 主键自增ID
     */
    public Integer getCustomerId() {
        return customerId;
    }

    /**
     * 设置主键自增ID
     *
     * @param customerId 主键自增ID
     */
    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    /**
     * @return open_id
     */
    public String getOpenId() {
        return openId;
    }

    /**
     * @param openId
     */
    public void setOpenId(String openId) {
        this.openId = openId == null ? null : openId.trim();
    }

    /**
     * @return unionid
     */
    public String getUnid() {
        return unionid;
    }

    /**
     * @param unionid
     */
    public void setUnid(String unionid) {
        this.unionid = unionid == null ? null : unionid.trim();
    }

    /**
     * 获取昵称
     *
     * @return nike_name - 昵称
     */
    public String getNikeName() {
        return nikeName;
    }

    /**
     * 设置昵称
     *
     * @param nikeName 昵称
     */
    public void setNikeName(String nikeName) {
        this.nikeName = nikeName == null ? null : nikeName.trim();
    }

    /**
     * 获取头像
     *
     * @return photo - 头像
     */
    public String getPhoto() {
        return photo;
    }

    /**
     * 设置头像
     *
     * @param photo 头像
     */
    public void setPhoto(String photo) {
        this.photo = photo == null ? null : photo.trim();
    }

    /**
     * 获取个性签名
     *
     * @return motto - 个性签名
     */
    public String getMotto() {
        return motto;
    }

    /**
     * 设置个性签名
     *
     * @param motto 个性签名
     */
    public void setMotto(String motto) {
        this.motto = motto == null ? null : motto.trim();
    }

    /**
     * 获取最后使用时间
     *
     * @return last_login_time - 最后使用时间
     */
    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    /**
     * 设置最后使用时间
     *
     * @param lastLoginTime 最后使用时间
     */
    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    /**
     * @return create_time
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @param createTime
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @return update_time
     */
    public Date getUpdateTime() {
        return updateTime;
    }

    /**
     * @param updateTime
     */
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
}

mapper




  
    
    
    
    
    
    
    
    
    
    
  
  
    
    customer_id, open_id, unionid, nike_name, photo, motto, last_login_time, create_time, 
    update_time
  

CREATE TABLE `weixin_user` (
  `customer_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键自增ID',
  `open_id` varchar(256) NOT NULL,
  `unid` varchar(256) NOT NULL,
  `nike_name` varchar(256) NOT NULL COMMENT '昵称',
  `photo` varchar(256) DEFAULT NULL COMMENT '头像',
  `motto` varchar(512) DEFAULT NULL COMMENT '个性签名',
  `last_login_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后使用时间',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`customer_id`),
  UNIQUE KEY `uk_id` (`open_id`,`unid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

spring-boot集成tkmybatis通用mapper和逆向工程mybatis-generator-core生成实体_第1张图片
六,使用tkmybatis项目相关注解

@MapperScan("com.dao")//扫描相关dao
@EntityScan(basePackages = {"com.entity"})//扫描相关实体

spring-boot集成tkmybatis通用mapper和逆向工程mybatis-generator-core生成实体_第2张图片

你可能感兴趣的:(springBoot,mybaits,java)