-
创建一个Maven(无骨架)工程
-
编写pom.xml (添加下面内容)
<!-- 依赖 MyBatis 核心包 --> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> </dependencies> </plugin> </plugins> </build>
-
在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> <!--mybatis-generator:generate--> <context id="atguiguTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 数据库连接的信息:驱动类、连接地址、用户名、密码--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql:///database?serverTimezone=UTC" userId="root" password="root"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和NUMETIC类型解析为Integer, 为true时把 JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- targetProject:生成Entity类的路径--> <javaModelGenerator targetPackage="com.example.org.entity" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀--> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格--> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- targetProject:XXXMapper.xml映射文件生成的路径--> <sqlMapGenerator targetPackage="com.example.org.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- Mapper接口生成的位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.org.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 数据库表名和对应的entity--> <table tableName="t_test" domainObjectName="Test"/> </context> </generatorConfiguration>
-
运行
- 在idea命令行执行下面命令
mybatis-generator:generate
-
使用插件自带的
Idea中使用Mybatis逆向工程
最新推荐文章于 2024-06-19 09:30:00 发布