mybatis-generator的使用

mybatis-generator的使用

自动生成代码mybatis-generator官网地址:
http://www.mybatis.org/generator/configreference/xmlconfig.html

1、新建maven项目并且在pom.xml中添加mybatis-generator的插件

<build>
    <finalName>ibatisGenerator</finalName>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.7</version>
        </plugin>
    </plugins>
</build>

2、在resources中添加database.properties的数据库的配置文件,注意路径的/

db.driverLocation=C:/Users/Administrator/IdeaProjects/MyBatisGenerator/doc/mysql-connector-java-5.1.48.jar
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/test1212?characterEncoding=utf-8
db.username=root
db.password=root

3、从官网下载generatorConfig.xml放在resources目录下,并且修改内容
内容详见doc下的generatorConfiguration.xml中的说明
http://www.mybatis.org/generator/configreference/xmlconfig.html
配置文件的加单配置

<?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="datasource.properties"></properties>

    <!-- 指定数据库驱动的jdbc驱动jar包的位置 -->
    <classPathEntry location="${db.driverLocation}" />

    <!-- context 是逆向工程的主要配置信息 -->
    <!-- id:起个名字 -->
    <!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
    <context id="default" targetRuntime="MyBatis3">
        <!--覆盖生成XML文件-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
        <!--optional,旨在创建class时,对注释进行控制-->
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />

        </commentGenerator>

        <!--jdbc的数据库连接-->
        <jdbcConnection driverClass="${db.driverClassName}"
                        connectionURL="${db.url}"
                        userId="${db.username}"
                        password="${db.password}">
        </jdbcConnection>

        <!--非必须,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <!-- 默认情况下数据库中的 decimal,bigInt 在 Java 对应是 sql 下的 BigDecimal 类 -->
            <!-- 不是 double 和 long 类型 -->
            <!-- 使用常用的基本类型代替 sql 包下的引用类型 -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetPackage:生成的实体类所在的包 -->
        <!-- targetProject:生成的实体类所在的硬盘位置 -->
        <javaModelGenerator targetPackage="com.fjh.entity"
                            targetProject=".\src\main\java">
            <!-- 是否允许子包,默认false -->
            <!--官网文档介绍:For example, suppose a table MYTABLE in schema MYSCHMA.
              Also suppose that the targetPackage attribute is set to "com.mycompany".
              If this property is true, the generated objects
              for the table will be placed in the package "com.mycompany.myschema".
              If the property is false, the generated objects will
              be placed in the "com.mycompany" schema.
              The default value is false.-->
            <property name="enableSubPackages" value="false" />
            <!-- 是否对model添加构造函数 -->
            <property name="constructorBased" value="true" />
            <!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
            <property name="trimStrings" value="true" />
            <!-- 建立model对象是否不可改变 即生成的model对象不会有setter方法,只有构造方法 -->
            <property name="immutable" value="false" />
        </javaModelGenerator>

        <!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
        <sqlMapGenerator targetPackage="mappers"
                         targetProject=".\src\main\resources">
            <!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.fjh.dao" targetProject=".\src\main\java">
            <!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <table tableName="auto_code" domainObjectName="AutoCode"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--用于指定自动生成的键的属性(来自标识字段或序列)-->
            <generatedKey column="id" sqlStatement="JDBC"></generatedKey>
        </table>

        <table tableName="course" domainObjectName="Course"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--用于指定自动生成的键的属性(来自标识字段或序列)-->
            <generatedKey column="id" sqlStatement="JDBC"></generatedKey>
        </table>
    </context>
</generatorConfiguration>

4、运行mvn 命令 mybatis-generator:generate 或者直接在idea中点击运行该命令

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值