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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。