mybatis-generator是一款在使用mybatis框架時(shí)捻浦,自動(dòng)生成model,dao和mapper的工具
第一步添加pom插件
<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>
第二步編寫(xiě)配置文件
jdbc.properties
db.driverLocation=/Users/dsying/Develop/lib/mysql-connector-java-5.1.6-bin.jar
db.driverClassName=com.mysql.jdbc.Driver
db.username=root
db.password=19920115asd
db.url=jdbc:mysql://localhost:3306/mmall_learning?characterEncoding=utf-8
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>
<!--導(dǎo)入屬性配置-->
<properties resource="jdbc.properties"></properties>
<!--指定特定數(shù)據(jù)庫(kù)的jdbc驅(qū)動(dòng)jar包的位置-->
<classPathEntry location="${db.driverLocation}"/>
<context id="default" targetRuntime="MyBatis3">
<!-- optional屋确,旨在創(chuàng)建class時(shí)纳击,對(duì)注釋進(jìn)行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的數(shù)據(jù)庫(kù)連接 -->
<jdbcConnection
driverClass="${db.driverClassName}"
connectionURL="${db.url}"
userId="${db.username}"
password="${db.password}">
</jdbcConnection>
<!-- 非必需,類(lèi)型處理器攻臀,在數(shù)據(jù)庫(kù)類(lèi)型和java類(lèi)型之間的轉(zhuǎn)換控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- Model模型生成器,用來(lái)生成含有主鍵key的類(lèi)焕数,記錄類(lèi) 以及查詢Example類(lèi)
targetPackage 指定生成的model生成所在的包名
targetProject 指定在該項(xiàng)目下所在的路徑
-->
<!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
<javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">
<!-- 是否允許子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否對(duì)model添加 構(gòu)造函數(shù) -->
<property name="constructorBased" value="true"/>
<!-- 是否對(duì)類(lèi)CHAR類(lèi)型的列的數(shù)據(jù)進(jìn)行trim操作 -->
<property name="trimStrings" value="true"/>
<!-- 建立的Model對(duì)象是否 不可改變 即生成的Model對(duì)象不會(huì)有 setter方法,只有構(gòu)造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!--mapper映射文件生成所在的目錄 為每一個(gè)數(shù)據(jù)庫(kù)的表生成對(duì)應(yīng)的SqlMap文件 -->
<!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
<sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 客戶端代碼,生成易于使用的針對(duì)Model對(duì)象和XML配置文件 的代碼
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對(duì)象
type="MIXEDMAPPER",生成基于注解的Java Model 和相應(yīng)的Mapper對(duì)象
type="XMLMAPPER",生成SQLMap XML文件和獨(dú)立的Mapper接口
-->
<!-- targetPackage:mapper接口dao生成的位置 -->
<!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!-- 重寫(xiě)這兩個(gè)字段的類(lèi)型 兩者在mysql中都是text類(lèi)型 要改成VARCHAR-->
<columnOverride column="detail" jdbcType="VARCHAR" />
<columnOverride column="sub_images" jdbcType="VARCHAR" />
</table>
<table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<!-- geelynote mybatis插件的搭建 -->
</context>
</generatorConfiguration>
3 雙擊插件
image.png
4 生成 Dao層接口 實(shí)體類(lèi)POJO 以及接口實(shí)現(xiàn) mapper.xml
image.png
5 項(xiàng)目結(jié)構(gòu)圖
image.png