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

 

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,在点那个三角形运行就可以自动生成了

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值