mybatis-generater逆向工程

项目目录结构:

mybatis-generater逆向工程_第1张图片

maven的pom.xml配置:

xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.io.llyong.mybatisgroupId>
    <artifactId>mybatis-generatorartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <mysql.version>5.1.45mysql.version>
        <mybatis.version>3.4.1mybatis.version>
        <mybatis-generator.version>1.3.5mybatis-generator.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>${mybatis.version}version>
        dependency>
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>${mybatis-generator.version}version>
        dependency>
    dependencies>

    <build>
        <finalName>${project.artifactId}finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.1version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
                <configuration>
        //这里我修改了默认的配置文件的位置和名称,所以需要手动指定
             <configurationFile>src/main/resources/mybatis/mybatis-generate-config.xmlconfigurationFile>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
                <dependencies>
        //采用的是maven方式,所以这个数据库连接依赖放到这里
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>${mysql.version}version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

project>

mybatis-generate-config.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="mybatis-generator.properties" />
    

    <context id="MysqlTables" targetRuntime="MyBatis3">
        
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <commentGenerator >
            
            <property name="suppressAllComments" value="false" />
            
            <property name="addRemarkComments" value="true"/>
        commentGenerator>
        
        <jdbcConnection driverClass="${jdbc.driver}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.username}"
                        password="${jdbc.password}">
        jdbcConnection>

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

        
        <javaModelGenerator targetPackage="${modelPackage}"
                            targetProject="${targetProject}">
            
            <property name="enableSubPackages" value="false" />
            <property name="javaFileEncoding" value="UTF-8" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="${sqlMapperPackage}"
                         targetProject="${targetProject}">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${daoMapperPackage}"
                             targetProject="${targetProject}">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>
        
                <table schema="${db_name}" tableName="${table_name}" domainObjectName="${domain_name}" selectByExampleQueryId="true">
            <property name="useActualColumnNames" value="false" />
        table>
        
        
        

        
    context>
generatorConfiguration>

配置文件中的每一项具体含义可以查看官方文档

mybatis-generator.properties配置文件:
#数据库配置
#jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.url=jdbc:mysql://localhost:3308/db_pms
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root

#执行:右键 Run As ---->Maven build ---->Goals:mybatis-generator:generate
#输出目录
targetProject=src/main/java
#modelPackage,sqlMapperPackage,daoMapperPackage 通常一致
modelPackage=com.io.ssm.module.domain
sqlMapperPackage=com.io.ssm.module.domain
daoMapperPackage=com.io.ssm.module.domain

##配置数据库和表
db_name=db_pms
table_name=cm_user
domain_name=CmUser

最后执行maven命令 即可:


1、 遇到的坑:

a)      如果是采用的是执行main方法的方式执行的,properties配置文件中对应的targetProject应该配置为以项目名开头:如我这里的

mybatis-generator/src/main/java。

b)      如果是采用maven插件的方式执行,targetProject应该配置为:src/main/java

c)      Mybatis的逆向工程默认的xml配置为,在resources目录下的generatorConfig.xml,如果需要更改需要自己手动指定配置文件的位置。

d)      如果在plugin中添加了jdbc依赖,在xml配置中可以不引入jar;如果这里没有添加依赖,则xml配置中需要引入jar


你可能感兴趣的:(mybatis-generater逆向工程)