MyBatis的逆向工程生成代码及代码使用

主要过程

  1. 新建项目,按照下载的jar包中的配置文件配置【本文使用1.3.5】
  2. 命令行生成代码
  3. 使用

1

1.1 解压后目录,mybatis-generator-core-1.3.5
1.2 IDEA创建一个maven项目,pom关键部分如下:

    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
    dependencies>
    <build>
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
            plugin>
        plugins>

1.3 新建generatorConfig.xml , generator.properties文件(放在src/main/resources目录下)

  1. 连接数据库等一些设置,放在properties文件中
  2. 指定要生成代码的位置,要生成的代码包括po类, mapper.xml和mapper.java
  3. 指定数据库中想要生成哪些表,名字要对应得上
    xml配置文件示例:



<generatorConfiguration>
    
    <properties resource="genertator.properties"/>
    
    <classPathEntry location="${generator.location}" />
    
    <context id="TaotaoTables" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="false" />
            
            <property name="suppressDate" value="false" />
        commentGenerator>
        
        <jdbcConnection driverClass="${db.example.driver}"
                        connectionURL="${db.example.url}"
                        userId="${db.example.username}"
                        password="${db.example.password}">
        jdbcConnection>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="${generator.packagePojos}"
                            targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="${generator.packageMappers}"
                         targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${generator.packageMappers}"
                             targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>
        
        <table schema="" tableName="tb_content">table>
        <table schema="" tableName="tb_content_category">table>
        <table schema="" tableName="tb_item">table>
        <table schema="" tableName="tb_item_cat">table>
        <table schema="" tableName="tb_item_desc">table>
        <table schema="" tableName="tb_item_param">table>
        <table schema="" tableName="tb_item_param_item">table>
        <table schema="" tableName="tb_order">table>
        <table schema="" tableName="tb_order_item">table>
        <table schema="" tableName="tb_order_shipping">table>
        <table schema="" tableName="tb_user">table>

    context>
generatorConfiguration>

properties文件:

db.example.type=mysql
db.example.driver=com.mysql.jdbc.Driver
db.example.url=jdbc:mysql://localhost:3306/taotao
db.example.username=root
db.example.password=123456

#MBGInfo
generator.location=D:/jarpackages/mysql-connector-java-5.1.40/mysql-connector-java-5.1.40-bin.jar
generator.packagePojos=per.wanghai.taotao.pojo
generator.packageMappers=per.wanghai.taotao.mapper

1.4 点击菜单Run->Edit Configuration,然后在弹出窗体的左上角,点击+->maven
填写项目根路径以及命令mybatis-generator:generate -e,应用运行即可
MyBatis的逆向工程生成代码及代码使用_第1张图片
1.5 结果:
MyBatis的逆向工程生成代码及代码使用_第2张图片

你可能感兴趣的:(项目实践)