前言
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之间条件 |