mybatis自动生成实体类
时间: 2023-08-29 21:12:49 浏览: 53
MyBatis可以通过逆向工程生成实体类,步骤如下:
1. 在项目中添加MyBatis Generator插件依赖。
2. 创建一个MyBatis Generator配置文件,通常命名为`generatorConfig.xml`。
3. 配置MyBatis Generator,包括数据源、实体类生成路径、表名和实体类名的映射关系等。
4. 运行MyBatis Generator,自动生成实体类。
以下是一个简单的MyBatis Generator配置文件示例:
```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="mysql" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test"
userId="root"
password="root">
</jdbcConnection>
<!-- 实体类生成路径 -->
<javaModelGenerator targetPackage="com.example.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 映射文件生成路径 -->
<sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- DAO接口生成路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 表名和实体类名映射关系 -->
<table tableName="user" domainObjectName="User"></table>
</context>
</generatorConfiguration>
```
运行MyBatis Generator可以使用命令行工具或者IDEA插件,具体操作可以参考MyBatis官方文档。