一、引言
前面已经介绍了SpringBoot 整合mybatis,然而对于一些基本的CRUD等操作,开发时没必要手写,可以扒出不错的代码生成器直接生成实体类、mapper映射文件、mapper类。
这里介绍用xml配置文件生成最基本的CRUD代码
二、操作
1、如图:
2、generatorConfig.xml 配置如下:
userId, userName, phone, sex
delete from userinfo
where userId = #{userid,jdbcType=INTEGER}
insert into userinfo (userId, userName, phone,
sex)
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR})
insert into userinfo
userId,
userName,
phone,
sex,
#{userid,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
update userinfo
userName = #{username,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
where userId = #{userid,jdbcType=INTEGER}
update userinfo
set userName = #{username,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR}
where userId = #{userid,jdbcType=INTEGER}
3、运行generatorConfig.xml文件
(2)
(3)
4、在运行完之后即可自动生成
UserInfoMapper.xml
userId, userName, phone, sex
delete from userinfo
where userId = #{userid,jdbcType=INTEGER}
insert into userinfo (userId, userName, phone,
sex)
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR})
insert into userinfo
userId,
userName,
phone,
sex,
#{userid,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
update userinfo
userName = #{username,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
where userId = #{userid,jdbcType=INTEGER}
update userinfo
set userName = #{username,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR}
where userId = #{userid,jdbcType=INTEGER}
UserInfoMapper
@Repository
public interface UserInfoMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbg.generated
*/
int deleteByPrimaryKey(Integer userid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbg.generated
*/
int insert(UserInfo record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbg.generated
*/
int insertSelective(UserInfo record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbg.generated
*/
UserInfo selectByPrimaryKey(Integer userid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbg.generated
*/
int updateByPrimaryKeySelective(UserInfo record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbg.generated
*/
int updateByPrimaryKey(UserInfo record);
//查询
//List findAllUser();
}
UserInfo
package com.example.springbootmybatis.pojo;
public class UserInfo {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column userinfo.userId
*
* @mbg.generated
*/
private Integer userid;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column userinfo.userName
*
* @mbg.generated
*/
private String username;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column userinfo.phone
*
* @mbg.generated
*/
private String phone;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column userinfo.sex
*
* @mbg.generated
*/
private String sex;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column userinfo.userId
*
* @return the value of userinfo.userId
*
* @mbg.generated
*/
public Integer getUserid() {
return userid;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column userinfo.userId
*
* @param userid the value for userinfo.userId
*
* @mbg.generated
*/
public void setUserid(Integer userid) {
this.userid = userid;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column userinfo.userName
*
* @return the value of userinfo.userName
*
* @mbg.generated
*/
public String getUsername() {
return username;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column userinfo.userName
*
* @param username the value for userinfo.userName
*
* @mbg.generated
*/
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column userinfo.phone
*
* @return the value of userinfo.phone
*
* @mbg.generated
*/
public String getPhone() {
return phone;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column userinfo.phone
*
* @param phone the value for userinfo.phone
*
* @mbg.generated
*/
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column userinfo.sex
*
* @return the value of userinfo.sex
*
* @mbg.generated
*/
public String getSex() {
return sex;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column userinfo.sex
*
* @param sex the value for userinfo.sex
*
* @mbg.generated
*/
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
}