IDEA+Maven实现MyBatis逆向工程

大家好,本周博主为大家带来mybatis逆向工程的实现,具体如下

什么是MyBatis逆向工程

MyBatis Generator,简称mbg,是专门为mybatis定制的代码生成器。

mybatis逆向工程,根据数据表生成Bean实体类、dao接口、mapper映射文件,封装了单表的增删改查操作,但是连表查询需要自定义。

使用Mybatis逆向工程前提要有数据库和数据表

使用MyBatis逆向工程的好处

自动生成项目所需要的实体类,mapper接口,和xml文件,简化开发流程

如何实现MyBatis的逆向工程?

这里采用IDEA+Maven的方式实现逆向工程,下面进入具体代码编写

项目结构图IDEA+Maven实现MyBatis逆向工程_逆向工程

新建Maven项目

File ---> New ---> Project IDEA+Maven实现MyBatis逆向工程_maven_02Maven ---> Next ---> 命名为MyBatisGenerator ---> 单击Finsh完成IDEA+Maven实现MyBatis逆向工程_java_03

编写配置文件

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

<groupId>org.examplegroupId>
<artifactId>MyBatisGenerateartifactId>
<version>1.0-SNAPSHOTversion>


<build>
<plugins>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.5version>
<configuration>

<verbose>trueverbose>

<overwrite>trueoverwrite>
<configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
configuration>

<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
dependencies>
plugin>
plugins>
build>
project>

新建database.properties配置文件

#连接数据库
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssmbuild?useSSL=true
jdbc.username = root
jdbc.password = 111111

#要生成的数据表
jdbc.table.user = user
jdbc.table.books = books

新建generatorConfig.xml配置文件

 version="1.0" encoding="UTF-8"?>

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

<properties resource="database.properties"/>




<context id="default" targetRuntime="MyBatis3">

<commentGenerator>
<property name="suppressDate" value="true"/>

<property name="suppressAllComments" value="true"/>
commentGenerator>

<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">jdbcConnection>

<javaTypeResolver>



<property name="forceBigDecimals" value="false"/>
javaTypeResolver>


<javaModelGenerator targetPackage="com.wanshi.entity"
targetProject="src/main/java">

<property name="enableSubPackages" value="false"/>

<property name="constructorBased" value="true"/>

<property name="trimStrings" value="true"/>

<property name="immutable" value="false"/>
javaModelGenerator>

<sqlMapGenerator targetPackage="com.wanshi.mapper"
targetProject="src/main/java">

<property name="enableSubPackages" value="false"/>
sqlMapGenerator>

<javaClientGenerator type="XMLMAPPER"
targetPackage="com.wanshi.mapper" targetProject="src/main/java">

<property name="enableSubPackages" value="false"/>
javaClientGenerator>

<table tableName="${jdbc.table.user}" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">table>
<table tableName="${jdbc.table.books}" domainObjectName="Books"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">table>
context>
generatorConfiguration>

Maven启动方式

Add Configuration ---> + ---> MavenIDEA+Maven实现MyBatis逆向工程_java_04编写Maven启动命令,---> Apply同意 ---> okIDEA+Maven实现MyBatis逆向工程_逆向工程_05

测试

OK,已经编写完毕,测试一把IDEA+Maven实现MyBatis逆向工程_java_06

结语

ok,到这里MyBatis逆向工程已成功生成,是不是很简单呢,赶快去试试吧~

如果本文对你有帮助的话,不妨给博主来个一键三连(点赞+评论+收藏