Java项目中使用Mybatis Plugins---MyBatis-generator来进行自动生成prjo、Dao、xml

  随着MyBatis使用的越来越广泛,越来越多的插件可以帮助我们更好的集成和使用MyBatis,今天就要介绍一个可以通过数据库自动生成用于自动生成prjo、xml、dao层代码的一个插件-MyBatis generate。
  1、MyBatis Generator的引用
  MyBatis Generate可以直接在Intellij IDEA中通过Maven的pom.xml直接引入对应的generator Jar包。就可以自动完成插件的引用。
  

  <build>
    <finalName>mmallfinalName>
    <plugins>
      <plugin>
        <groupId>org.mybatis.generatorgroupId>
        <artifactId>mybatis-generator-maven-pluginartifactId>
        <version>1.3.2version>
        <configuration>
          <verbose>trueverbose>
          <overwrite>trueoverwrite>
        configuration>
      plugin>

      
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <configuration>
          <source>1.7source>
          <target>1.7target>
          <encoding>UTF-8encoding>
          <compilerArguments>
            <extdirs>${project.basedir}/src/main/webapp/WEB-INF/libextdirs>
          compilerArguments>
        configuration>
      plugin>
    plugins>

  build>

2、MyBatis Generator的使用
  这里我们需要在对应的Project下面的resources 目录下创建一个generatorConfig.xml 文件用以配置自动生成的相关代码和配置文件的属性和范围。具体代码可以参考如下设置:
  




<generatorConfiguration>
    
    <properties resource="datasource.properties">properties>

    
    <classPathEntry location="${db.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">

        
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        commentGenerator>

        
        <jdbcConnection
                driverClass="${db.driverClassName}"
                connectionURL="${db.url}"
                userId="${db.username}"
                password="${db.password}">
        jdbcConnection>


        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>


        
        
        <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false"/>
            
            <property name="constructorBased" value="true"/>
            
            <property name="trimStrings" value="true"/>
            
            <property name="immutable" value="false"/>
        javaModelGenerator>

        
        
        <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>

        

        
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>

        <!- 这里的table标签里面就对应的数据库里面的table-->
        <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">
            <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>


        
    context>
generatorConfiguration>

这里是与之配套的properties配置文件已经数据库的database内table情况。

db.driverLocation=C:\\Users\\Administrator\\my4\\mmall\\src\\main\\webapp\\WEB-INF\\lib\\mysql-connector-java-5.1.6-bin.jar
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://192.168.175.131:3306/mmall_learning?characterEncoding=utf-8
db.username=mmall
db.password=123456

db.initialSize=20
db.maxActive=50
db.maxIdle=20
db.minIdle=10
db.maxWait=10
db.defaultAutoCommit=true
db.minEvictableIdleTimeMillis=3600000

这里的数据库参数要注意,需要和自己本地的数据做适配,此外db.driverLocation中是你本地中存放connector jar包的地址。

Java项目中使用Mybatis Plugins---MyBatis-generator来进行自动生成prjo、Dao、xml_第1张图片

3、生成的效果


Java项目中使用Mybatis Plugins---MyBatis-generator来进行自动生成prjo、Dao、xml_第2张图片
这是Dao层
Java项目中使用Mybatis Plugins---MyBatis-generator来进行自动生成prjo、Dao、xml_第3张图片
这是Pojo层
Java项目中使用Mybatis Plugins---MyBatis-generator来进行自动生成prjo、Dao、xml_第4张图片
这里是对应的xml实现配置文件,里面已经自动生成了部分相关的SQL语句。

你可能感兴趣的:(Java实战,Java面试常用基础知识点)