idea中使用mybatis逆向工程
阿新 • • 發佈:2019-01-11
Intellij IDEA中使用MyBatis-generator 自動生成MyBatis程式碼
1、在idea中安裝MyBatis-generator外掛,點選 File -> Settings -> Plugins,輸入mybatis-generator找到這個外掛,如下圖
2、建立一個maven工程,此過程略
3、在pom檔案中新增以下程式碼
<dependencies> <!-- mybatis --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!-- mybatis整合spring 的適配包 --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- mybatis逆向工程 --> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <finalName>generator</finalName> <plugins> <!--mybatis 逆向工程外掛--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>false</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> </plugin> </plugins>
4、在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> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> <!-- 是否去除所有自動生成的檔案的時間戳,預設為false --> <!-- <property name="suppressDate" value="false"/> --> </commentGenerator> <!-- 配置資料庫連線 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/資料庫名?useSSL=true" userId="root" password="root"> </jdbcConnection> <!--Oracle資料庫的連線資訊--> <!-- <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> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定javaBean生成的位置 --> <!-- targetPackage:包名稱(自定義) targetProject:專案路徑(自定義) --> <javaModelGenerator targetPackage="com.xhanglog.entity" targetProject=".\src\main\java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="true" /> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--指定sql對映檔案生成的位置 --> <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 指定dao介面生成的位置,mapper介面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xhanglog.dao" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- table指定每個表的生成策略 domainObjectName="Abnormal"--> <table tableName="artical" ></table> <table tableName="tag" ></table> <table tableName="member" ></table> <table tableName="link" ></table> <table tableName="comment" ></table> </context> </generatorConfiguration>
5、點選 Maven Projects -> mybatis-generator,如下圖所示
6,執行後的目錄結構如下
專案地址:https://github.com/MXMonkey/generator