mysql+mybatis+generator整合


在ORM框架中经常使用mybatis+generator来实现快速逆向生成代码,如下使用mysql数据库简单记录下

1.创建maven工程并选择jdk

2.在pom中添加引入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-demo-projectartifactId>
        <groupId>org.examplegroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <groupId>org.examplegroupId>
    <artifactId>mybatisGenerator-demoartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>

    <properties>
        <maven.compiler.source>11maven.compiler.source>
        <maven.compiler.target>11maven.compiler.target>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>2.3.5.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.14version>
        dependency>
        <dependency>
            <groupId>com.github.xiaoymingroupId>
            <artifactId>knife4j-spring-boot-starterartifactId>
            <version>3.0.3version>
            <exclusions>
                <exclusion>
                    <artifactId>swagger-annotationsartifactId>
                    <groupId>io.swaggergroupId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <artifactId>swagger-annotationsartifactId>
            <groupId>io.swaggergroupId>
            <version>1.5.22version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.18version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
            <version>2.3.6.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.0.0version>
        dependency>
        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>1.2.10version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <version>2.4.0version>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>1.3.7.RELEASEversion>
            plugin>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.7version>

                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.3.7version>
                    dependency>

                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>5.1.46version>
                    dependency>
                dependencies>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xmlconfigurationFile>
                    <overwrite>trueoverwrite>
                    <verbose>trueverbose>
                configuration>
            plugin>
        plugins>
    build>
project>

3.配置文件中添加数据库配置

server:
  port: 8080

spring:
  datasource:
    name: mysql
    url: jdbc:mysql://127.0.0.1:3306/devdatabase
    username: devuser
    password: admin

mybatis:
  mapper-locations: classpath:mapping/*.xml  #注意resources文件夹下目录层级使用"/"而不是"."
  type-aliases-package: com.mybatisgeneratordemo.entity  # 注意:对应实体类的路径

4.在配置文件夹resources创建generatorConfig.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
    
    <classPathEntry location="D:\repository\org\mybatis\generator\mybatis-generator-core\1.3.7\mybatis-generator-core-1.3.7.jar"/>

    <context id="context" targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        commentGenerator>
        
        <jdbcConnection userId="root" password="root" driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/devdatabase"/>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="com.mybatisgeneratordemo.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>
        
        <javaClientGenerator targetPackage="com.mybatisgeneratordemo.mapper" type="XMLMAPPER" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        javaClientGenerator>
        
        <table schema="BuyrUser" tableName="buyr_user" enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false"/>
    context>
generatorConfiguration>

 5.创建数据库表:

CREATE TABLE `buyr_user` (
`USER_ID` bigint(19) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`USER_NAME` varchar(90) NOT NULL COMMENT '用户名称',
`NICK_NAME` varchar(90) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '用户昵称',
`USER_INTRO` varchar(300) DEFAULT NULL COMMENT '个性签名',
`AVATAR` varchar(1000) DEFAULT NULL COMMENT '头像图片',
`EMAIL` varchar(255) DEFAULT NULL COMMENT '邮件地址',
`PHONE` varchar(255) DEFAULT NULL COMMENT '手机号',
`USER_PASS` varchar(255) DEFAULT NULL COMMENT '密码',
`PASS_SALT` varchar(255) DEFAULT NULL COMMENT '密码盐',
`USER_STATUS` varchar(32) DEFAULT '1' COMMENT '用户状态',
`USER_SCORE` int(11) DEFAULT NULL COMMENT '用户打分',
`TOTAL_COST_AMT` decimal(24,6) DEFAULT NULL COMMENT '累计消费金额',
`LAST_LOGIN_TIME` timestamp NULL DEFAULT NULL COMMENT '最后登录时间',
`REVISION` int(11) DEFAULT NULL COMMENT '乐观锁',
`CREATED_BY` varchar(32) DEFAULT NULL COMMENT '创建人',
`CREATED_TIME` timestamp NULL DEFAULT NULL COMMENT '创建时间',
`UPDATED_BY` varchar(32) DEFAULT NULL COMMENT '更新人',
`UPDATED_TIME` timestamp NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='买家';

6.maven工具窗点击并运行mybatis-generator:generate

运行窗展示 BUILD SUCCESS 并自动生成个对应文件, 同时注意修改实体中主键id的类型为String,防止Long长度过长前端数据进度丢失;

项目结构如下:

ORM