Mybatis-Generator学习

1、创建maven的jar项目。
2、在pom.xml中添加依赖

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zichen</groupId>
    <artifactId>MyBatis-Generator</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <!--mybatis的代码生成器的配置文件-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <!--允许覆盖生成的文件-->
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!--mysql的JDBC驱动-->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.47</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

3、在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>
    <!-- 引入配置文件 -->
    <properties resource="application-dev.properties"/>
    <!--
        id : 随便填,保证多个 context id 不重复就行
        defaultModelType : 可以不填,默认值 conditional,flat表示一张表对应一个po
        targetRuntime :可以不填,默认值 MyBatis3,常用的还有 MyBatis3Simple(接口会少很多,只包含最最常用的),这个配置会影响生成的 dao 和 mapper.xml的内容

        标签的名称
        property (0..N)
        plugin (0..N)
        commentGenerator (0 or 1)
        jdbcConnection (需要connectionFactory 或 jdbcConnection)
        javaTypeResolver (0 or 1)
        javaModelGenerator (至少1个)
        sqlMapGenerator (0 or 1)
        javaClientGenerator (0 or 1)
        table (1..N)
    -->
    <context id="mybatisTables" targetRuntime="MyBatis3Simple">
	
			<!-- 自动识别数据库关键字,默认为 false,一般保留默认值,遇到数据库关键字(Java关键字)时,按照 table 元素中 columnOverride 属性的配置进行覆盖;
		  如果设置为 true, 则需按照 SqlReservedWords 中定义的关键字列表,对关键字进行定界(分隔);
		  定界符(分隔符)参见 beginningDelimiter 和 endingDelimiter 的设置
		<property name="autoDelimitKeywords" value="false"/>
		
		 beginningDelimiter 和 endingDelimiter,定界符(分隔符),指明用于标记数据库关键字的符号,默认为为双引号 (");
		  在 oracle 中是双引号 ("),在 MySQL 中需配置为反引号 (`)  
		<property name="beginningDelimiter" value="`"/>
		<property name="endingDelimiter" value="`"/>
		
		 生成的 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"/>
			-->

			


        <!--
            plugin
            配置一个插件,例如
            <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
            这个插件给生成的Java模型对象增加了equals和hashCode方法
            
             使生成的 Model 实现 Serializable 接口 
			<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
			
			 为生成的 Model 覆写 toString() 方法 
			<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        -->
        <!--<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>-->
        <!--
            commentGenerator标签中 用来配置生成的注释。默认是生成注释的,并且会生成时间戳

            不希望生成的注释中包含时间戳
            <property name="suppressDate" value="true"/>

            添加 db 表中字段的注释
            默认生成的注释是不会有 db 表中字段的注释,如果你想知道每个字段在数据库中的含义(前提是数据库中对应表的字段你添加了注释)
            <property name="addRemarkComments" value="true"/>
        -->

		
        <commentGenerator>
            <!--
                是否去除自动生成的注释 true:是 : false:否
                说实话,MyBatis Generator 生成注释无用信息太多了,所以我一般都选择不生成注释
            -->

            <property name="suppressAllComments" value="true" />
        </commentGenerator>



        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="${spring.datasource.driverClassName}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        </jdbcConnection>

        <!--
            <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
                connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
                userId="yycg"
                password="yycg">
            </jdbcConnection>
        -->

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <!--
                是否使用 bigDecimal,默认false。
                false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
                true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal


                默认false
                false,将所有 JDBC 的时间类型解析为 java.util.Date
                true,将 JDBC 的时间类型按如下规则解析
                    DATE	                -> java.time.LocalDate
                    TIME	                -> java.time.LocalTime
                    TIMESTAMP                   -> java.time.LocalDateTime
                    TIME_WITH_TIMEZONE  	-> java.time.OffsetTime
                    TIMESTAMP_WITH_TIMEZONE	-> java.time.OffsetDateTime
            -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置和项目路径 -->
        <javaModelGenerator targetPackage="${model.po.Package}"
                            targetProject="${model.po.Project}">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>


        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="${map.mapperxml.Package}"
                         targetProject="${map.mapperxml.Project}">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${map.mapper.Package}"
                             targetProject="${map.mapper.Project}">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>


        <!--
            指定数据库表
            schema为数据库名,oracle需要配置,mysql不需要配置。
            tableName为对应的数据库表名
            domainObjectName 是要生成的实体类名(可以不指定,默认按帕斯卡命名法将表名转换成类名)
            enableXXXByExample 默认为 true, 为 true 会生成一个对应Example帮助类,帮助你进行条件查询,不想要可以设为false

            是否使用实际列名,默认为false
            <property name="useActualColumnNames" value="false" />
         -->

        <table schema="" tableName="sys_user" domainObjectName="User"
               enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false" selectByExampleQueryId="false">
        </table>
        <table schema="" tableName="sys_role" domainObjectName="Role"
               enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false" selectByExampleQueryId="false">
        </table>
        <table schema="" tableName="sys_dept" domainObjectName="Dept"></table>
        <table schema="" tableName="sys_menu" domainObjectName="Menu"></table>
        <table schema="" tableName="sys_oper_log" domainObjectName="OperLog"></table>


    </context>
</generatorConfiguration>

4、在resources下面创建application-dev.properties文件

# mysql数据源
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/jiangzi?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

# generator-Config
# po,entity实体类的包路径和项目路径
model.po.Package=com.zichen.entity
model.po.Project=src/main/java
# mapper和mapping
map.mapper.Package=com.zichen.mapper
map.mapper.Project=src/main/java

map.mapperxml.Package=mapping
map.mapperxml.Project=src/main/resources

5、启动
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值