Mybatis的generator自动生成代码

mybatis-generator有三种用法:命令行、ide插件、maven插件。本次使用maven生成

环境:IDEA,mysql8,maven

(1):新建项目,本次以SpringBoot项目为例

(2)在maven中添加依赖:

 1 <plugin>
 2                 <groupId>org.mybatis.generator</groupId>
 3                 <artifactId>mybatis-generator-maven-plugin</artifactId>
 4                 <version>1.3.7</version>
 5                 <configuration>
 6                     <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
 7                     <verbose>true</verbose>
 8                     <overwrite>true</overwrite>
 9                 </configuration>
10 </plugin>
mybatis-generator-maven-plugin

(3)添加配置文件,项目结构如图:

(4)在application.yml文件中添加mybatis, 需要注意的是mysql8和之前的Driver不一样,而且需要设置时区,不然会报错

 1 server:
 2   port: 8080
 3 
 4 spring:
 5   datasource:
 6     name: test
 7     url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
 8     username: root
 9     password: 123456
10     driver-class-name: com.mysql.cj.jdbc.Driver
11 #    页面加载
12   thymeleaf:
13     cache: false
14 
15 
16 mybatis:
17   mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
18   type-aliases-package: com.feifei.login.entity  # 注意:对应实体类的路径
application.yml

(5)在resource文件夹下创建generator文件夹,并在文件夹中创建generatorConfig.xml文件,自行修改包的关系,连接信息等

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 <generatorConfiguration>
 6     <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
 7     <classPathEntry
 8             location="C:\Users\Flyuz\.m2\repository\mysql\mysql-connector-java\8.0.12\mysql-connector-java-8.0.12.jar"/>
 9     <context id="DB2Tables" targetRuntime="MyBatis3">
10         <commentGenerator>
11             <property name="suppressDate" value="true"/>
12             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
13             <property name="suppressAllComments" value="true"/>
14         </commentGenerator>
15         <!--数据库连接驱动类,URL,用户名、密码 -->
16         <jdbcConnection
17                 driverClass="com.mysql.cj.jdbc.Driver"
18                 connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"
19                 userId="root"
20                 password="123456">
21             <property name="nullCatalogMeansCurrent" value="true"/>
22         </jdbcConnection>
23         <javaTypeResolver>
24             <property name="forceBigDecimals" value="false"/>
25         </javaTypeResolver>
26         <!-- 生成(实体)模型的包名和位置-->
27         <javaModelGenerator targetPackage="com.feifei.login.entity" targetProject="src/main/java">
28             <property name="enableSubPackages" value="true"/>
29             <property name="trimStrings" value="true"/>
30         </javaModelGenerator>
31         <!-- 生成XML映射文件的包名和位置-->
32         <sqlMapGenerator targetPackage="com.feifei.login.mapper" targetProject="src/main/java">
33             <property name="enableSubPackages" value="true"/>
34         </sqlMapGenerator>
35         <!-- 生成DAO接口的包名和位置-->
36         <javaClientGenerator type="XMLMAPPER" targetPackage="com.feifei.login.mapper" targetProject="src/main/java">
37             <property name="enableSubPackages" value="true"/>
38         </javaClientGenerator>
39         <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
40         <!--当表有主键时,根据primaryKey生成select、update、delete方法-->
41         <!--Text类型的字段时,generator会生成WithBLOBS.java文件并继承-->
42         <table tableName="%" domainObjectName="User"
43                enableSelectByPrimaryKey="true"
44                enableUpdateByPrimaryKey="true"
45                enableDeleteByPrimaryKey="true"
46                enableCountByExample="false" enableUpdateByExample="false"
47                enableDeleteByExample="false" enableSelectByExample="false"
48                selectByExampleQueryId="false">
49             <columnOverride column="title" javaType="java.lang.String" jdbcType="VARCHAR"/>
50             <columnOverride column="content" javaType="java.lang.String" jdbcType="VARCHAR"/>
51         </table>
52 
53 
54     </context>
55 </generatorConfiguration>
generatorConfig.xml

自动生成:

结果在下面命令行中显示:

 如果遇到什么问题,根据显示结果修改配置文件

猜你喜欢

转载自www.cnblogs.com/flyuz/p/10896468.html