• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
FlyWIHTSKY
帮助1

项目中用的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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgacafg
系列文章
更多 icon
同类精品
更多 icon
继续加载