Hibernate 根据写好的 pojo启动服务器会自动帮助我们生成对应的数据表。
Mybatis Generator简称 MBG,是一个专门为 MyBatis和 ibatis框架使用者提供的代码生成器。也可以快速的根据数据表生成对应的pojo类、Mapper接口、Mapper文件,甚至生成QBC风格的查询对象。
一般在项目中,根据MyBatis Generator的XML配置文件设置生成简单的CRUD,但是复杂的查询或者有关关联的操作还是需要我们写SQL完成。
官方项目地址:https://github.com/mybatis/generator
官方文档地址:http://www.mybatis.org/generator/
新建一个 maven web项目,pom.xml引出 mybatis-generator依赖
org.mybatis
mybatis
3.4.6
org.mybatis.generator
mybatis-generator-core
1.3.7
mysql
mysql-connector-java
5.1.46
一、使用 targetRuntime="MyBatis3Simple" 生成
MyBatis3Simple:类似MyBatis3,只是不生成XXXBySample
1、MBG 的配置文件,一般起名为 generatorConfig.xml:一般项目中使用这些就OK
2、MBG启动类:使用 Java代码运行,也可使用 Maven插件运行
固定写法,可参考官方文档:http://www.mybatis.org/generator/running/runningWithJava.html
public class App {
public static void main(String[] args) {
try {
App app = new App();
app.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
public void generator() throws Exception{
List warnings = new ArrayList();
boolean overwrite = true;
// 指定 逆向工程配置文件,这两种都可以
String path = this.getClass().getClassLoader().getResource("generatorConfig.xml").getPath();
File configFile = new File(path);
// File configFile = new File("D:/JDWorkspaceIDEA19/jqmybatis/src/main/resources/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
3、生成的文件
delete from t_user
where id = #{id,jdbcType=BIGINT}
insert into t_user (id, username, pazzword,
salary, reg_date)
values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{pazzword,jdbcType=VARCHAR},
#{salary,jdbcType=DECIMAL}, #{regDate,jdbcType=TIMESTAMP})
update t_user
set username = #{username,jdbcType=VARCHAR},
pazzword = #{pazzword,jdbcType=VARCHAR},
salary = #{salary,jdbcType=DECIMAL},
reg_date = #{regDate,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
select id, username, pazzword, salary, reg_date
from t_user
where id = #{id,jdbcType=BIGINT}
select id, username, pazzword, salary, reg_date
from t_user
二、使用 targetRuntime="MyBatis3" 生成
MyBatis3:默认的值,生成基于MyBatis3.x以上版本的内容,包括XXXBySample
把 MBG配置文件 那里改为 MyBatis3 即可,启动类一样,需要生成那张表,运行它即可
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
id, username, pazzword, salary, reg_date
select
distinct
from t_user
order by ${orderByClause}
select
from t_user
where id = #{id,jdbcType=BIGINT}
delete from t_user
where id = #{id,jdbcType=BIGINT}
delete from t_user
insert into t_user (id, username, pazzword,
salary, reg_date)
values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{pazzword,jdbcType=VARCHAR},
#{salary,jdbcType=DECIMAL}, #{regDate,jdbcType=TIMESTAMP})
insert into t_user
id,
username,
pazzword,
salary,
reg_date,
#{id,jdbcType=BIGINT},
#{username,jdbcType=VARCHAR},
#{pazzword,jdbcType=VARCHAR},
#{salary,jdbcType=DECIMAL},
#{regDate,jdbcType=TIMESTAMP},
select count(*) from t_user
update t_user
id = #{record.id,jdbcType=BIGINT},
username = #{record.username,jdbcType=VARCHAR},
pazzword = #{record.pazzword,jdbcType=VARCHAR},
salary = #{record.salary,jdbcType=DECIMAL},
reg_date = #{record.regDate,jdbcType=TIMESTAMP},
update t_user
set id = #{record.id,jdbcType=BIGINT},
username = #{record.username,jdbcType=VARCHAR},
pazzword = #{record.pazzword,jdbcType=VARCHAR},
salary = #{record.salary,jdbcType=DECIMAL},
reg_date = #{record.regDate,jdbcType=TIMESTAMP}
update t_user
username = #{username,jdbcType=VARCHAR},
pazzword = #{pazzword,jdbcType=VARCHAR},
salary = #{salary,jdbcType=DECIMAL},
reg_date = #{regDate,jdbcType=TIMESTAMP},
where id = #{id,jdbcType=BIGINT}
update t_user
set username = #{username,jdbcType=VARCHAR},
pazzword = #{pazzword,jdbcType=VARCHAR},
salary = #{salary,jdbcType=DECIMAL},
reg_date = #{regDate,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
上面截图和生成的Mapper映射文件,主要看一下它的书写很规范,可多多参考规范。
三、Mybatis Generator最完整配置详解转自:https://blog.csdn.net/xiao_jun_0820/article/details/50402299
推荐参考文章:MyBatis Generator 详解
配置文件信息可参考,项目中有需要的可作调整
会使用就ok
ends ~