IntelliJ IDEA下MyBatis逆向工程生成工具的使用

xiaoxiao2021-02-28  92

1、关于MyBatis

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。 iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(sDAO)

2、MyBatis逆向工程步骤

2.1、数据库

数据库如下:

2.2、步骤

①在pom.xml中配置MyBatis逆向工程插件; <!--MyBatis自动生成工具插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> ②配置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 > <!-- mysql jar 文件位置 --> <classPathEntry location="/Users/fei/Documents/Apache/Maven/localRepository/mysql/mysql-connector-java/5.1.43/mysql-connector-java-5.1.43.jar" /> <context id="store" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> <!-- 是否去除所有自动生成的文件的时间戳,默认为false --> <property name="suppressDate" value="true"/> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/SSMBlog" userId="root" password="qwer1234"> </jdbcConnection> <!-- targetPackage:包名称(自定义) targetProject:项目路径(自定义) --> <!--定义model的包名称--> <javaModelGenerator targetPackage="com.ssmblog.entity" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 配置生成相应的实体Mapper.xml,对于Mapper3.X我们需要把type="XMLMAPPER" --> <!-- targetPackage:包名称(自定义) targetProject:项目路径(自定义) --> <sqlMapGenerator targetPackage="com.ssmblog.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 配置生成相应的接口类,对应与Mapper.xml中的一系列CRUD方法SQL语句 --> <!-- targetPackage:包名称(自定义) targetProject:项目路径(自定义) --> <javaClientGenerator targetPackage="com.ssmblog.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 通讯录人员表 --> <table schema="SSMBlog" tableName="contacts" domainObjectName="Contacts" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration> ③如图,添加一个Run命令,完成之后依次点击Apply和OK; ④完成之后点击右上角的运行按钮; ⑤此时,发现左侧项目栏中实体类文件、Mapper文件和持久层的接口文件已经自动生成;

3、总结

怎么样? 是不是很简单呢? 大家可以自己动手试试看! MyBatis自动生成工具的好处是能够提升编码的效率,在有数十张表格甚至更多的时候可以很快地生成。
转载请注明原文地址: https://www.6miu.com/read-33238.html

最新回复(0)