Mybatis Generator dao层代码生成
介绍
我们在使用mybatis开发的时候,需要去写表的基本的crud,这些咱们通常定义到dao层里面去,而mybatis generator可以帮我们把这件事情自动完成,大大节省时间并且可以规范dao层的代码。
springboot 集成
引入pom
主要是build部分
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.7</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.hh</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>demo</description><properties><java.version>8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.4.2</version><configuration><verbose>true</verbose><overwrite>true</overwrite></configuration><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency></dependencies></plugin></plugins></build></project>
配置文件
默认名称是generatorConfig.xml
<!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="simple" targetRuntime="MyBatis3"><jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/ry?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8"userId="root" password="root"/><javaModelGenerator targetPackage="com.hh.dao.model" targetProject="src/main/java"/><sqlMapGenerator targetPackage="hh.mapper" targetProject="src/main/resources"/><javaClientGenerator type="XMLMAPPER" targetPackage="com.hh.dao.mapper" targetProject="src/main/java"><property name="enableSubPackages" value="false"/><property name="trimStrings" value="true"/><property name="mapUnderscoreToCamelCase" value="true"/></javaClientGenerator><table tableName="base_user"/></context>
</generatorConfiguration>
生成代码
创建表
CREATE TABLE `base_user` (`user_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',`tenant_id` int(11) DEFAULT NULL COMMENT '租户id',`user_name` varchar(30) DEFAULT '' COMMENT '用户昵称',`remark` varchar(500) DEFAULT NULL COMMENT '备注',PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户信息表';
在项目的maven中双击生成代码插件
生成的类
完成。