我们在相关项目开发中经常要用到mybatis,它的一些插件例如mybatis generator 就是很好用的一个插件,下面简要介绍一下它的配置:
1:首先在pom文件中加入如下依赖:
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <!-- <exclusions> <exclusion> <groupId>com.google.collections</groupId> <artifactId>google-collections</artifactId> </exclusion> </exclusions> --> </dependency>
在配置该文件的时候一定要注意版本号,刚开始用的版本是1.3.4就会出现jar包冲突的问题,
2:接下来进行配置文件的设置:
<?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="db.properties"/> <!--数据库驱动的jar地址 --> <classPathEntry location="${classpath}"/> <context id="default" targetRuntime="MyBatis3"> <!-- 创建class时对注释进行控制 --> <commentGenerator> <!-- 是否生成注释时间戳 --> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="false"/> </commentGenerator> <jdbcConnection driverClass="${jdbc.driverClassName}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <!-- 类型转换 --> <javaTypeResolver > <!-- 是否使用bigDecimal,false可以自动转化以下类型long,Integer,short,etc --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类的地址 --> <javaModelGenerator targetPackage="com.listore.pojo" targetProject="./src/main/java"> <!-- 是否在当前路径下新加一层schema,eg:fase路径 --> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成map.xml文件 --> <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO层文件的代码 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.listore.dao" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" > <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> --> <!-- 用户表 --> <table tableName="listore_user" alias="user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"> </table> <!-- 购物车表 --> <table tableName="listore_cart" alias="cart" domainObjectName="Cart" enableUpdateByExample="false" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" ></table> <!-- 产品种类表 --> <table tableName="listore_category" alias="category" domainObjectName="Category" enableUpdateByExample="false" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"></table> <!-- 产品表 --> <table tableName="listore_product" alias="product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"><!--in database there is a text type,so in this configuration will
be VARCHAR in order to generator a String type in pojo class
--> <columnOverride column="detail" jdbcType="VARCHAR"/> <columnOverride column="sub_images" jdbcType="VARCHAR"/> </table> <!-- 订单表 --> <table tableName="listore_order" alias="order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"></table> <!-- 订单明细表 --> <table tableName="listore_order_item" alias="orderItem" domainObjectName="OrderItem" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"></table> <!-- 收货地址表 --> <table tableName="listore_shipping" alias="shipping" domainObjectName="Shipping" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"></table> <!-- 付款信息表 --> <table tableName="listore_pay_info" alias="payInfo" domainObjectName="PayInfo" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"></table> </context> </generatorConfiguration>
然后在idea的右侧Maven项目中选择下载好的mybatis-generotor.jar包双击就可以生成相应的文件了