自动生成
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wp</groupId>
<artifactId>mybatis-auto-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.1</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
</dependencies>
<configuration>
<!--If true, then MBG will write progress messages to the build log.-->
<verbose>true</verbose>
<!--允许自动覆盖文件,第一次可覆盖,以后都不可覆盖-->
<overwrite>true</overwrite>
<!-- 自定义配置文件的名称 默认是generatorConfig.xml -->
<configurationFile>
src/main/resources/custom-mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/resources/custom-mybatis-generator.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">
<!-- 如果是粘贴复制的话,有可能上面这行会报红,按住Alt+Enter 导入即可 -->
<generatorConfiguration>
<!-- context 是逆向工程的主要配置信息 -->
<!-- id: 名字而已 作为唯一标识 -->
<!-- targetRuntime: 设置生成的文件使用于哪个 MyBatis 版本 -->
<context id="wpmysql" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://49.4.13.102:3306/test_wp?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8"
userId="root"
password="es0s1oJvm@">
<!--if true, disable generate code for tables in the MySql information schemas (sys, information_schema, performance_schema, etc.)-->
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.wp.model" targetProject="src/main/java">
<!-- 针对数据库的一个配置,是否把 schema 作为包名 -->
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mybatis/mapper" targetProject="src/main/resources">
<!-- 针对数据库的一个配置,是否把 schema 作为包名 -->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.wp.mapper" targetProject="src/main/java">
<!-- 针对数据库的一个配置,是否把 schema 作为包名 -->
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="test_wp" tableName="user" domainObjectName="WpUser" >
<!--false 代表,针对这个列名,采用驼峰式 和 下划线的转换-->
<property name="dept_id" value="false"/>
<!-- <generatedKey column="ID" sqlStatement="DB2" identity="true" />-->
<columnOverride column="name" property="userName" />
<ignoreColumn column="enable" />
<!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->
</table>
</context>
</generatorConfiguration>
运行:
mvn mybatis-generator:generate
这样,就能生成3个文件了,
一个是xml配置的sql文件—WpUserMapper.xml
一个是java实体类----WpUser
一个mapper接口—WpUserMapper
使用自动生成的文件,进行业务增删改查操作
@SpringBootTest
class UseGenerateResultApplicationTests {
@Autowired
private WpUserMapper wpUserMapper;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
void contextLoads() throws JsonProcessingException {
WpUserExample wpUserExample = new WpUserExample();
wpUserExample.createCriteria().andUserNameLike("张三");
wpUserExample.or().andAgeGreaterThan(23);
List<WpUser> wpUsers = wpUserMapper.selectByExample(wpUserExample);
System.out.println(objectMapper.writeValueAsString(wpUsers));
}
}
输出
[{"id":"001","userName":"张三","age":20,"deptId":"111","gmtCreated":1670398647376,"gmtModified":1670398647376},
{"id":"004","userName":"麻子","age":25,"deptId":"111","gmtCreated":1672021325899,"gmtModified":1672715489052},
{"id":"005","userName":"叶凡","age":30,"deptId":"111","gmtCreated":1672021368363,"gmtModified":1672715489112}]