mybatis自动生成数据库对应的mapper接口,xml和实体类

1.加载依赖

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-core</artifactId>
          <version>${mybatis.generator.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

2. 在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>
  <!--JDBC驱动jar包的位置
    <classPathEntry location="C:/workspace/project/learning/mybatis/lib/mysql-connector-java-5.1.6.jar"/> -->
    <!--数据库驱动包路径 -->
   <classPathEntry location="C:\Users\xxxx\.m2\repository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar"/>
    <context id="mysql" targetRuntime="MyBatis3">
        <!--关闭注释 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3307/test?characterEncoding=utf-8"
                        userId="root" password="123456">
        </jdbcConnection>
        <!--生成的model 包路径 -->
        <javaModelGenerator targetPackage="com.example.demo.user.model" targetProject="src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="true"/>
        </javaModelGenerator>
        <!--生成xml mapper文件 路径 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis">
            <property name="enableSubPackages" value="ture"/>
        </sqlMapGenerator>

        <!-- 生成的Dao接口 的包路径 -->
        <!-- 生成易于使用的针对Model对象和XML配置文件的代码
        type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象
        type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
        type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.user.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="ture"/>
        </javaClientGenerator>
        <!--对应数据库表名 -->
        <table tableName="role" domainObjectName="Role" enableCountByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="permission" domainObjectName="Permission" enableCountByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="role_permission" domainObjectName="RolePermission" enableCountByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

3. 写java类自动生成mapper的xml(单表的增删改查)和数据库对应实体类

package com.example.demo.test.util;

import org.mybatis.generator.api.ShellRunner;

public class RunMybatisGeneratorShell {
    public static void main(String[] args) {
        String config = RunMybatisGeneratorShell.class.getClassLoader()
                .getResource("generatorConfig.xml").getFile();
            String[] arg = { "-configfile", config, "-overwrite" };
            ShellRunner.main(arg);
    }


}

4.运行结果

Existing file git\demo\src\main\java\com\example\demo\user\model\Role.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\dao\RoleMapper.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\model\Permission.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\dao\PermissionMapper.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\model\UserRole.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\dao\UserRoleMapper.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\model\RolePermission.java was overwritten
Existing file git\demo\src\main\java\com\example\demo\user\dao\RolePermissionMapper.java was overwritten

MyBatis Generator finished successfully, there were warnings.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值