第一步:先安裝mybatis-generator插件; java
第二步:建立maven項目, 在src/main/resources下建立generatorConfig.xml mysql
第三步:配置generatorConfig.xml,具體配置以下:
sql
<?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"/> <!-- 制定數據鏈接驅動jar地址 --> <classPathEntry location="${path}"/> <!-- 一個數據庫對應一個context --> <context id="db_context"> <!-- 註釋 --> <commentGenerator> <property name="suppressAllComments" value="true"/><!-- 是否取消註釋 --> <property name="suppressDate" value="false" /> <!-- 是否生成註釋代時間戳--> </commentGenerator> <!-- jdbc鏈接 --> <jdbcConnection driverClass="${driverClassName}" connectionURL="${url1}" userId="${username}" password="${password}" /> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化如下類型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成實體類地址 --> <javaModelGenerator targetPackage="com.ssm.domain" targetProject="${projectName}" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false"/> <!-- 是否針對string類型的字段在set的時候進行trim調用 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="com.ssm.dao" targetProject="${projectName}"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成mapxml對應client,也就是接口dao --> <javaClientGenerator targetPackage="com.ssm.dao" targetProject="${projectName}" type="XMLMAPPER" > <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 配置表信息 --> <!-- schema即爲數據庫名 tableName爲對應的數據庫表 domainObjectName是要生成的實體類 enable*ByExample是否生成 example類 --> <table schema="${database}" tableName="sys_user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- 忽略列,不生成bean 字段 --> <ignoreColumn column="FRED" /> <columnOverride column="id" property="id" javaType="java.lang.Long"/> </table> <table schema="${database}" tableName="sys_attachment" domainObjectName="Attachment" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- 忽略列,不生成bean 字段 --> <ignoreColumn column="FRED" /> <columnOverride column="id" property="id" javaType="java.lang.Long"/> <columnOverride column="createTime" property="create_time" javaType="java.util.Date"/> <columnOverride column="creater" property="creater" javaType="java.lang.Long"/> </table> </context> </generatorConfiguration>jdbc.properties配置以下:
driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=UTF-8 username=root password=*** maxIdle=5 maxActive=40 defaultAutoCommit=false timeBetweenEvictionRunsMillis=3600000 minEvictableIdleTimeMillis=3600000 url1=jdbc:mysql://localhost:3306/test path=E:/mysql-connector-java-5.1.18.jar projectName=ssm database=test配置好以後,就能夠測試了。
第四步:測試,右鍵generatorConfig.xml 點擊Generate Mybatis/Ibatis Artifacts,而後刷新項目去檢測有沒有生成代碼吧。 數據庫