Mybatis自动生成mapper、实体类及mapper xml文件

项目中用的mybatis,依据数据库表手工写java实体类、mapper接口及mapper xml文件,是一件很郁闷的乏味的事情,而且容易出错,下面记录了一下本人用工具类自动生成这些文件的过程及碰到的坑。

  1. maven pom.xml 配置(只列出需要用到的2个核心依赖库)
org.postgresql postgresql org.mybatis.generator mybatis-generator-core 1.4.0 引用的第一个包是连数据库的驱动类,这个按照连接的数据库不同替换为对应的配置即可

引用的第二个包是生成mybatis相关文件需要用到的核心包,本文的主角就是它了

  1. 写一个配置文件,用于由表生成我们需要的文件:mybatis-generator.xml,文件内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
	<!-- 生成实体类及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>
  1. 写一个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

  1. 执行完毕,会生成四个文件: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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值