MyBatis-generator与generatorConfig.xml结合逆向生成实体、mapper和dao层

MyBatis-generator与generatorConfig.xml结合逆向生成实体、mapper和dao层

结合上篇博客,今天分享下个人的心得,刚接触ssm时知道有逆向生成这回事,于是就在网上各种搜。

这篇说这个嘛很简单,什么dos命令。那篇说怎么怎么样,最后什么也没学到。。。。

好了,言归正传。

一.准备mybatis-generator jar包

在pom.xml中配置获取mybatis-generator jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>comgroupId>
  <artifactId>webartifactId>
  <packaging>warpackaging>
  <version>1.0-SNAPSHOTversion>
  <name>mmall Maven Webappname>
  <url>http://maven.apache.orgurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    <maven.compiler.encoding>UTF-8maven.compiler.encoding>

    <org.springframework.version>4.0.0.RELEASEorg.springframework.version>
    <org.mybatis.version>3.4.1org.mybatis.version>
    <org.mybatis.spring.version>1.3.0org.mybatis.spring.version>
  properties>

  <dependencies>

    <dependency>
      <groupId>org.apache.tomcatgroupId>
      <artifactId>tomcat-servlet-apiartifactId>
      <version>7.0.64version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${org.springframework.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-oxmartifactId>
      <version>${org.springframework.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${org.springframework.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${org.springframework.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${org.springframework.version}version>
    dependency>


    <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjweaverartifactId>
      <version>1.7.3version>
    dependency>

    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>${org.mybatis.spring.version}version>
    dependency>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>${org.mybatis.version}version>
    dependency>

    <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjrtartifactId>
      <version>1.6.11version>
    dependency>

    <dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-mapper-aslartifactId>
      <version>1.9.12version>
    dependency>

    <dependency>
      <groupId>commons-dbcpgroupId>
      <artifactId>commons-dbcpartifactId>
      <version>1.4version>
      
    dependency>


    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>1.1.2version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
      <version>1.1.2version>
      <scope>compilescope>
    dependency>

    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.6version>
    dependency>

    <dependency>
      <groupId>com.google.guavagroupId>
      <artifactId>guavaartifactId>
      <version>20.0version>
    dependency>


    <dependency>
      <groupId>org.apache.commonsgroupId>
      <artifactId>commons-lang3artifactId>
      <version>3.5version>
    dependency>


    <dependency>
      <groupId>commons-collectionsgroupId>
      <artifactId>commons-collectionsartifactId>
      <version>3.2.1version>
    dependency>


    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      
    dependency>

    <dependency>
      <groupId>joda-timegroupId>
      <artifactId>joda-timeartifactId>
      <version>2.3version>
    dependency>


    
    <dependency>
      <groupId>org.hashidsgroupId>
      <artifactId>hashidsartifactId>
      <version>1.0.1version>
    dependency>


    
    <dependency>
      <groupId>commons-netgroupId>
      <artifactId>commons-netartifactId>
      <version>3.1version>
    dependency>

    

    
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.2.2version>
    dependency>

    <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
      <version>2.0.1version>
    dependency>


    

    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>4.1.0version>
    dependency>

    <dependency>
      <groupId>com.github.miemiedevgroupId>
      <artifactId>mybatis-paginatorartifactId>
      <version>1.2.17version>
    dependency>

    <dependency>
      <groupId>com.github.jsqlparsergroupId>
      <artifactId>jsqlparserartifactId>
      <version>0.9.4version>
    dependency>


    
    <dependency>
      <groupId>commons-codecgroupId>
      <artifactId>commons-codecartifactId>
      <version>1.10version>
    dependency>
    <dependency>
      <groupId>commons-configurationgroupId>
      <artifactId>commons-configurationartifactId>
      <version>1.10version>
    dependency>
    <dependency>
      <groupId>commons-langgroupId>
      <artifactId>commons-langartifactId>
      <version>2.6version>
    dependency>
    <dependency>
      <groupId>commons-logginggroupId>
      <artifactId>commons-loggingartifactId>
      <version>1.1.1version>
    dependency>
    <dependency>
      <groupId>com.google.zxinggroupId>
      <artifactId>coreartifactId>
      <version>2.1version>
    dependency>
    <dependency>
      <groupId>com.google.code.gsongroupId>
      <artifactId>gsonartifactId>
      <version>2.3.1version>
    dependency>
    <dependency>
      <groupId>org.hamcrestgroupId>
      <artifactId>hamcrest-coreartifactId>
      <version>1.3version>
    dependency>

    <dependency>
      <groupId>redis.clientsgroupId>
      <artifactId>jedisartifactId>
      <version>2.9.0version>
    dependency>
  dependencies>


  <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>


project>

以上是我用的配置文件,导入后获得下图Jar包

MyBatis-generator与generatorConfig.xml结合逆向生成实体、mapper和dao层_第1张图片

二.获取数据库连接

MyBatis-generator与generatorConfig.xml结合逆向生成实体、mapper和dao层_第2张图片

三.重点是generatorConfig.xml文件

xml version="1.0" encoding="UTF-8"?>
 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="C:\Users\user\.m2\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/>

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

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

        
        <jdbcConnection
                driverClass="${driver}"
                connectionURL="${url}"
                userId="${username}"
                password="${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 tableName="t_user" domainObjectName="t_user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>


        
    context>
generatorConfiguration>

注意三点:

1.Mysql Jar包引用路径

2.包名只需命名就可自动生成,不用手动创建

3.tableName与表名相对应


MyBatis-generator与generatorConfig.xml结合逆向生成实体、mapper和dao层_第3张图片

MyBatis-generator与generatorConfig.xml下载:https://download.csdn.net/download/blogsdemo/10377857


你可能感兴趣的:(MyBatis-generator与generatorConfig.xml结合逆向生成实体、mapper和dao层)