什么是mybatis逆向工程?
mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml,pojo…),可以让程序员将更多的精力放在业务逻辑上。
实际开发中,常用的逆向工程方式:由数据库的表生成java代码。就算是在比较大型的项目中,也很少有多表关联查询,所以说mybatis逆向工程的作用还是很大的。
mybatis逆向工程的生成:
首先先创建一个maven项目,以便于管理自己的项目,也方便自己导入所需要的jar包。
(由于我设置了maven的本地仓库,所以新建的项目先把maven的仓库路径改为本地仓库)
在pom.xml配置文件下导入所需要的mysql的jar包
然后在resources下创建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>
<!--(1)配置mysql 驱动jar包路径.用了绝对路径-->
<classPathEntry location="D:\MavenResporitys\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar"></classPathEntry>
<context id="scm_mysql_tables" targetRuntime="Mybatis3">
<!--防止生成的代码中有很多注释,加入下面的配置控制-->
<commentGenerator>
<property name="suppressAllComments" value="true"></property>
<property name="suppressDate" value="true"></property>
</commentGenerator>
<!--(2)连接数据库-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--(3)数据表对应的model层-->
<javaModelGenerator targetPackage="com.mybatis.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
<property name="constructorBased" value="true"/>
</javaModelGenerator>
<!-- (4)sql mapper 映射配置文件 -->
<sqlMapGenerator targetPackage="com.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- (5)mybatis3中的mapper接口 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--(6)数据表进行生成操作 schema:相当于库名; tableName:表名;domainObjectName:对应的DO-->
<table schema="mybatis" tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
在写完配置文件后,进行如下操作。
双击后,程序运行完会自动生成entity类,以及mapper接口和mapper映射文件。
以上的操作就是使用mybatis逆向工程生成一张表的entity类,以及mapper接口和mapper映射文件。
作者:将来嘚将来
来源:CSDN
原文:https://blog.csdn.net/Iamarookie999/article/details/91895700
版权声明:本文为博主原创文章,转载请附上博文链接!