mybatis-generator使用详解(含Example类)
前言
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之间条件 |
mybatis-generator使用详解(含Example类)相关推荐
- mybatis generator 属性详解
2019独角兽企业重金招聘Python工程师标准>>> <?xml version="1.0" encoding="UTF-8"?> ...
- IDEA 2018 集成 MyBatis Generator 插件 详解、代码生成
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1.修改maven的pom文件 只需要将如下依赖添加到pom.xml文件中即可.(注意此处是以plu ...
- Mybatis Generator 配置详解
许多人在Java项目中都会到使用Mybatis Generator这个工具包,这里把这个工具的配置完整列一下: <?xml version="1.0" encoding=&q ...
- ibatis mysql 配置文件详解_MyBatis Generator 配置文件详解
MyBatis Generator (MBG) 是由一个XML配置文件驱动的.这个配置文件中会声明以下内容: 如何连接数据库 要生成什么对象,以及如何生成它们 哪些表需要应用于对象生成. 根元素 ge ...
- 【ES6】Generator函数详解
[ES6]Generator函数详解 一.Generator函数简介 基本概念 函数写法 yield关键字介绍 二.next方法的参数 三.for...of循环 四.关于普通throw()与Gener ...
- jQuery数组处理详解(含实例演示)
jQuery的数组处理,便捷,功能齐全. 最近的项目中用到的比较多,深感实用,一步到位的封装了很多原生js数组不能企及的功能. 最近时间紧迫,今天抽了些时间回过头来看 jQuery中文文档 中对数组的 ...
- 封装成jar包_通用源码阅读指导mybatis源码详解:io包
io包 io包即输入/输出包,负责完成 MyBatis中与输入/输出相关的操作. 说到输入/输出,首先想到的就是对磁盘文件的读写.在 MyBatis的工作中,与磁盘文件的交互主要是对 xml配置文件的 ...
- 如何配置Mybatis?(详解)
如何配置Mybatis?(详解) 官网文档: https://mybatis.org/mybatis-3/zh/getting-started.html pom.xml <?xml versio ...
- Mybatis案例超详解
Mybatis案例超详解 前言: 本来是想像之前一样继续跟新Mybatis,但由于种种原因,迟迟没有更新,快开学了,学了一个暑假,博客也更新了不少,我觉得我得缓缓,先整合一些案例练练,等我再成熟点理解 ...
- Mybatis的特性详解——动态SQL
Mybatis的特性详解--动态SQL 前言 一.动态sql的元素 1.MyBatis if标签:条件判断 2.MyBatis choose.when和otherwise标签 3.MyBatis wh ...
最新文章
- 为什么我的Button文本被迫在Lollipop上全部大写?
- Linux进程top命令作用是,Linux中top命令起什么作用呢?
- “我的父老乡亲”公益征文活动 ——讲述平凡故事,记录小的伟大
- 职场社交是一个真需求吗?
- C++ new/delete、malloc/free
- HDU 1525 Euclid's Game
- 设置框开始隐藏状态html5,小猿圈分享HTML5中form如何关闭自动完成功能的方法
- LeetCode 1752. 检查数组是否经排序和轮转得到
- 树莓派输出pwm波c语言,树莓派Ubuntu18.04使用pigpio库产生PWM波实现舵机控制
- steam授权_听歌、看番、学习甚至开车...steam好像忘了自己是个游戏平台
- C#--打包安装项目
- RiceQuant和 JoinQuant合成月k线、周k线的极简公式
- [Error] expected primary-expression before '' token是什么意思
- php爬拉钩数据,拉勾网数据两种爬取
- win10的cmd显示颜色更换为绿色的方法
- C语言调用API的方法
- Origin 如何输出高清图片
- 安装Kdevelop--我的过程
- SAN和NAS的区别
- gtsam 学习十一(ISAM2 实践)