温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis中逆向工程的示例分析

发布时间:2021-09-14 10:34:52 来源:亿速云 阅读:189 作者:小新 栏目:MySQL数据库

这篇文章主要介绍了MyBatis中逆向工程的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

MyBatis的逆向工程

一:什么是逆行工程。

MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。 当数据库表比较多的时候,重复的创建pojo对象和简单的数据库表的(CRUD)操作的mapper,效率低,官方给出了使用mybatis Generator用来根据数据库表逆向生成pojo和mapper文件,极大的方便开发。

二:简单教程

  • maven的pom.xml中添加插件

 <packaging>jar</packaging> <dependencies>         <!-- 添加对mybatis的依赖 -->         <dependency>             <groupId>org.mybatis</groupId>             <artifactId>mybatis</artifactId>         </dependency>         <dependency>             <groupId>org.mybatis</groupId>             <artifactId>mybatis-spring</artifactId>         </dependency>         <dependency>             <groupId>com.github.miemiedev</groupId>             <artifactId>mybatis-paginator</artifactId>         </dependency>         <dependency>             <groupId>com.github.pagehelper</groupId>             <artifactId>pagehelper</artifactId>         </dependency>         <!-- MySql -->         <dependency>             <groupId>mysql</groupId>             <artifactId>mysql-connector-java</artifactId>         </dependency>         <!-- 连接池 -->         <dependency>             <groupId>com.alibaba</groupId>             <artifactId>druid</artifactId>         </dependency>         <dependency>             <groupId>org.mybatis.generator</groupId>             <artifactId>mybatis-generator-core</artifactId>             <version>1.3.6</version>         </dependency>     </dependencies>     <build>         <plugins>             <!-- mybatis逆向工程 -->             <plugin>                 <groupId>org.mybatis.generator</groupId>                 <artifactId>mybatis-generator-maven-plugin</artifactId>                 <version>1.3.7</version>                 <configuration>                     <!--配置文件的位置-->                     <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>                     <verbose>true</verbose>                     <overwrite>true</overwrite>                 </configuration>             </plugin>         </plugins>     </build>
  • 创建配置文件 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="jdbc.properties"/>     <!--数据库驱动文件  需要修改 TODO-->     <classPathEntry             location="/Volumes/Tool/JAVA_Tool/maven/Maven_Repository/mysql/mysql-connector-java/5.1.32/mysql-connector-java-5.1.32.jar"/>     <context id="context" targetRuntime="MyBatis3Simple">         <commentGenerator>             <!-- 是否去除自动生成的注释,true:是;false:否 -->             <property name="suppressAllComments" value="true"/>             <property name="suppressDate" value="false"/>         </commentGenerator>         <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->         <jdbcConnection driverClass="${jdbc.driverClassName}"                         connectionURL="${jdbc.url}"                         userId="${jdbc.username}"                         password="${jdbc.password}"/>         <!--默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,-->         <!--为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->         <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->         <javaTypeResolver>             <property name="forceBigDecimals" value="false"/>         </javaTypeResolver>         <!--生成PO类的位置 配置生成的实体包-->         <!--targetPackage:生成的实体包位置,默认存放在src目录下-->         <!--targetProject:相对路径  路径拼接结果 src/main/java/one/domain -->         <javaModelGenerator targetPackage="one.domain" targetProject="src/main/java">             <!-- enableSubPackages:是否让schema作为包的后缀 -->             <property name="enableSubPackages" value="false"/>             <!-- 从数据库返回的值被清理前后的空格 -->             <property name="trimStrings" value="true"/>         </javaModelGenerator>         <!-- 实体包对应映射文件位置及名称,默认存放在src目录下 同上 -->         <sqlMapGenerator targetPackage="MapperXml" targetProject="src/main/resources">             <!-- enableSubPackages:是否让schema作为包的后缀 -->             <property name="enableSubPackages" value="false"/>         </sqlMapGenerator>         <!--targetPackage:mapper接口生成的位置-->         <javaClientGenerator targetPackage="one.mapper" type="XMLMAPPER" targetProject="src/main/java">             <!-- enableSubPackages:是否让schema作为包的后缀 -->             <property name="enableSubPackages" value="false"/>         </javaClientGenerator>         <!--配置表-->         <!--schema:不用填写-->         <!--tableName: 表名 TODO-->         <!--enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:-->         <!--去除自动生成的注释例子-->         <table tableName="tb_user" enableCountByExample="false" enableDeleteByExample="false"                enableSelectByExample="false" enableUpdateByExample="false"/>     </context> </generatorConfiguration>
  • 运行Generator类即可生成

感谢你能够认真阅读完这篇文章,希望小编分享的“MyBatis中逆向工程的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI