MyBatis自动生成实体类、映射、Dao接口

xiaoxiao2025-08-18  49

 

1、在web.xml配置,加入以下代码(加在<plugins><plugins/>标签中)

<plugin>   <groupId>org.mybatis.generator</groupId>   <artifactId>mybatis-generator-maven-plugin</artifactId>   <version>1.3.5</version>   <configuration>     <!--配置文件的位置-->    <overwrite>true</overwrite>     <verbose>true</verbose>   </configuration></plugin>

 

2、在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>     <classPathEntry location="Maven中央仓库MySQL依赖中jar包的位置"/>     <context id="context1" targetRuntime="项目名">         <commentGenerator>             <property name="suppressAllComments" value="true"/>         </commentGenerator>         <!-- 数据库链接URL、用户名、密码 -->        <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/blog"         userId="root" password="root"> -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver"                        connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true" userId="数据库用户名"                        password="数据库密码">         </jdbcConnection>         <!-- 生成模型的包名和位置 -->        <javaModelGenerator targetPackage="实体类包的全路径名"                            targetProject="src/main/java">             <!--<property name="enableSubPackages" value="true"/>-->            <!--<property name="trimStrings" value="true"/>-->        </javaModelGenerator>         <!-- 生成的映射文件包名和位置 -->        <sqlMapGenerator targetPackage="映射包的全路径名"                         targetProject="src/main/java">             <!--<property name="enableSubPackages" value="true"/>-->        </sqlMapGenerator>         <!-- 生成DAO的包名和位置 -->        <javaClientGenerator type="XMLMAPPER"                             targetPackage="dao包的全路径名" targetProject="src/main/java">             <property name="enableSubPackages" value="true"/>         </javaClientGenerator><table schema="数据库表名" tableName="数据库表名" domainObjectName="设置实体类名"></table>

 

如果有多个表,以上类推     </context></generatorConfiguration>

 

 

4.1、选中Edit Configurations(如下图)

 

4.2选中加号(如下图)

 

 

4.3、选中Maven

 

4.4、在Command line填入 mybatis-generator:generate -e ,再Apply应用,ok完成

 

4.5、选中Unnamed,在点那个三角形运行就可以自动生成了

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-5035025.html

最新回复(0)