Mybatis自动生成代码

扶醉桌前 提交于 2020-01-03 15:11:24

Mybatis自动生成代码

1、配置maven依赖

<dependency>

       <groupId>org.mybatis.generator</groupId>

       <artifactId>mybatis-generator-core</artifactId>

       <version>1.3.7</version>

</dependency>

2、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>

 

    <context id="tusiTables" targetRuntime="MyBatis3">

        <commentGenerator>

            <!-- 是否去除自动生成的注释-->

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

        </commentGenerator>

        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

                        connectionURL="jdbc:mysql://192.168.134.132:3306/eonline"

                        userId="root" password="root">

        </jdbcConnection>

        <javaTypeResolver>

            <property name="forceBigDecimals" value="false" />

        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->

        <javaModelGenerator targetPackage="com.tencent.domain"

                            targetProject=".\src\main\java">

            <!-- enableSubPackages:是否让schema作为包的后缀 -->

            <property name="enableSubPackages" value="false"/>

            <!-- 从数据库返回的值被清理前后的空格 -->

            <property name="trimStrings" value="true"/>

        </javaModelGenerator>

        <!-- targetProject:mapper映射文件生成的位置 -->

        <sqlMapGenerator targetPackage="mapper"

                         targetProject=".\src\main\resources">

            <!-- enableSubPackages:是否让schema作为包的后缀 -->

            <property name="enableSubPackages" value="false"/>

        </sqlMapGenerator>

        <!-- targetPackage:mapper接口生成的位置 -->

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.tencent.mapper"

                             targetProject=".\src\main\java">

            <!-- enableSubPackages:是否让schema作为包的后缀 -->

            <property name="enableSubPackages" value="false"/>

        </javaClientGenerator>

        <table tableName="t_deposit_cert"/>

        <table tableName="t_deposit_file"/>

    </context>

</generatorConfiguration>

3、代码接口调用

public void generator() throws Exception {

       List<String> warning = new ArrayList<>();

       boolean overwrite = true;

 

       File file = new File("src/main/resources/mybatis-generator.xml");

       if (!file.exists()) {

              return;

       }

 

       ConfigurationParser parser = new ConfigurationParser(warning);

       Configuration configuration = parser.parseConfiguration(file);

       DefaultShellCallback callback = new DefaultShellCallback(overwrite);

       MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warning);

       myBatisGenerator.generate(null);

}

 

public static void main(String[] args) {

       TestGenerator generator = new TestGenerator();

       try {

              generator.generator();

       } catch (Exception e) {

              e.printStackTrace();

       }

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!