单表操作CRUD
插入
普通插入
- parameterType可选参数可不写,会自动匹配
<insert id="insertOne" parameterType="com.mbyte.easy.admin.entity.Student">
insert into student (name, img, sex, create_time)
values (
#{student.name,jdbcType=VARCHAR},
#{student.img,jdbcType=VARCHAR},
#{student.sex,jdbcType=INTEGER},
#{student.createTime,jdbcType=TIMESTAMP}
)
</insert>
回填主键
useGeneratedKeys="true" keyProperty="id"
<!--回填主键:useGeneratedKeys="true" keyProperty="id" -->
<insert id="insertTwo" parameterType="com.mbyte.easy.admin.entity.Student"
useGeneratedKeys="true" keyProperty="id">
insert into student (name, img, sex, create_time)
values (
#{student.name,jdbcType=VARCHAR},
#{student.img,jdbcType=VARCHAR},
#{student.sex,jdbcType=INTEGER},
#{student.createTime,jdbcType=TIMESTAMP}
)
</insert>
// 回填主键,执行完毕之后,student2中id自动赋值
studentMapper.insertTwo(student2);
// 直接可以获取到数据库生成的主键id
student2.getId();
System.out.println("student2:"+student2);
批量插入 => foreach
<insert id="insertBatch">
insert into student (name, img, sex, create_time)
values
<foreach collection="studentList" item="student" separator=",">
(
#{student.name,jdbcType=VARCHAR},
#{student.img,jdbcType=VARCHAR},
#{student.sex,jdbcType=INTEGER},
#{student.createTime,jdbcType=TIMESTAMP}
)
</foreach>
</insert>
删除
单个删除
<delete id="deleteSutdentById" parameterType="java.lang.Long" >
delete from student where id = #{id,jdbcType=BIGINT}
</delete>
批量删除
<delete id="deleteByIds" parameterType="java.lang.Long" >
delete from student where id in
<foreach collection="idList" index="index" item="item"
open="(" separator="," close=")">
#{item}
</foreach>
</delete>
更新
<!--注意字段判断-->
<update id="updateStuddent" >
update student
<set>
<if test="student.name != null">
name = #{student.name,jdbcType=VARCHAR},
</if>
<if test="student.img != null">
img = #{student.img,jdbcType=VARCHAR},
</if>
<if test="student.sex != null">
sex = #{student.sex,jdbcType=INTEGER},
</if>
<if test="student.createTime != null">
create_time = #{student.createTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{student.id,jdbcType=BIGINT}
</update>
foreach标签
delete from xxx_table where id in
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach> => (1,2,3,4)
-
collection:表示迭代集合的名称,自定义名称,可以使用@Param注解指定
- 只有一个List/Array/Map类型参数时,如果不加@Param注解,collection分别必须填:list、array、map中的key属性 - 有多个参数时,不加@Param注解,collection和默认参数名称一致 - 如果加@Param注解,都和注解定义一致
-
item:表示本次迭代获取的元素,自定义名称;表示本次迭代获取的元素,若collection为List、Set或者数组,则表示其中的元素;若collection为map,则代表key-value的value,该参数为必选
-
separator:分隔符,mybatis会在每次迭代后给sql语句append上separator属性指定的字符,该参数为可选项
-
open:表示该语句以什么开始,最常用的是左括弧’(’,注意:mybatis会将该字符拼接到整体的sql语句之前,并且只拼接一次,该参数为可选项
-
close:表示该语句以什么开始,最常用的是左括弧’(’,注意:mybatis会将该字符拼接到整体的sql语句之前,并且只拼接一次,该参数为可选项
-
index:在list、Set和数组中,index表示当前迭代的位置,在map中,index代指是元素的key,该参数是可选项。
连表查询
一对一、多对一(association)
<select id="listPage1" parameterType="java.lang.Long"
resultMap="BaseResultMapOneToOne">
select
<include refid="Base_Column_List" />
from student
</select>
<resultMap id="BaseResultMapOneToOne"
type="com.mbyte.easy.admin.entity.Student">
<id column="id" jdbcType="BIGINT" property="id" />
<id column="class_id" jdbcType="BIGINT" property="classId" />
<id column="name" jdbcType="VARCHAR" property="name" />
<id column="img" jdbcType="VARCHAR" property="img" />
<id column="sex" jdbcType="INTEGER" property="sex" />
<id column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<association property="classInfo" column="class_id"
select="com.mbyte.easy.admin.mapper.ClassInfoMapper.getClassInfoById">
</association>
</resultMap>
<select id="getClassInfoById" parameterType="java.lang.Long"
resultMap="BaseResultMapTest">
select
<include refid="Base_Column_List" />
from class_info
where id = #{id,jdbcType=BIGINT}
</select>
一对多、多对多(collections)
<resultMap id="BaseResultMapStudent"
type="com.mbyte.easy.admin.entity.ClassInfo">
<id column="id" jdbcType="BIGINT" property="id" />
<id column="name" jdbcType="VARCHAR" property="name" />
<id column="remark" jdbcType="VARCHAR" property="remark" />
<collection property="studentList" column="id"
select="getStudnetByClassId" >
</collection>
</resultMap>
<select id="getClassInfo" parameterType="java.lang.Long"
resultMap="BaseResultMapStudent">
select
*
from class_info
</select>
<select id="getStudnetByClassId" parameterType="java.lang.Long" resultType="com.mbyte.easy.admin.entity.Student">
select
*
from student
where class_id = #{classId,jdbcType=BIGINT}
</select>
传递多个参数时示例
collection :
<!-- 传递两个参数
column={personId=person_id,checkIndexSumId=check_index_sum_id}
传递查询参数名称=数据库字段的名字
-->
<collection property="checkIndexSumPersonChildList"
column="{personId=person_id,checkIndexSumId=check_index_sum_id}"
select="getCheckIndexSumPersonChild"/>
getCheckIndexSumPersonChild:
<select id="getCheckIndexSumPersonChild"
resultType="com.mbyte.easy.admin.entity.CheckIndexSumPersonChild">
select * from check_index_sum_person_child
where check_index_sum_id = #{checkIndexSumId}
and person_id = #{personId}
order by id asc
</select>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115417.html