1.在Intellij IDEA建立maven項目(本過程比較簡單,略)java
2. 在maven項目的pom.xml 添加mybatis-generator-maven-plugin 插件web
<build><!--mybatis逆向工程插件--> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
3.用了外置的配置文件jdbc.properties,能夠將一下屬性配置到properties文件之中,模板以下:sql
driver=org.postgresql.Driver
url=jdbc:postgresql://192.168.31.101:5432/DataBase
usernames=userName
password=password
driverLocation=src/main/webapp/lib/postgresql-42.2.2.jre7.jar
4.在maven項目下的src/main/resources 目錄下創建名爲 generatorConfig.xml的配置文件,做爲mybatis-generator-maven-plugin 插件的執行目標,模板以下:數據庫
簡單模版:mybatis
<?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"/> <!--指定特定數據庫的jdbc驅動jar包的位置--> <classPathEntry location="${driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在建立class時,對註釋進行控制 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的數據庫鏈接 --> <jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${usernames}" password="${password}"> </jdbcConnection> <!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 targetPackage 指定生成的model生成所在的包名 targetProject 指定在該項目下所在的路徑 --> <!--<javaModelGenerator targetPackage="com.demo.pojo" targetProject=".\src\main\java">--> <javaModelGenerator targetPackage="com.bean" targetProject="./src/main/java"> <!-- 是否容許子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="true"/> <!-- 是否對model添加 構造函數 --> <property name="constructorBased" value="true"/> <!-- 是否對類CHAR類型的列的數據進行trim操做 --> <property name="trimStrings" value="true"/> <!-- 創建的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!--mapper映射文件生成所在的目錄 爲每個數據庫的表生成對應的SqlMap文件 --> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件的代碼 type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper對象 type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper對象 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 --> <!-- targetPackage:mapper接口dao生成的位置 --> <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.demo.dao" targetProject=".\src\main\java">--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="./src/main/java"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--數據庫對應數據表--> <table tableName="數據庫表名字" domainObjectName="SingleLib" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <!-- mybatis插件的搭建 --> </context> </generatorConfiguration>
複雜配置:app
<?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"></properties> <!--指定特定數據庫的jdbc驅動jar包的位置 --> <classPathEntry location="${jdbc.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在建立class時,對註釋進行控制 --> <commentGenerator> <property name="suppressDate" value="true" /> </commentGenerator> <!--jdbc的數據庫鏈接 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection> <!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制--> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 targetPackage 指定生成的model生成所在的包名 targetProject 指定在該項目下所在的路徑 --> <javaModelGenerator targetPackage="com.bean" targetProject="./src/main/java"> <!-- 是否對model添加 構造函數 --> <property name="constructorBased" value="true"/> <!-- 是否容許子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 創建的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 --> <property name="immutable" value="true"/> <!-- 給Model添加一個父類 --> <property name="rootClass" value="com.foo.louis.Hello"/> <!-- 是否對類CHAR類型的列的數據進行trim操做 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--Mapper映射文件生成所在的目錄 爲每個數據庫的表生成對應的SqlMap文件 --> <sqlMapGenerator targetPackage="mapping" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼 type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper對象 type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper對象 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 --> <javaClientGenerator targetPackage="com.dao" targetProject="./src/main/java" type="MIXEDMAPPER"> <property name="enableSubPackages" value=""/> <!-- 定義Maper.java 源代碼中的ByExample() 方法的可視性,可選的值有: public; private; protected; default 注意:若是 targetRuntime="MyBatis3",此參數被忽略 --> <property name="exampleMethodVisibility" value=""/> <!--方法名計數器 Important note: this property is ignored if the target runtime is MyBatis3. --> <property name="methodNameCalculator" value=""/> <!-- 爲生成的接口添加父接口--> <property name="rootInterface" value=""/> </javaClientGenerator> <!--數據庫對應數據表--> <table schema="" tableName="表名" domainObjectName="實體類名" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="louis" tableName="user" domainObjectName="User" > <!-- optional , only for mybatis3 runtime 自動生成的鍵值(identity,或者序列值) 若是指定此元素,MBG將會生成<selectKey>元素,而後將此元素插入到SQL Map的<insert> 元素之中 sqlStatement 的語句將會返回新的值 若是是一個自增主鍵的話,你可使用預約義的語句,或者添加自定義的SQL語句. 預約義的值以下: Cloudscape This will translate to: VALUES IDENTITY_VAL_LOCAL() DB2: VALUES IDENTITY_VAL_LOCAL() DB2_MF: SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1 Derby: VALUES IDENTITY_VAL_LOCAL() HSQLDB: CALL IDENTITY() Informix: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1 MySql: SELECT LAST_INSERT_ID() SqlServer: SELECT SCOPE_IDENTITY() SYBASE: SELECT @@IDENTITY JDBC: This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. This is a database independent method of obtaining the value from identity columns. identity: 自增主鍵 If true, then the column is flagged as an identity column and the generated <selectKey> element will be placed after the insert (for an identity column). If false, then the generated <selectKey> will be placed before the insert (typically for a sequence). --> <generatedKey column="" sqlStatement="" identity="" type=""/> <!-- optional. 列的命名規則: MBG使用 <columnRenamingRule> 元素在計算列名的對應 名稱以前,先對列名進行重命名, 做用:通常須要對BUSI_CLIENT_NO 前的BUSI_進行過濾 支持正在表達式 searchString 表示要被換掉的字符串 replaceString 則是要換成的字符串,默認狀況下爲空字符串,可選 --> <columnRenamingRule searchString="" replaceString=""/> <!-- optional.告訴 MBG 忽略某一列 column,須要忽略的列 delimitedColumnName:true ,匹配column的值和數據庫列的名稱 大小寫徹底匹配,false 忽略大小寫匹配 是否限定表的列名,即固定表列在Model中的名稱 --> <ignoreColumn column="PLAN_ID" delimitedColumnName="true" /> <!--optional.覆蓋MBG對Model 的生成規則 column: 數據庫的列名 javaType: 對應的Java數據類型的徹底限定名 在必要的時候能夠覆蓋由JavaTypeResolver計算獲得的java數據類型. For some databases, this is necessary to handle "odd" database types (e.g. MySql's unsigned bigint type should be mapped to java.lang.Object). jdbcType:該列的JDBC數據類型(INTEGER, DECIMAL, NUMERIC, VARCHAR, etc.),該列能夠覆蓋由JavaTypeResolver計算獲得的Jdbc類型,對某些數據庫而言,對於處理特定的JDBC 驅動癖好 頗有必要(e.g. DB2's LONGVARCHAR type should be mapped to VARCHAR for iBATIS). typeHandler: --> <columnOverride column="" javaType="" jdbcType="" typeHandler="" delimitedColumnName="" /> </table> </context> </generatorConfiguration>
5.在webapp下新建lib包,並把數據庫驅動jar包放在裏面,項目目錄以下:dom
6.使用webapp