好長時間沒有寫博客了,最近公司要用java語言,開始學習java,屬於初學者,今天主要記錄一下mybatis generator自動生成代碼,首先在以下圖的目錄中新建兩個文件,以下圖java
具體generatorConfig.xml內容以下:mysql
<?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> <classPathEntry location="${dbconfig.sqlServer.driverLocation}" /> <context id="context1" targetRuntime="MyBatis3"> <!--<commentGenerator> <!– 去除自動生成的註釋 –> <property name="suppressAllComments" value="true" /> </commentGenerator>--> <!-- 是否生成註釋 去除自動生成的註釋--> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 數據庫鏈接配置 --> <jdbcConnection driverClass="${dbconfig.sqlServer.driverClasss}" connectionURL="${dbconfig.sqlServer.ssmDemo.read.jdbcUrl}" userId="${dbconfig.sqlServer.username}" password="${dbconfig.sqlServer.password}" /> <!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--配置生成的實體包 targetPackage:生成的實體包位置,默認存放在src目錄下 targetProject:目標工程名 --> <javaModelGenerator targetPackage="com.pojo" targetProject="src/main/java" /> <!-- 實體包對應映射文件位置及名稱,默認存放在src目錄下 --> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources" /> <!--生成Dao類存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成對應表及類名--> <!-- 配置表 schema:不用填寫 tableName: 表名 enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId: 去除自動生成的例子 --> <table schema="" tableName="UzaiSupplierStore" domainObjectName="UzaiSupplierStore" enableCountByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" > </table> <table schema="" tableName="UzaiCountry" enableCountByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" > </table> </context> </generatorConfiguration>
jdbc.properties內容以下sql
dbconfig.sqlServer.driverClasss=com.microsoft.sqlserver.jdbc.SQLServerDriver dbconfig.sqlServer.ssmDemo.read.jdbcUrl=jdbc:sqlserver://127.0.0.1;DatabaseName=Travel dbconfig.sqlServer.username=uzai_trip dbconfig.sqlServer.password=Flzx3qC!$ #定義初始鏈接數 dbconfig.initialSize=0 #定義最大鏈接數 dbconfig.maxActive=20 #定義最大空閒 dbconfig.maxIdle=20 #定義最小空閒 dbconfig.minIdle=1 #定義最長等待時間 dbconfig.maxWait=60000 dbconfig.sqlServer.driverLocation=D://java//jar_package//sqljdbc4-4.0.jar
D://java//jar_package//sqljdbc4-4.0.jar這個地址必定是在本地下載好這個jar包的地址
jdbc.properties的參數是對應generatorConfig.xml裏面的,以下圖數據庫
而後就是須要配置pom.xmlmybatis
以上配置完成之後,點run彈出以下界面 app
以上都配置之後點擊運行就能夠生成了dom
目前發的生成的dao文件名字是Mapper,還有就是生成的數據庫類沒有中文註釋,大多數解決方案是mysql數據庫。關於sqlserver還在進一步瞭解中sqlserver