代码生成
首先在idea中安装MyBatisX插件
右键数据表
如图所示,设置实体类包名等
如图所示,生成注解,实体类注解,方法等
快速生成添加
UserMapper
public interface UserMapper extends BaseMapper<User> {
int insertSelective(User user);
}
UserMapper.xml
<insert id="insertSelective">
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">,</if>
<if test="userName != null">user_name,</if>
<if test="age != null">age,</if>
<if test="email != null">email,</if>
<if test="sex != null">sex,</if>
<if test="isDeleted != null">is_deleted,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">#{uid,jdbcType=NUMERIC},</if>
<if test="userName != null">#{userName,jdbcType=VARCHAR},</if>
<if test="age != null">#{age,jdbcType=NUMERIC},</if>
<if test="email != null">#{email,jdbcType=VARCHAR},</if>
<if test="sex != null">#{sex,jdbcType=NUMERIC},</if>
<if test="isDeleted != null">#{isDeleted,jdbcType=NUMERIC},</if>
</trim>
</insert>
快速生成删除
UserMapper
public interface UserMapper extends BaseMapper<User> {
int insertSelective(User user);
int deleteByUidAndUserName(@Param("uid") Long uid, @Param("userName") String userName);
}
UserMapper.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="top.qaqaq.mybatisplus.mapper.UserMapper">
<!--Map<String, Object> selectMapById(Long id);-->
<select id="selectMapById" resultType="map">
select id, name, age, email from user where id = #{id}
</select>
<!--Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);-->
<select id="selectPageVo" resultType="User">
select uid, user_name, age, email from t_user where age > #{age}
</select>
<insert id="insertSelective">
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">,</if>
<if test="userName != null">user_name,</if>
<if test="age != null">age,</if>
<if test="email != null">email,</if>
<if test="sex != null">sex,</if>
<if test="isDeleted != null">is_deleted,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">#{uid,jdbcType=NUMERIC},</if>
<if test="userName != null">#{userName,jdbcType=VARCHAR},</if>
<if test="age != null">#{age,jdbcType=NUMERIC},</if>
<if test="email != null">#{email,jdbcType=VARCHAR},</if>
<if test="sex != null">#{sex,jdbcType=NUMERIC},</if>
<if test="isDeleted != null">#{isDeleted,jdbcType=NUMERIC},</if>
</trim>
</insert>
<delete id="deleteByUidAndUserName">
delete
from t_user
where
= #{uid,jdbcType=NUMERIC}
AND user_name = #{userName,jdbcType=VARCHAR}
</delete>
</mapper>
快速生成修改
UserMapper
public interface UserMapper extends BaseMapper<User> {
int insertSelective(User user);
int deleteByUidAndUserName(@Param("uid") Long uid, @Param("userName") String userName);
int updateAgeAndSexByUid(@Param("age") Integer age, @Param("sex") Integer sex, @Param("uid") Long uid);
}
UserMapper.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="top.qaqaq.mybatisplus.mapper.UserMapper">
<!--Map<String, Object> selectMapById(Long id);-->
<select id="selectMapById" resultType="map">
select id, name, age, email from user where id = #{id}
</select>
<!--Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);-->
<select id="selectPageVo" resultType="User">
select uid, user_name, age, email from t_user where age > #{age}
</select>
<insert id="insertSelective">
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">,</if>
<if test="userName != null">user_name,</if>
<if test="age != null">age,</if>
<if test="email != null">email,</if>
<if test="sex != null">sex,</if>
<if test="isDeleted != null">is_deleted,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">#{uid,jdbcType=NUMERIC},</if>
<if test="userName != null">#{userName,jdbcType=VARCHAR},</if>
<if test="age != null">#{age,jdbcType=NUMERIC},</if>
<if test="email != null">#{email,jdbcType=VARCHAR},</if>
<if test="sex != null">#{sex,jdbcType=NUMERIC},</if>
<if test="isDeleted != null">#{isDeleted,jdbcType=NUMERIC},</if>
</trim>
</insert>
<delete id="deleteByUidAndUserName">
delete
from t_user
where
= #{uid,jdbcType=NUMERIC}
AND user_name = #{userName,jdbcType=VARCHAR}
</delete>
<update id="updateAgeAndSexByUid">
update t_user
set age = #{age,jdbcType=NUMERIC},
sex = #{sex,jdbcType=NUMERIC}
where
= #{uid,jdbcType=NUMERIC}
</update>
</mapper>
快速生成查询
UserMapper
public interface UserMapper extends BaseMapper<User> {
int insertSelective(User user);
int deleteByUidAndUserName(@Param("uid") Long uid, @Param("userName") String userName);
int updateAgeAndSexByUid(@Param("age") Integer age, @Param("sex") Integer sex, @Param("uid") Long uid);
List<User> selectAgeAndSexByAgeBetween(@Param("beginAge") Integer beginAge, @Param("endAge") Integer endAge);
}
UserMapper.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="top.qaqaq.mybatisplus.mapper.UserMapper">
<!--Map<String, Object> selectMapById(Long id);-->
<select id="selectMapById" resultType="map">
select id, name, age, email from user where id = #{id}
</select>
<!--Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);-->
<select id="selectPageVo" resultType="User">
select uid, user_name, age, email from t_user where age > #{age}
</select>
<insert id="insertSelective">
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">,</if>
<if test="userName != null">user_name,</if>
<if test="age != null">age,</if>
<if test="email != null">email,</if>
<if test="sex != null">sex,</if>
<if test="isDeleted != null">is_deleted,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">#{uid,jdbcType=NUMERIC},</if>
<if test="userName != null">#{userName,jdbcType=VARCHAR},</if>
<if test="age != null">#{age,jdbcType=NUMERIC},</if>
<if test="email != null">#{email,jdbcType=VARCHAR},</if>
<if test="sex != null">#{sex,jdbcType=NUMERIC},</if>
<if test="isDeleted != null">#{isDeleted,jdbcType=NUMERIC},</if>
</trim>
</insert>
<delete id="deleteByUidAndUserName">
delete
from t_user
where
= #{uid,jdbcType=NUMERIC}
AND user_name = #{userName,jdbcType=VARCHAR}
</delete>
<update id="updateAgeAndSexByUid">
update t_user
set age = #{age,jdbcType=NUMERIC},
sex = #{sex,jdbcType=NUMERIC}
where
= #{uid,jdbcType=NUMERIC}
</update>
<select id="selectAgeAndSexByAgeBetween" resultMap="BaseResultMap">
select age, sex
from t_user
where age between #{beginAge,jdbcType=NUMERIC} and #{endAge,jdbcType=NUMERIC}
</select>
</mapper>
快速生成排序
UserMapper
public interface UserMapper extends BaseMapper<User> {
int insertSelective(User user);
int deleteByUidAndUserName(@Param("uid") Long uid, @Param("userName") String userName);
int updateAgeAndSexByUid(@Param("age") Integer age, @Param("sex") Integer sex, @Param("uid") Long uid);
List<User> selectAgeAndSexByAgeBetween(@Param("beginAge") Integer beginAge, @Param("endAge") Integer endAge);
List<User> selectAllOrderByAgeDesc();
}
UserMapper.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="top.qaqaq.mybatisplus.mapper.UserMapper">
<!--Map<String, Object> selectMapById(Long id);-->
<select id="selectMapById" resultType="map">
select id, name, age, email from user where id = #{id}
</select>
<!--Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);-->
<select id="selectPageVo" resultType="User">
select uid, user_name, age, email from t_user where age > #{age}
</select>
<insert id="insertSelective">
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">,</if>
<if test="userName != null">user_name,</if>
<if test="age != null">age,</if>
<if test="email != null">email,</if>
<if test="sex != null">sex,</if>
<if test="isDeleted != null">is_deleted,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">#{uid,jdbcType=NUMERIC},</if>
<if test="userName != null">#{userName,jdbcType=VARCHAR},</if>
<if test="age != null">#{age,jdbcType=NUMERIC},</if>
<if test="email != null">#{email,jdbcType=VARCHAR},</if>
<if test="sex != null">#{sex,jdbcType=NUMERIC},</if>
<if test="isDeleted != null">#{isDeleted,jdbcType=NUMERIC},</if>
</trim>
</insert>
<delete id="deleteByUidAndUserName">
delete
from t_user
where
= #{uid,jdbcType=NUMERIC}
AND user_name = #{userName,jdbcType=VARCHAR}
</delete>
<update id="updateAgeAndSexByUid">
update t_user
set age = #{age,jdbcType=NUMERIC},
sex = #{sex,jdbcType=NUMERIC}
where
= #{uid,jdbcType=NUMERIC}
</update>
<select id="selectAgeAndSexByAgeBetween" resultMap="BaseResultMap">
select age, sex
from t_user
where age between #{beginAge,jdbcType=NUMERIC} and #{endAge,jdbcType=NUMERIC}
</select>
<select id="selectAllOrderByAgeDesc" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user
order by age desc
</select>
</mapper>