项目中用的mybatis,依据数据库表手工写java实体类、mapper接口及mapper xml文件,是一件很郁闷的乏味的事情,而且容易出错,下面记录了一下本人用工具类自动生成这些文件的过程及碰到的坑。
- maven pom.xml 配置(只列出需要用到的2个核心依赖库)
引用的第二个包是生成mybatis相关文件需要用到的核心包,本文的主角就是它了
- 写一个配置文件,用于由表生成我们需要的文件:mybatis-generator.xml,文件内容如下:
<!-- 生成实体类及Example类的包名和位置-->
<javaModelGenerator targetPackage="com.study.mybatis.entity"
targetProject="src/main/java">
<!-- 是否让schema作为包后缀-->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格-->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件xml的包名和位置-->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<!-- 是否让schema作为包后缀-->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Dao接口的包名和位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.study.mybatis.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 用于自动生成代码的数据库表;生成哪些表-->
<table tableName="core_container">
<generatedKey column="container_id" sqlStatement="postgresql" identity="true"/>
</table>
</context>
- 写一个main类,放在src/main/java下,比如 Main
import org.mybatis.generator.api.ShellRunner;
public class Main {
public static void main(String[] args) {
args = new String[] { "-configfile", "src\\main\\resources\\mybatis-generator.xml", "-overwrite" };
ShellRunner.main(args);
}
}
4. 在eclipse中运行上面的Main
- 执行完毕,会生成四个文件:CoreContainer、CoreContainerExample、CoreContainerMapper、CoreContainerMapper.xml
CoreContainer(只列了部分字段):
public class CoreContainer {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column core_container.container_id
*
* @mbg.generated Sat Jul 11 16:29:50 CST 2020
*/
private String containerId;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column core_container.container_no
*
* @mbg.generated Sat Jul 11 16:29:50 CST 2020
*/
private String containerNo;
}
CoreContainerExample(生成的example文件内容很长,下面只截取了部分片段):
package com.thingple.basf.report.entity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class CoreContainerExample {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table core_container
*
* @mbg.generated Sat Jul 11 16:29:50 CST 2020
*/
protected String orderByClause;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table core_container
*
* @mbg.generated Sat Jul 11 16:29:50 CST 2020
*/
protected boolean distinct;
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
CoreContainerMapper(mapper接口也只截取了部分片段):
public interface CoreContainerMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table core_container
*
* @mbg.generated Sat Jul 11 16:29:50 CST 2020
*/
long countByExample(CoreContainerExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table core_container
*
* @mbg.generated Sat Jul 11 16:29:50 CST 2020
*/
int deleteByExample(CoreContainerExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table core_container
*
* @mbg.generated Sat Jul 11 16:29:50 CST 2020
*/
int deleteByPrimaryKey(String containerId);
}
CoreContainerMapper.xml(也只截取了部分片段):
<?xml version="1.0" encoding="UTF-8"?> select distinct from core_container order by ${orderByClause} 大功告成。怎么样,是否轻轻松松就由数据库表生成了所需要的mybatis相关文件?参考链接:https://blog.csdn.net/hello_junz/article/details/107287010