Mybatis逆向工程的定义
-
根据数据表自动生成针对单表的
bean
类、mapper
映射文件、mapper
接口。
使用Mybatis
提供的逆向工具来实现
操作步骤
项目导入
创建数据表
创建项目
执行自动生成代码
项目导入
在porm.xml
中导入项目:
<denpendency>
<groupId>org.mybatis.generaton</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>x.x.x</version>
</denpendency>
创建数据表
User
Student
StudentCard
Website
user
:
drop table if exists user;
create table user(
`id` int(11) not null auto_increment,
`name` varchar(20) default null,
`pwd` varchar(20) default null,
primary key (`id`)
)engine = InnoDB auto_increment = 7 default charset = utf8;
student
:
drop table if exists student;
create table student(
`id` int(11) not null auto_increment,
`name` varchar(20) character set utf8 collate utf8_unicode_ci default null,
`sex` tinyint(4) default null,
`cardId` int(20) default null,
primary key (`id`),
key `cardId` (`cardId`),
constraint `student_ibfk_1` foreign key (`cardId`) references `studentcard` (`id`)
)engine = InnoDB auto_increment = 7 default charset = utf8;
studentcard
:
drop table if exists studentcard;
create table studentcard(
`id` int(20) not null auto_increment,
`studentId` int(20) default null,
`startDate` date default null,
`endDate` date default null,
primary key (`id`),
key `studentId` (`studentId`)
)engine = InnoDB auto_increment = 6 default charset = utf8;
website
:
drop table if exists website;
create table website(
`id` int(11) not null auto_increment,
`name` varchar(20) collate utf8_unicode_ci not null,
`url` varchar(20) collate utf8_unicode_ci default '',
`age` tinyint(3) unsigned not null,
`country` char(3) collate utf8_unicode_ci not null default '',
`createtime` timestamp null default current_timestamp,
primary key (`id`)
)engine = InnoDB auto_increment = 9 default charset = utf8 collate = utf8_unicode_ci;
创建项目
新建文件夹config
,在config
下创建genertorConfig.xml
文件,配置指定数据库及表
<!-- 引用mybatis逆向工程的.dtd -->
执行生成代码
-
在
pojo
包中有一部分是名字为 XxxExample 的类。类中包含以下 3 个成员变量-
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria; -
属性说明:
-
distinct 字段用于指定 DISTINCT 查询。
-
orderByClause 字段用于指定 ORDER BY 条件,这个条件没有构造方法,直接通过传递字符串值指定。
-
-
-