Use mybatis-generator plugin to generate dao, po, mapping files

1. Introducing the jar package
I am using Mysql database here, so I need to introduce a jar package to connect to the myspl database
Insert picture description here

2. Create the configuration file generatorConfig.xml in the src directory, the code is as follows:

<?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" />
    <!-- classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 -->
    <classPathEntry location="${jdbc.driverPath}" />
    <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />

         <!--  关闭自动生成注释  -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 数据库链接URL,用户名、密码 -->
        <jdbcConnection connectionURL="${jdbc.url}"
            driverClass="${jdbc.driverClass}" userId="${jdbc.user}" password="${jdbc.password}" />

        <!-- 指定JDBC和Java类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal,默认为false false可自动转化以下类型(Long, Integer, Short, etc.-->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成模型的po包名和位置 -->
        <javaModelGenerator targetPackage="${package.model}"
            targetProject="${targetProject}">
            <property name="constructorBased" value="true" />
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
            <!-- <property name="rootClass" value="com.ajs.base.BaseEntity" /> -->
        </javaModelGenerator>

        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="${package.sql.mapper}"
            targetProject="${targetProject}">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator targetPackage="${package.dao.mapper}"
            targetProject="${targetProject}" type="XMLMAPPER">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 这里开始写需要要生成的表 -->
        <!-- 要生成的表 tableName 是数据库中的表名或视图名 domainObjectName 是实体类名 -->
        <table tableName="user" domainObjectName="User"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
            <!-- 配置主键    column 主键列名 ; identity 指定主键是否自增-->
            <generatedKey column="userid" sqlStatement="Mysql" identity="true" />
        </table>
        
        <table tableName="power" domainObjectName="Power"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
            <!-- 配置主键    column 主键列名 ; identity 指定主键是否自增-->
            <generatedKey column="powerid" sqlStatement="Mysql" identity="true" />
        </table>
        
    </context>
</generatorConfiguration>

3. Generated by the mybatis-generator plug-in right-click menu, right-click on generatorConfig.xml, and select Generate MyBatis/iBATIS Artifacts, as shown in the following figure:
Insert picture description here

4. Wait for the end of the generation progress bar, dao, mapping, and po will be generated under the package name of the configuration file configuration (configured in the jdbc.properties file), as shown in the following figure:

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44547592/article/details/109555971