一、引言
前面已经介绍了SpringBoot 整合mybatis,然而对于一些基本的CRUD等操作,开发时没必要手写,可以扒出不错的代码生成器直接生成实体类、mapper映射文件、mapper类。
这里介绍用xml配置文件生成最基本的CRUD代码
二、操作
1、如图:
2、generatorConfig.xml 配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springbootmybatis.dao.UserInfoMapper">
<resultMap id="BaseResultMap" type="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="userId" jdbcType="INTEGER" property="userid" />
<result column="userName" jdbcType="VARCHAR" property="username" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
userId, userName, phone, sex
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from userinfo
where userId = #{userid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from userinfo
where userId = #{userid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into userinfo (userId, userName, phone,
sex)
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into userinfo
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userid != null">
userId,
</if>
<if test="username != null">
userName,
</if>
<if test="phone != null">
phone,
</if>
<if test="sex != null">
sex,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userid != null">
#{userid,jdbcType=INTEGER},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
</if>
<if test="sex != null">
#{sex,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update userinfo
<set>
<if test="username != null">
userName = #{username,jdbcType=VARCHAR},
</if>
<if test="phone != null">
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="sex != null">
sex = #{sex,jdbcType=VARCHAR},
</if>
</set>
where userId = #{userid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update userinfo
set userName = #{username,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR}
where userId = #{userid,jdbcType=INTEGER}
</update>
<select id="findAllUser" resultType="com.example.springbootmybatis.pojo.UserInfo">
SELECT * FROM USERINFO
</select>
</mapper>
3、运行generatorConfig.xml文件
(2)
(3)
4、在运行完之后即可自动生成
UserInfoMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springbootmybatis.dao.UserInfoMapper">
<resultMap id="BaseResultMap" type="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="userId" jdbcType="INTEGER" property="userid" />
<result column="userName" jdbcType="VARCHAR" property="username" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
userId, userName, phone, sex
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from userinfo
where userId = #{userid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from userinfo
where userId = #{userid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into userinfo (userId, userName, phone,
sex)
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into userinfo
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userid != null">
userId,
</if>
<if test="username != null">
userName,
</if>
<if test="phone != null">
phone,
</if>
<if test="sex != null">
sex,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userid != null">
#{userid,jdbcType=INTEGER},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
</if>
<if test="sex != null">
#{sex,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update userinfo
<set>
<if test="username != null">
userName = #{username,jdbcType=VARCHAR},
</if>
<if test="phone != null">
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="sex != null">
sex = #{sex,jdbcType=VARCHAR},
</if>
</set>
where userId = #{userid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.springbootmybatis.pojo.UserInfo">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update userinfo
set userName = #{username,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR}
where userId = #{userid,jdbcType=INTEGER}
</update>
<select id="findAllUser" resultType="com.example.springbootmybatis.pojo.UserInfo">
SELECT * FROM USERINFO
</select>
</mapper>
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<UserInfo> 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();
}
}