在開發中ssm框架用的十分普遍。mybatis最爲持久層框架,根據xml、或者註解映射數據。本身能夠控制sql,靈活簡單操做數據庫。可是,全部的sql文件都是有本身編寫,不只繁瑣,並且很耗時,在開發中,速度、效率很重要。因此不少基礎sql是有規律可循,能夠根據數據庫字段自動生成的。下面就進入今天的主題,經過maven插件mybatis-generator自動生成代碼。html
一、環境配置,建立maven項目,在pom.xml添加插件配置。java
<build> <finalName>zsxt</finalName> <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>
二、配置generatorConfig.xmlmysql
<?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> <classPathEntry location="D:\maven-repository\repository\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar"/> <context id="my" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--數據庫鏈接 必需要有的,使用這個配置連接數據庫--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mt" userId="root" password="root"> </jdbcConnection> <!-- javamodel類生成器,targetPackage是生成實體的包名; targetProject是項目地址--> <javaModelGenerator targetPackage="com.mdd.pip.model" targetProject="src\main\java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成SQL map的XML文件生成器,注意,在Mybatis3以後,咱們可使用mapper.xml文件+Mapper接口(或者不用mapper接口), 或者只使用Mapper接口+Annotation,因此,若是 javaClientGenerator配置中配置了須要生成XML的話,這個元素就必須配置 targetPackage/targetProject:同javaModelGenerator --> <sqlMapGenerator targetPackage="com.mdd.pip.mapper" targetProject="src\main\java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator targetPackage="com.mdd.pip.mapper" targetProject="src\main\java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 選擇一個table來生成相關文件,能夠有一個或多個table,必需要有table元素 選擇的table會生成一下文件: 1,SQL map文件 2,生成一個主鍵類; 3,除了BLOB和主鍵的其餘字段的類; 4,包含BLOB的類; 5,一個用戶生成動態查詢的條件類(selectByExample, deleteByExample),可選; 6,Mapper接口(可選) tableName(必要):要生成對象的表名; 可選: 1,schema:數據庫的schema; 2,catalog:數據庫的catalog; 3,alias:爲數據表設置的別名,若是設置了alias,那麼生成的全部的SELECT SQL語句中,列名會變成:alias_actualColumnName 4,domainObjectName:生成的domain類的名字,若是不設置,直接使用表名做爲domain類的名字;能夠設置爲somepck.domainName,那麼會自動把domainName類再放到somepck包裏面; 5,enableInsert(默認true):指定是否生成insert語句; 6,enableSelectByPrimaryKey(默認true):指定是否生成按照主鍵查詢對象的語句(就是getById或get); 7,enableSelectByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢語句; 8,enableUpdateByPrimaryKey(默認true):指定是否生成按照主鍵修改對象的語句(即update); 9,enableDeleteByPrimaryKey(默認true):指定是否生成按照主鍵刪除對象的語句(即delete); 10,enableDeleteByExample(默認true):MyBatis3Simple爲false,指定是否生成動態刪除語句; 11,enableCountByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢總條數語句(用於分頁的總條數查詢); 12,enableUpdateByExample(默認true):MyBatis3Simple爲false,指定是否生成動態修改語句(只修改對象中不爲空的屬性); 13,modelType:參考context元素的defaultModelType,至關於覆蓋; 14,delimitIdentifiers:參考tableName的解釋,注意,默認的delimitIdentifiers是雙引號,若是相似MYSQL這樣的數據庫,使用的是`(反引號,那麼還須要設置context的beginningDelimiter和endingDelimiter屬性) 15,delimitAllColumns:設置是否全部生成的SQL中的列名都使用標識符引發來。默認爲false,delimitIdentifiers參考context的屬性 注意,table裏面不少參數都是對javaModelGenerator,context等元素的默認屬性的一個複寫; --> <table tableName="t_proxy_ip" domainObjectName="ProxyIp" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
在本例中,使用的是mysql數據庫,須要指定MySQL數據庫的JDBC驅動。sql
一、指定鏈接數據庫的URL;數據庫
2 javaModelGenerator 指定生成數據模型對象的包名,如com.xxx.xxx.model, targetProject 指定是項目及存放model的目錄。mybatis
三、sqlMapGenerator 須要設置包名,和存放映射文件的路徑。若是用maven 管理,通常xml文件放在src/main/resources目錄下。app
四、javaClientGenerator 須要設置包名及路徑。框架
六、接下來須要配置你須要生成的表名。dom
三、生成代碼eclipse
若是是在eclipse 中,選擇pom.xml文件,擊右鍵先擇Run AS——>Maven Build… ——>在Goals框中輸入:mybatis-generator:generate
若是在命令行輸入Maven命令,注意:必定是當前項目目錄下運行該命令:
mvn mybatis-generator:generate
ok完成