Mybatis官方提供了一个逆向工程,可以针对单表的表结构自动生成Mybatis执行所需要的代码(包括实体类、Mapper.xml映射文件和Mapper接口等);
下面以用户表为例,来创建逆向工程生成代码:
1、数据库中执行sql语句,创建用户信息表;
CREATE TABLE `tbl_user_info` (
`user_id` int NOT NULL COMMENT 'id',
`user_school` varchar(64) DEFAULT NULL COMMENT '用户所属学校',
`user_name` varchar(32) NOT NULL COMMENT '用户姓名',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='用户表';
2、完善项目结构,如下所示:
3、在pom.xml中引入逆向工程插件依赖;
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.4</version>
</dependency>
</dependencies>
4、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>
<context id="context" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
<property name="searchString" value="Example$" />
<property name="replaceString" value="Criteria" />
</plugin>
<!-- 去掉生成出来的代码的注解 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!--数据库连接信息 -->
<jdbcConnection driverClass= "com.mysql.cj.jdbc.Driver"
connectionURL= "jdbc:mysql://localhost:3306/item04?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"
userId="****" password="****"></jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.demo.projo"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.demo.mapping"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.demo.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
<!-- 配置表格映射 -->
<!--要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="tbl_user_info"
domainObjectName="UserInfo"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
需要修改文件generatorConfig.xml中生成的代码存放的位置,如下所示:
5、创建一个main方法,用于运行逆向工程;
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//实际的配置文件目录
File configFile = new File("C:\\Users\\Administrator\\IdeaProjects\\springMvc0413\\mybatisgenerator\\src\\main\\resources\\generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
System.out.println("逆向工程生成成功");
}
6、点击运行,执行main方法;
生成成功: