美文网首页
一起来学习Mybatis:第三篇

一起来学习Mybatis:第三篇

作者: __y | 来源:发表于2018-10-22 14:28 被阅读168次

1.逆向工程

MyBatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需要的代码(包括mapper.xml、mapper.java、po..)。一般在开发中,常用的逆向工程方式是通过数据库的表生成代码

2.如何在idea中使用逆向工程

1.创建一个maven项目(略)
2.配置pom

<?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.0</modelVersion>

    <groupId>com.lan</groupId>
    <artifactId>com.mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <finalName>lyj</finalName>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.创建generatorConfig.xml配置文件

<?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="conn.properties" />
          -->
    <!-- 处理1,这里的jar包位置可能需要修改 -->
    <!--Mysql驱动-->
    <classPathEntry location="S:\Program Files\maven\repository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar"/>
    <!-- 指定运行环境是mybatis3的版本 -->
    <context id="testTables" targetRuntime="MyBatis3">

        <commentGenerator>
            <!-- 是否取消注释 -->
            <property name="suppressAllComments" value="true" />
            <!-- 是否生成注释代时间戳 -->
            <property name="suppressDate" value="true" />
        </commentGenerator>
        <!-- 处理2   jdbc 连接信息,看看库是否存在 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/taotao?useUnicode=true&amp;characterEncoding=UTF-8"  userId="root" password="12345">
        </jdbcConnection>

        <!--处理3   targetPackage指定模型在生成在哪个包 ,targetProject指定项目的src,-->
        <javaModelGenerator targetPackage="com.taotao.entity"
                            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--处理4   配置SQL映射文件生成信息 -->
        <sqlMapGenerator targetPackage="com.taotao.mapper"
                         targetProject="src/main/java" />
        <!-- 处理5   配置dao接口生成信息-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.taotao.dao" targetProject="src/main/java" />
        <!-- 指定数据库表 -->
        <!-- 指定数据库表 -->
        <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>

4.启动插件:


image.png

PS:没有插件的话可以在以下界面进行安装


image.png
日志提示成功就OK啦
image.png
image.png

相关文章

网友评论

      本文标题:一起来学习Mybatis:第三篇

      本文链接:https://www.haomeiwen.com/subject/hhxneftx.html