drop table if exists `demo`; create table `demo` ( `id` bigint not null comment 'id', `name` varchar(50) comment '名称', primary key (`id`) ) engine=innodb default charset=utf8mb4 comment='测试'; insert into `demo` (id, name) VALUES (1, '测试');
mybatis gennerator
自动生成代码插件
<!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <configurationFile>src/main/resources/generator/generator-config.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies> </plugin>
根据自己设置的位置进行配置
generator-config.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> <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat"> <!-- 自动检查关键字,为关键字增加反引号 --> <property name="autoDelimitKeywords" value="true"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!--覆盖生成XML文件--> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> <!-- 生成的实体类添加toString()方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 不生成注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/wiki?serverTimezone=Asia/Shanghai" userId="wiki" password="root"> </jdbcConnection> <!-- domain类的位置 --> <javaModelGenerator targetProject="src\main\java" targetPackage="com.gaolang.wiki.domain"/> <!-- mapper xml的位置 --> <sqlMapGenerator targetProject="src\main\resources" targetPackage="mapper"/> <!-- mapper类的位置 --> <javaClientGenerator targetProject="src\main\java" targetPackage="com.gaolang.wiki.mapper" type="XMLMAPPER"/> <table tableName="demo" domainObjectName="Demo"/> <!--<table tableName="ebook"/>--> <!--<table tableName="category"/>--> <!--<table tableName="doc"/>--> <!--<table tableName="content"/>--> <!--<table tableName="user"/>--> <table tableName="ebook_snapshot"/> </context> </generatorConfiguration>
注意要修改生成类的位置和自己的表名
然后增加一个Maven启动运行我们的代码生成器
然后运行即可,可以看到生成的4个文件: