个人认为就是把mybatis配置文件进行封装了,仅此而已,比如像mapper层,mybatisplus提供了一个BaseMapper给我们使用,里面有mapper层的CRUD,当然比较繁多比如根据id查字段等等,我们的mapper直接继承这个基类就可以了,实际上我们mybatis配置文件中也有这种,也可以自己写,比如:参数为一个map类型,不管你根据什么进行查询将其转为一个map集合,这样效率也很高,就不用id查询,name查询具体区分了;
这里我们是用mybatisx一键生成的mvc三层(右键数据库表)
package com.wyh.mybatisx.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wyh.mybatisx.pojo.User;
/**
* @author 邬雨航
* @description 针对表【user】的数据库操作Mapper
* @createDate 2022-07-03 12:05:47
* @Entity com.wyh.mybatisX.pojo.User
*/
public interface UserMapper extends BaseMapper<User> {
int insertSelective(User user);
List<User> getAllByName(@Param("name") String name);
int deleteByIdAndName(@Param("id") Long id, @Param("name") String name);
int updateAgeAndById(@Param("age") Integer age, @Param("id") Long id);
// List<User> selectAgeAndName(@Param("age")Integer age,@Param("name")String name);
List<User> selectAgeAndNameByAgeBetween(@Param("beginAge") Integer beginAge, @Param("endAge") Integer endAge);
}
这里的mybatisx插件帮助我们快速开发,他提供了mapper下的一些基础方法,当编写完后快捷键,配置文件都不需要写了
<?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.wyh.mybatisx.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.wyh.mybatisx.pojo.User">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="email" column="email" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,name,age,
email
</sql>
<insert id="insertSelective">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="name != null">name,</if>
<if test="age != null">age,</if>
<if test="email != null">email,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">#{id,jdbcType=BIGINT},</if>
<if test="name != null">#{name,jdbcType=VARCHAR},</if>
<if test="age != null">#{age,jdbcType=INTEGER},</if>
<if test="email != null">#{email,jdbcType=VARCHAR},</if>
</trim>
</insert>
<select id="getAllByName" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user
<where>
<if test="name != null and name != ''">
name = #{name,jdbcType=VARCHAR}
</if>
</where>
</select>
<delete id="deleteByIdAndName">
delete from user
<where>
<if test="id != null">
id = #{id,jdbcType=NUMERIC}
</if>
<if test="name != null and name != ''">
AND name = #{name,jdbcType=VARCHAR}
</if>
</where>
</delete>
<update id="updateAgeAndById">
update user
set age = #{age,jdbcType=NUMERIC},
<where>
<if test="id != null">
id = #{id,jdbcType=NUMERIC}
</if>
</where>
</update>
<select id="selectAgeAndNameByAgeBetween" resultMap="BaseResultMap">
select age, name
from user
<where>
<if test="beginAge != null and endAge != null">
age between #{beginAge,jdbcType=INTEGER} and #{endAge,jdbcType=INTEGER}
</if>
</where>
</select>
</mapper>
我们再来看看之前我们mybatis的map写法
直接sql+rebid
<?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.yanzhen.dao.ClazzDao">
<resultMap type="com.yanzhen.entity.Clazz" id="Clazz">
<id column="id" property="id"/>
<result column="clazz_name" property="clazzName"/>
<result column="id" property="id"/>
<result column="remark" property="remark"/>
<result column="subject_id" property="subjectId"/>
</resultMap>
<insert id="create" keyProperty="id" useGeneratedKeys="true" parameterType="com.yanzhen.entity.Clazz">
insert into tb_clazz(
clazz_name,
remark,
subject_id
)values(
#{clazzName},
#{remark},
#{subjectId}
)
</insert>
<select id="query" resultMap="Clazz">
select * from tb_clazz
<include refid="ClazzFindCriteria"/>
<if test="offset!=null and rows!=null">limit ${offset} , ${rows}</if>
</select>
<select id="count" resultType="int">
select count(1) from tb_clazz
<include refid="ClazzFindCriteria"/>
</select>
<select id="detail" resultMap="Clazz">
select * from tb_clazz
<include refid="ClazzFindCriteria"/>
limit 1
</select>
<delete id="delete">
delete from tb_clazz
<include refid="ClazzFindCriteria"/>
</delete>
<update id="update">
update tb_clazz
<include refid="ClazzUpdateCriteria"/>
<include refid="ClazzFindCriteria"/>
</update>
<sql id="ClazzFindCriteria">
<where>
<if test="clazzName != null and clazzName != ''">and clazz_name like concat('%',#{clazzName},'%')</if>
<if test="id != null">and id = #{id}</if>
<if test="remark != null and remark != ''">and remark = #{remark}</if>
<if test="subjectId != null">and subject_id = #{subjectId}</if>
</where>
</sql>
<sql id="ClazzUpdateCriteria">
<set>
<if test="updateClazzName != null and updateClazzName != ''">clazz_name = #{updateClazzName},</if>
<if test="updateId != null">id = #{updateId},</if>
<if test="updateRemark != null and updateRemark != ''">remark = #{updateRemark},</if>
<if test="updateSubjectId != null">subject_id = #{updateSubjectId},</if>
</set>
</sql>
</mapper>
package com.yanzhen.utils;
import java.util.HashMap;
import java.util.Map;
public class MapParameter {
//目标对象
private Map<String,Object> paramMap = new HashMap<>();
//私有构造
private MapParameter(){
}
public static MapParameter getInstance(){
return new MapParameter();
}
public MapParameter add(String key,Object value){
paramMap.put(key,value);
return this;
}
public MapParameter addId(Object value){
paramMap.put("id",value);
return this;
}
public MapParameter add(Map<String,Object> map){
for (Map.Entry<String, Object> entry : map.entrySet()) {
paramMap.put(entry.getKey(),entry.getValue());
}
return this;
}
public Map<String,Object> getMap(){
return paramMap;
}
}