在这里简单介绍一下如何整合Mybatis
自动生成代码的插件MybatisGenerator
需要在pom.xml
文件中的
中加入以下设置
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 Project
的mybatis-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
就可以解决绝大部分的问题啦