一.实体entity及请求对象,响应对象
package com.xxx.xxx.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_abc_entity")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 姓名
**/
@TableField("name")
private String name;
/**
* 年龄
*/
@TableField("age")
private Integer age;
/**
* 创建时间
*/
@TableField("create_time")
private LocalDateTime createdTime;
}
package com.xxx.xxx.dto;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class RequestDto implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
private Long id;
/**
* 姓名
**/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 年龄列表
**/
private List<Integer> ageList;
/**
* 创建时间
*/
private LocalDateTime createdTime;
}
3.响应对象
package com.xxx.xxx.vo;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public class ResponseVo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
private Long id;
/**
* 姓名
**/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 创建时间
*/
private LocalDateTime createdTime;
}
二. Mapper层数据库中的执行语句写法
解释说明:
1)id后面跟的是方法名,parameterType后面跟的是请求对象类型(带上包名),resultType后面跟的是响应对象类型(带上包名);
2)useGeneratedKeys=“true” keyProperty="id"表示:如果插入的表id以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键id返回。useGeneratedKeys参数只针对 insert 语句生效,默认为 false;
3)当对数据库进行批量操作的时候使用时需要注意,如果在Mapper.java文件中对应的方法中使用了@Param(“list”)这个注解进行绑定时,参数列表一定要相同都是list,否则在数据进入数据库操作时会报错
<insert id="insert" parameterType="com.xxx.xxx.entity.User" useGeneratedKeys="true" keyProperty="id">
insert into t_abc_entity
(
name,
age,
create_time,
)
values
(
#{name},
#{age},
#{createTime}
)
</insert>
<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
insert into t_abc_entity
(
name,
age,
create_time,
)
values
<foreach collection="list" item="item" separator=",">
(
#{item.name},
#{item.age},
#{item.createTime}
)
</foreach>
</insert>
<select id="queryBySelect" parameterType="com.xxx.xxx.dtoRequestDto " resultType="com.xxx.xxx.vo.ResponseVo">
select id,name,age,create_time
from t_abc_entity
<where>
<if test="id != null">
and Id = #{id}
</if>
<if test="name != null and name!=''">
and name = #{name,jdbcType=VARCHAR}
</if>
<if test="age != null">
and age = #{age}
</if>
<if test="ageList!= null and ageList.size() > 0">
AND age IN
<foreach collection="ageList" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="createTime != null and createTime !=''">
and create_time= #{createTime,jdbcType=VARCHAR }
</if>
</where>
</select>
<delete id="deleteByIds" parameterType="java.util.List">
delete
from t_abc_entity
where ID in
(
<foreach collection ="list" item="item" separator="," >
#{item}
</foreach>
)
</delete>
<update id="batchUpdate" parameterType="java.util.List">
<foreach item="emp" separator=";" collection="list">
update t_abc_entity
<set>
<if test="emp.name != null">
name = #{emp.name },
</if>
<if test="emp.age!= null">
age= #{emp.age},
</if>
<if test="emp.createdTime!= null">
create_time= #{emp.createdTime}
</if>
</set>
where
ID = #{emp.id}
</foreach>
</update>