mybatis代码自动生成

1.pom.xml配置generator插件

复制代码
<plugin>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-maven-plugin</artifactId>
      <version>1.3.7</version>
    <configuration>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
    </configuration>
</plugin>
复制代码

2.jdbc.propreties 配置数据源和代码生成需要的配置

1
2
3
4
5
6
7
8
9
10
11
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://ip\:端口/数据库?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
jdbc.username=用户名
jdbc.password=密码
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
 
#注意:使用最新版本的jar会有问题,要使用五点几版本的
jdbc.driverPath=D:\\Java\\IdeaProject\\mysql-connector-java-5.1.47\\mysql-connector-java-5.1.47.jar
target_package=生成代码的包位置
project=src\\main\\java

3.generatorConfig.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?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>
    <properties resource="jdbc.properties" />
      <!-- 指定数据连接驱动jar地址 -->
    <classPathEntry location="${jdbc.driverPath}" />
    <!-- 此处指定生成针对MyBatis3的DAO -->
    <context id="context" targetRuntime="MyBatis3">
 
          <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>
 
        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
 
        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
 
         <!--beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号;-->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
 
         <!-- 为生成的Java模型创建一个toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
         <!-- 添加序列号方法 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
 
         <!-- 重命名插件 -->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<!--         <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> -->
<!--             <property name="searchString" value="Example$" /> -->
<!--             <property name="replaceString" value="Criteria" /> -->
<!--         </plugin> -->
 
        <!-- 去掉生成出来的代码的注解 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>
 
        <!--数据库链接URL,用户名、密码  jdbc.propreties中读取-->
        <jdbcConnection driverClass="${jdbc.driverClass}"
            connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">
        </jdbcConnection>
 
        <!-- 默认false,表示把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer; true表示把JDBC DECIMAL
            和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
 
        <!-- 生成bean对象 -->
        <javaModelGenerator targetPackage="${target_package}.pojo"
            targetProject="${project}">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
 
        <!-- 生成sqlMap xml -->
        <sqlMapGenerator targetPackage="${target_package}.mapping"
            targetProject="${project}">
            <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
 
        <!-- 生成DAO的类文件 -->
        <javaClientGenerator targetPackage="${target_package}.dao"
            targetProject="${project}" type="XMLMAPPER">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
             
<!--      简单生成-->
<!--     <table tableName="表名" domainObjectName="生成前缀"-->
<!--            enableCountByExample="false" enableUpdateByExample="false"-->
<!--            enableDeleteByExample="false" enableSelectByExample="false"-->
<!--            selectByExampleQueryId="false">-->
<!--     </table>-->
 
<!--             复杂生成-->
                <table tableName="表名" domainObjectName="生成前缀"/>
 
    </context>
</generatorConfiguration>

 4,运行方式:maven -plugins- mybatis-generator:generate 

posted @   韩伊  阅读(917)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示