自动化配置(1):利用Mybatis-generator自动生成实体类,接口,mapper.xml
Mybatis自动化★★
前言:
本文介绍在IDEA中Maven项目下,利用Mybatis-generator插件自动生成JavaBeans实体类,dao层接口以及mapper文件,在数据库使用多表时大大提高效率,减小重复机械的劳动,不易出错
使用步骤:
1.创建Maven项目
2.添加依赖包(pom.xml)
<!-- 反向自动生成 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
3.加载插件(pom.xml)
<!--反向自动生成实体类-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的路径-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</plugin>
4.修改配置文件(
4.1硬编码方式(不推荐)
在src/main/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>
<!--数据库驱动jar -->
<classPathEntry
location="D:\apache-maven-3.3.3-bin\maven_repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar" />
<context id="MyBatis" targetRuntime="MyBatis3">
<!--去除注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm002"
userId="root"
password="">
</jdbcConnection>
<!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建
使用Maven生成在target目录下,会自动创建) -->
<javaModelGenerator targetPackage="com.pengluo.bean"
targetProject="E:\git\web\ssm002\src\main\java">
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成SQLmapper文件 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="E:\git\web\ssm002\src\main\resources">
</sqlMapGenerator>
<!--生成Dao文件,生成接口 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.pengluo.dao"
targetProject="E:\git\web\ssm002\src\main\java">
</javaClientGenerator>
<table tableName="bills" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="billtype" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
4.2配置文件方式(主流)
- 在src/main/resources 新建文件generatorConfig.xml
- 在src/main/resources 新建文件generator.properties
实现思路:在属性文件generator.properties中配置好属性,在generatorConfig.xml用
4.2.1.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="generator.properties"></properties>
<!--数据库驱动jar -->
<classPathEntry location="${jdbc.driverLocation}" />
<context id="MyBatis" targetRuntime="MyBatis3">
<!--去除注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接 -->
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建
使用Maven生成在target目录下,会自动创建) -->
<javaModelGenerator targetPackage="com.pengluo.bean"
targetProject="${project.path}\src\main\java">
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成SQLmapper文件 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="${project.path}\src\main\resources">
</sqlMapGenerator>
<!--生成Dao文件,生成接口 -->
<!-- type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.pengluo.dao"
targetProject="${project.path}\src\main\java">
</javaClientGenerator>
<!--Mysql中需要映射的表名 -->
<table tableName="bills" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="billtype" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
4.2.2.generator.properties
#格式:key=value
#数据库驱动jar包的地址
jdbc.driverLocation=D:\\apache-maven-3.3.3-bin\\maven_repository\\mysql\\mysql-connector-java\\5.1.38\\mysql-connector-java-5.1.38.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm002?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=
#指定javaBeans等文件存放的项目路径
project.path=E:\\git\\web\\Mybatis-generator
5.点击 菜单run中Edit Configurations-> 点击+号,选择maven,输入命令,运行。
mybatis-generator:generate -e
7.引用:``
Idea使用Mybatis Generator 自动生成代码: https://yq.aliyun.com/articles/640673
MyBatis配置文件mybatis-config详解:https://blog.csdn.net/hzj1998/article/details/10296086
Maven之插件与命令: https://www.jianshu.com/p/741d9c69a6a4
Mybatis自动化编程之better-mybatis-generator使用: https://blog.csdn.net/cywtd/article/details/1050974
米罗mirror:欢迎下次光临