博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第十四篇 SpringBoot2 x整合MyBatisGenerator
阅读量:6695 次
发布时间:2019-06-25

本文共 18573 字,大约阅读时间需要 61 分钟。

在这里简单介绍一下如何整合Mybatis自动生成代码的插件MybatisGenerator

引入插件

需要在pom.xml文件中的<build><plugins></plugins></build>中加入以下设置

org.mybatis.generator
mybatis-generator-maven-plugin
1.3.5
mysql
mysql-connector-java
5.1.28
runtime
src/main/resources/generatorConfig.xml
true
复制代码

引入依赖后右侧的maven project侧边栏中的Plugins选项会多出一个mybatis-generator

设置配置文件

接下来在resources文件夹中创建一个generatorConfig.xml文件用于自动构建代码的配置

复制代码

#运行插件 配置就结束了,最后运行一下,这里找到在Maven Projectmybatis-generator:generator选项,双击运行

#最后的成果 User.java

package priv.gabriel.model;import java.util.Date;public class User {    private Integer id;    private String username;    private String pword;    private Integer age;    private Date utime;    private Date ctime;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username == null ? null : username.trim();    }    public String getPword() {        return pword;    }    public void setPword(String pword) {        this.pword = pword == null ? null : pword.trim();    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public Date getUtime() {        return utime;    }    public void setUtime(Date utime) {        this.utime = utime;    }    public Date getCtime() {        return ctime;    }    public void setCtime(Date ctime) {        this.ctime = ctime;    }}复制代码

