exclipse使用maven mybatis-generator-maven-plugin 外掛生成程式碼的坑
阿新 • • 發佈:2018-11-10
按照網上的步驟使用maven配置生成mybatis程式碼就是行,沒有報錯也沒有執行.
步驟是這樣的:
1.在pom.xml配置
<build> <finalName>byh</finalName> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- MyBatis Generator --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <!-- 列印執行過程 --> <verbose>true</verbose> <!-- 允許覆蓋生成的檔案 --> <overwrite>true</overwrite> <!-- 配置檔案路徑 --> <configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile> <!-- 生成部分mapper,指定table名字 --> <tableNames> <!-- b_article, b_byzone, b_collection, b_comment, b_contacts, b_enterprise, b_feedback, b_honor, b_opportunity, b_photo, b_user_attach_info, b_vip_approval, d_dictionary, s_resource, s_role, s_role_resource, s_user_role, sb_user, b_byzone b_activity --> b_entertainment </tableNames> </configuration> <dependencies> <!-- generator --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>cc.pengar.mybitis</groupId> <artifactId>generator</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.0.2</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build>
2:在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="mysql" defaultModelType="hierarchical" targetRuntime="cc.pengar.mybitis.generator.MyTkMyBatis3SimpleImpl" > <!-- 自動識別資料庫關鍵字,預設false,如果設定為true,根據SqlReservedWords中定義的關鍵字列表; 一般保留預設值,遇到資料庫關鍵字(Java關鍵字),使用columnOverride覆蓋 --> <property name="autoDelimitKeywords" value="false"/> <!-- 生成的Java檔案的編碼 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java程式碼 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML程式碼 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <!-- beginningDelimiter和endingDelimiter:指明資料庫的用於標記資料庫物件名的符號,比如ORACLE就是雙引號,MYSQL預設是`反引號; --> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <plugin type="cc.pengar.mybitis.generator.expand.MyMapperPlugin"> <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/> <property name="caseSensitive" value="true"/> <property name="configurationType" value="cc.pengar.mybitis.generator.expand.internal.ChineseCommentGenerator"/> </plugin> <!-- 必須要有的,使用這個配置連結資料庫 @TODO:是否可以擴充套件 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.82:3306/byh_test?serverTimezone=UTC&useSSL=false" userId="root" password="admin"> <!-- 這裡面可以設定property屬性,每一個property屬性都設定到配置的Driver上 --> </jdbcConnection> <!-- java型別處理器 用於處理DB中的型別到Java中的型別,預設使用JavaTypeResolverDefaultImpl; 注意一點,預設會先嚐試使用Integer,Long,Short等來對應DECIMAL和 NUMERIC資料型別; --> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- java模型建立器,是必須要的元素 負責:1,key類(見context的defaultModelType);2,java類;3,查詢類 targetPackage:生成的類要放的包,真實的包受enableSubPackages屬性控制; targetProject:目標專案,指定一個存在的目錄下,生成的內容會放到指定目錄中,如果目錄不存在,MBG不會自動建目錄 --> <javaModelGenerator targetPackage="com.byh.entity" targetProject="src/main/java"> <property name="constructorBased" value="false"/> <property name="enableSubPackages" value="true"/> <property name="immutable" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成SQL map的XML檔案生成器, 注意,在Mybatis3之後,我們可以使用mapper.xml檔案+Mapper介面(或者不用mapper介面), 或者只使用Mapper介面+Annotation,所以,如果 javaClientGenerator配置中配置了需要生成XML的話,這個元素就必須配置 targetPackage/targetProject:同javaModelGenerator --> <sqlMapGenerator targetPackage="com.byh.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 對於mybatis來說,即生成Mapper介面,注意,如果沒有配置該元素,那麼預設不會生成Mapper介面 targetPackage/targetProject:同javaModelGenerator type:選擇怎麼生成mapper介面(在MyBatis3/MyBatis3Simple下): 1,ANNOTATEDMAPPER:會生成使用Mapper介面+Annotation的方式建立(SQL生成在annotation中),不會生成對應的XML; 2,MIXEDMAPPER:使用混合配置,會生成Mapper介面,並適當新增合適的Annotation,但是XML會生成在XML中; 3,XMLMAPPER:會生成Mapper介面,介面完全依賴XML; 注意,如果context是MyBatis3Simple:只支援ANNOTATEDMAPPER和XMLMAPPER --> <javaClientGenerator targetPackage="com.byh.dao" type="XMLMAPPER" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 配置表資訊,不生成幫助類examples--> <!-- <table tableName="b_article" mapperName="ArticleDao" domainObjectName="Article"/> <table tableName="b_byzone" mapperName="ByZoneDao" domainObjectName="ByZone"/> <table tableName="b_technology" mapperName="TechnologyDao" domainObjectName="Technology"/> <table tableName="b_collection" mapperName="CollectionDao" domainObjectName="Collection"/> <table tableName="b_comment" mapperName="CommentDao" domainObjectName="Comment"/> <table tableName="b_enterprise" mapperName="EnterpriseDao" domainObjectName="Enterprise"/> <table tableName="b_feedback" mapperName="FeedbackDao" domainObjectName="Feedback"/> <table tableName="b_honor" mapperName="HonorDao" domainObjectName="Honor"/> <table tableName="b_opportunity" mapperName="OpportunityDao" domainObjectName="Opportunity"/> <table tableName="b_photo" mapperName="PhotoDao" domainObjectName="Photo"/> <table tableName="b_user_attach_info" mapperName="UserAttachInfoDao" domainObjectName="UserAttachInfo"/> <table tableName="b_vip_order" mapperName="VipOrderDao" domainObjectName="VipOrder"/> <table tableName="d_dictionary" mapperName="DictionaryDao" domainObjectName="Dictionary"/> <table tableName="d_area" mapperName="AreaDao" domainObjectName="Area"/> <table tableName="s_resource" mapperName="ResourceDao" domainObjectName="Resource"/> <table tableName="s_role" mapperName="RoleDao" domainObjectName="Role"/> <table tableName="s_role_resource" mapperName="RoleResourceDao" domainObjectName="RoleResource"/> <table tableName="s_user_role" mapperName="UserRoleDao" domainObjectName="UserRole"/> <table tableName="sb_user" mapperName="UserDao" domainObjectName="User"/> <table tableName="b_activity" mapperName="ActivityDao" domainObjectName="Activity"/> --> <table tableName="b_entertainment" mapperName="EntertainmentDao" domainObjectName="Entertainment"/> </context> </generatorConfiguration>
一切準備就緒後執行...
右鍵專案-->Run As-->run configurations
-->點選Run執行,發現控制檯空空如也,也不報錯.鬱悶中...
最後嘗試用命令列執行,發現居然可以了
開啟cmd命令視窗
-->進入專案位置(cd 即可自行百度)
-->執行如下命令:mvn mybatis-generator:generate
-->完事了,重新整理專案即可
如下圖: