mybatis-generator使用详解(含Example类)

本文介绍MyBatis-Generator的使用方法,包括配置generator.xml文件以生成持久层代码,如实体类、映射文件和DAO接口等。并详细解析生成的实体类和Example类的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

MyBatis-Generator:简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以根据数据库表生成持久层代码,即对应的映射文件,mapper接口,以及实体类。

下载

在我提供的github仓库中,地址:mybatis-generator

配置

配置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">
<generatorConfiguration>
	<!-- 数据库驱动包位置 -->
	<classPathEntry location="C:\Users\lenovo\Desktop\generator\mysql-connector-java-5.1.38.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 数据库链接URL、用户名、密码 -->
		 <jdbcConnection driverClass="com.mysql.jdbc.Driver"
		  connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true" userId="root" password="">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		
		
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="com.demo.schoolmanager.entity" targetProject="E:\MyProject\CSDN_Blog_Solution\mybatis-generator\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		
		
		<!-- 生成的映射文件包名和位置 -->
		<sqlMapGenerator targetPackage="com.demo.schoolmanager.mapping" targetProject="E:\MyProject\CSDN_Blog_Solution\mybatis-generator\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		
		
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.demo.schoolmanager.dao" targetProject="E:\MyProject\CSDN_Blog_Solution\mybatis-generator\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		
		
		<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
		<!-- 如果要生产Example类,则将下面的false改为true -->
		<table tableName="%"  enableCountByExample="false"
			enableUpdateByExample="false"
			enableDeleteByExample="false"
			enableSelectByExample="false"
			selectByExampleQueryId="false" />
		
<!--		<table tableName="Desk" domainObjectName="Desk" enableCountByExample="false" -->
<!--			enableUpdateByExample="false" -->
<!--			enableDeleteByExample="false" -->
<!--			enableSelectByExample="false" -->
<!--			selectByExampleQueryId="false" />-->

<!--		<table tableName="Emp" domainObjectName="Emp" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />-->
<!--		<table tableName="Menu" domainObjectName="Menu" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />-->
<!--		<table tableName="Order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />-->
<!--		<table tableName="order_detail" domainObjectName="OrderDetail" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />-->
	</context>
</generatorConfiguration>

使用

在当前目录打开cmd命令行界面,输入生成语句

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

生成类详解

实体类

实体类是根据数据库中的表以及表中的字段生成的,一个表生成一个实体类,实体类名对应表名,成员变量名对应表中的字段名。在根据数据库生成实体类时有以下生成规则:
表名生成的实体类名首字母大写:student -> Student
字段名生成成员变量名的规则为:
使用下划线:成员变量名生成规则为驼峰法 student_name -> studentName
不使用下划线:大写一律改为小写 studentName -> studentname
因此在设计数据库时,数据库中的表及字段名应全为小写且最好使用下划线。

在数据库设计时,如果数据表的主键不止一个,即由多个字段构成唯一标识,generator则生成一个新的Key类。
实例:一个sc表中含有三个字段:student_id,course_id,score,其中student_id和course_id是主键,则生成实体类Sc和Key类ScKey,并且Sc继承ScKey。
ScKey的成员变量:studentId,courseId
Sc的成员变量:score
但因为Sc继承了ScKey,实际上studentId和courseId也算是Sc的成员变量。

实体类Example

该类的三个成员变量:

protected boolean distinct用于指定DISTINCT查询。
protected String orderByClause用于指定ORDER BY条件,这个条件没有构造方法,直接通过传递字符串值指定。
protected List< Criteria >oredCriteria用于自定义查询条件。

使用:

//创建Example对象
CourseExample courseExample = new CourseExample();
//去除重复对象
courseExample .setDistinct(false);
//设置查询条件
CourseExample.Criteria criteria = courseExample.createCriteria();//创建查询条件
criteria.andTeacheridEqualTo(id);//设置的查询条件为teacherId等于传入的参数
//执行查询
List<Course> list = courseMapper.selectByExample(courseExample);

Example的方法,去除重复和设置排列条件

方法说明
example.setDistinct(false);去除重复,boolean型,true为选择不重复的记录。
example.setOrderByClause(“字段名 ASC”);添加升序排列条件,DESC为降序

Criteria的方法,定义SQL 语句where后的查询条件。

方法说明
criteria.andXxxIsNull;添加字段xxx为null的条件
criteria.andXxxIsNotNull;添加字段xxx不为null的条件
criteria.andXxxEqualTo(value);添加xxx字段等于value条件
criteria.andXxxNotEqualTo(value);添加xxx字段不等于value条件
criteria.andXxxGreaterThan(value);添加xxx字段大于value条件
criteria.andXxxGreaterThanOrEqualTo(value);添加xxx字段大于等于value条件
criteria.andXxxLessThan(value);添加xxx字段小于value条件
criteria.andXxxLessThanOrEqualTo(value);添加xxx字段小于等于value条件
criteria.andXxxIn(List<?>);添加xxx字段值在List<?>条件
criteria.andXxxNotIn(List<?>);添加xxx字段值不在List<?>条件
criteria.andXxxLike(“%”+value+”%”);添加xxx字段值为value的模糊查询条件
criteria.andXxxNotLike(“%”+value+”%”);添加xxx字段值不为value的模糊查询条件
criteria.andXxxBetween(value1,value2);添加xxx字段值在value1和value2之间条件
criteria.andXxxNotBetween(value1,value2);添加xxx字段值不在value1和value2之间条件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值