UserMapper.xml

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, pword, age, utime, ctime
delete from user where id = #{id,jdbcType=INTEGER}
delete from user
insert into user (id, username, pword, age, utime, ctime) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
insert into user
id,
username,
pword,
age,
utime,
ctime,
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{pword,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{utime,jdbcType=DATE},
#{ctime,jdbcType=DATE},
update user
id = #{record.id,jdbcType=INTEGER},
username = #{record.username,jdbcType=VARCHAR},
pword = #{record.pword,jdbcType=VARCHAR},
age = #{record.age,jdbcType=INTEGER},
utime = #{record.utime,jdbcType=DATE},
ctime = #{record.ctime,jdbcType=DATE},
update user set id = #{record.id,jdbcType=INTEGER}, username = #{record.username,jdbcType=VARCHAR}, pword = #{record.pword,jdbcType=VARCHAR}, age = #{record.age,jdbcType=INTEGER}, utime = #{record.utime,jdbcType=DATE}, ctime = #{record.ctime,jdbcType=DATE}
update user
username = #{username,jdbcType=VARCHAR},
pword = #{pword,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
utime = #{utime,jdbcType=DATE},
ctime = #{ctime,jdbcType=DATE},
where id = #{id,jdbcType=INTEGER}
update user set username = #{username,jdbcType=VARCHAR}, pword = #{pword,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, utime = #{utime,jdbcType=DATE}, ctime = #{ctime,jdbcType=DATE} where id = #{id,jdbcType=INTEGER}
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, pword, age, utime, ctime
delete from user where id = #{id,jdbcType=INTEGER}
delete from user
insert into user (id, username, pword, age, utime, ctime) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
insert into user
id,
username,
pword,
age,
utime,
ctime,
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{pword,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{utime,jdbcType=DATE},
#{ctime,jdbcType=DATE},
update user
id = #{record.id,jdbcType=INTEGER},
username = #{record.username,jdbcType=VARCHAR},
pword = #{record.pword,jdbcType=VARCHAR},
age = #{record.age,jdbcType=INTEGER},
utime = #{record.utime,jdbcType=DATE},
ctime = #{record.ctime,jdbcType=DATE},
update user set id = #{record.id,jdbcType=INTEGER}, username = #{record.username,jdbcType=VARCHAR}, pword = #{record.pword,jdbcType=VARCHAR}, age = #{record.age,jdbcType=INTEGER}, utime = #{record.utime,jdbcType=DATE}, ctime = #{record.ctime,jdbcType=DATE}
update user
username = #{username,jdbcType=VARCHAR},
pword = #{pword,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
utime = #{utime,jdbcType=DATE},
ctime = #{ctime,jdbcType=DATE},
where id = #{id,jdbcType=INTEGER}
update user set username = #{username,jdbcType=VARCHAR}, pword = #{pword,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, utime = #{utime,jdbcType=DATE}, ctime = #{ctime,jdbcType=DATE} where id = #{id,jdbcType=INTEGER}
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, pword, age, utime, ctime
delete from user where id = #{id,jdbcType=INTEGER}
delete from user
insert into user (id, username, pword, age, utime, ctime) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
insert into user
id,
username,
pword,
age,
utime,
ctime,
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{pword,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{utime,jdbcType=DATE},
#{ctime,jdbcType=DATE},
update user
id = #{record.id,jdbcType=INTEGER},
username = #{record.username,jdbcType=VARCHAR},
pword = #{record.pword,jdbcType=VARCHAR},
age = #{record.age,jdbcType=INTEGER},
utime = #{record.utime,jdbcType=DATE},
ctime = #{record.ctime,jdbcType=DATE},
update user set id = #{record.id,jdbcType=INTEGER}, username = #{record.username,jdbcType=VARCHAR}, pword = #{record.pword,jdbcType=VARCHAR}, age = #{record.age,jdbcType=INTEGER}, utime = #{record.utime,jdbcType=DATE}, ctime = #{record.ctime,jdbcType=DATE}
update user
username = #{username,jdbcType=VARCHAR},
pword = #{pword,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
utime = #{utime,jdbcType=DATE},
ctime = #{ctime,jdbcType=DATE},
where id = #{id,jdbcType=INTEGER}
update user set username = #{username,jdbcType=VARCHAR}, pword = #{pword,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, utime = #{utime,jdbcType=DATE}, ctime = #{ctime,jdbcType=DATE} where id = #{id,jdbcType=INTEGER}
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, pword, age, utime, ctime
delete from user where id = #{id,jdbcType=INTEGER}
delete from user
insert into user (id, username, pword, age, utime, ctime) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
insert into user
id,
username,
pword,
age,
utime,
ctime,
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{pword,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{utime,jdbcType=DATE},
#{ctime,jdbcType=DATE},
update user
id = #{record.id,jdbcType=INTEGER},
username = #{record.username,jdbcType=VARCHAR},
pword = #{record.pword,jdbcType=VARCHAR},
age = #{record.age,jdbcType=INTEGER},
utime = #{record.utime,jdbcType=DATE},
ctime = #{record.ctime,jdbcType=DATE},
update user set id = #{record.id,jdbcType=INTEGER}, username = #{record.username,jdbcType=VARCHAR}, pword = #{record.pword,jdbcType=VARCHAR}, age = #{record.age,jdbcType=INTEGER}, utime = #{record.utime,jdbcType=DATE}, ctime = #{record.ctime,jdbcType=DATE}
update user
username = #{username,jdbcType=VARCHAR},
pword = #{pword,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
utime = #{utime,jdbcType=DATE},
ctime = #{ctime,jdbcType=DATE},
where id = #{id,jdbcType=INTEGER}
update user set username = #{username,jdbcType=VARCHAR}, pword = #{pword,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, utime = #{utime,jdbcType=DATE}, ctime = #{ctime,jdbcType=DATE} where id = #{id,jdbcType=INTEGER}
复制代码

UserMapper.java

package priv.gabriel.dao;import java.util.List;import org.apache.ibatis.annotations.Param;import priv.gabriel.model.User;import priv.gabriel.model.UserExample;public interface UserMapper {    long countByExample(UserExample example);    int deleteByExample(UserExample example);    int deleteByPrimaryKey(Integer id);    int insert(User record);    int insertSelective(User record);    List
selectByExample(UserExample example); User selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); int updateByExample(@Param("record") User record, @Param("example") UserExample example); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record);}复制代码

可以看到已经帮我们生产了关于CRUD的绝大代码,再结合之前的通用mapper就可以解决绝大部分的问题啦

转载于:https://juejin.im/post/5c1ca9dc6fb9a049a42f0635

你可能感兴趣的文章
推荐引擎
查看>>
Mac版:上传图片到远程图床哪家强?
查看>>
Android学习系列-----2 Activity的生命周期与启动模式
查看>>
前端真的能做到彻底权限控制吗?
查看>>
EdgeX Foundry边缘计算框架-核心服务层
查看>>
LVM动态扩展
查看>>
MongoDB副本集搭建
查看>>
JS数组交集 并集 差集
查看>>
webpack中打包后端模板的思路
查看>>
腾讯前端求职直播课——简历篇
查看>>
【译】JS基础算法脚本:查找字符串中最长的子字符
查看>>
项目 - 收藏集 - 掘金
查看>>
从零开始用 Flask 搭建一个网站(二)
查看>>
js中的for in和for each in的用法和区别
查看>>
夏日葵电商:微信商城初步搭建,如何提高产品转化率
查看>>
利用vue-cli配合vue-router搭建一个完整的spa流程(一)
查看>>
Microsoft推出适用于Win 8.1和Win10的KB 4010250 Flash Player更新
查看>>
JS的内置对象系列:Array(一)
查看>>
微软宣布开源WPF、WinForms和WinUI
查看>>
携程对AIOps场景和算法的探索与实践
查看>>