mybatis的相关操作

基本参数介绍

1. parameterType
1. 简单数据类型
int double类型 String类型
简单的写法:java.lang.Integer --> int integer Int Integer 都可以,框架提供简写的方式。
2. POJO(JavaBean实体类)对象类型,默认是不能简写,可以配置。
User对象

2. resultType
1. 返回简单数据类型
int double long String
2. 返回POJO数据类型
返回User对象类型

3. resultMap结果类型
resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。
如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,
resultMap实质上还需要将查询结果映射到pojo对象中。 resultMap可以实现将查询结果映射为复杂类型的
pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。

1、增删改查的实现

<?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.qcby.Dao.UserDao">
    <select id="findAll" resultType="com.qcby.entity.User">
        select * from user
    </select>

    <select id="findById" resultType="com.qcby.entity.User" parameterType="java.lang.Integer">
        select * from user where id = #{id}
    </select>

    <insert id="insert" parameterType="com.qcby.entity.User">
        insert into user(username,birthday,sex,address)
        values(#{username},#{birthday},#{sex},#{address})
    </insert>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from user where id = #{id}
    </delete>

    <update id="update" parameterType="com.qcby.entity.User">
        update user set username = #{username},birthday = #{birthday},
                sex = #{sex},address = #{address} where id = #{id}
    </update>

    <!--返回主键 :我们的主键需要设置自动递增 -->
    <insert id="insertGetId" parameterType="com.qcby.entity.User">
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into user(username,birthday,sex,address)
        values(#{username},#{birthday},#{sex},#{address})
    </insert>

    <!--${}:拼接 , #{}预编译 -->
    <select id="likeByName" resultType="com.qcby.entity.User" parameterType="java.lang.String">
        select * from user where username like '%${value}%';
    </select>

</mapper>

2、多表关联查询
一对一
场景案例:一个学生对应一个老师

Student类

public class Student {

    private Integer id;
    private String Sname;
    private String sex;
    private Integer age;
    private Integer t_id;
    //在Student中加入Teacher对象
    private Teacher teacher;
}

Teacher类

public class Teacher {
    private Integer id;
    private String Tname;
}
<!--    按照结果嵌套处理-->
<select id="getStudent1" resultMap="StudentTeacher1">
   SELECT  student.id,student.Sname,teacher.Tname 
   FROM student  LEFT JOIN teacher  on student.t_id = teacher.id
</select>
   <resultMap id="StudentTeacher1" type="com.qcby.entity.Student">
       <result property="id" column="id"/>
       <result property="Sname" column="Sname"/>
       <result property="sex" column="sex"/>
       <result property="age" column="age"/>
       <result property="t_id" column="t_id"/>
        <!-- 复杂的属性我们需要单独去处理 对象:association   集合:collection   -->
   	 <!-- property="teacher" student类当中的关联字段 -->
   	 <!-- javaType="com.javen.model.Teacher" 为复杂属性设置类类型-->	
       <association property="teacher" javaType="com.qcby.entity.Teacher">
           <result property="id" column="id"/>
           <result property="Tname" column="Tname"/>
       </association>
   </resultMap>

一对多
场景案例:一个老师对应多个学生
Student类

public class Student {

    private Integer id;
    private String Sname;
    private String sex;
    private Integer age;
    private Integer t_id;
    
}

Teacher类

public class Teacher {
    private Integer id;
    private String Tname;
    //Teacher类中加入Student的集合
    private List<Student> students;
}

<!--按照结果进行查询-->
<select id="getTeacher" resultMap="TeacherStudent">
    SELECT  teacher.id,teacher.Tname,student.Sname FROM teacher
        LEFT JOIN student  on student.t_id = teacher.id
 </select>
<resultMap id="TeacherStudent" type="com.qcby.entity.Teacher">
    <result property="id" column="id"/>
    <result property="Tname" column="Tname"/>
    <!-- 复杂的属性我么需要单独去处理 对象:association   集合:collection
      在集合中的泛型信息,我们使用ofType获取
      -->
    <collection property="students" ofType="com.qcby.entity.Student">
        <result property="Sname" column="Sname"/>
    </collection>
</resultMap>

3、Mybatis中的标签元素
1、if标签
当需要查询的条件为多个时可以使用if标签判断查询条件是否为null来进行查询语句的拼接

<select id="selectUserByUsernameAndSex" parameterType="com.qcby.entity.User"
        resultType="com.qcby.entity.User">

    select * from user where
    <if test="username != null and username = ''">
        username=#{username}
    </if>
    <if test="sex != null and sex !=''">
       and sex=#{sex}
    </if>
</select>

2、where-if标签
从上边的案例当中我们可以看出如果username等于null,那么查询语句为select * from user where and sex=#{sex} 那这显然是不对的。
where-if标签可以解决这个问题,在where标签下的if标签会判断是否存在and(or),且是否应该有and(or),mybatis会根据情况去除不必要的and(or)

<select id="selectUserByUsernameAndSex" parameterType="com.qcby.entity.User"
        resultType="com.qcby.entity.User">

    select * from user
    <where>
        <if test="username != null">
            username=#{username}
        </if>
        <if test="sex != null">
             and sex=#{sex}
        </if>
    </where>
</select>

3、set-if
注意逗号

<update id="update" parameterType="com.qcby.entity.User">
    update user
    <set>
        <if test="username !=null and username!=''">
            username = #{username} ,
        </if>
        <if test="address != null and address != ''">
             address = #{address}
        </if>
     </set>
     where id = #{id}
</update>

4、choose-when-otherwise标签

<choose> 标签是这个标签组合当中的父标签<when><othrtwise>标签都在<choose>标签内部。
<when> 标签就相当于是我们的 if 和 elseif
<othrtwise>标签相当于是我们的 else
<select id="selectUserByChoose" resultType="com.qcby.entity.User"
        parameterType="com.qcby.entity.User">
    select * from user
    <where>
        <choose>
            <when test="id !='' and id != null">
                id=#{id}
            </when>
            <when test="username !='' and username != null">
                and username=#{username}
            </when>
            <otherwise>
                and sex=#{sex}
            </otherwise>
        </choose>
    </where>
</select>

5、foreach标签
可以通过该标签实现批量删除等操作

<!-- 批量删除的sql语句:delete from user where id in (1,2,3,4,5);   -->
<delete id="deleteMoreByArray">
    delete from user where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
</delete>
<!-- collection:当前要循环的数组或者集合   -->
<!--  item: 我们指定要循环的数组的每一个元素  -->
<!-- separator:每一个元素应该用什么来做分割   -->
<!-- open:当前循环是以什么开始   -->
<!-- close:当前循环是以什么结束   -->
/**
 * 通过数组批量删除
 * @param ids
 * @return
 */
int deleteMoreByArray(@Param("ids") Integer[] ids);

批量添加

<insert id="insertMoreByList" >
    insert into user(id,username,birthday,sex,address) values
    <foreach collection="users" item="user" separator=",">
        (null,#{user.username},#{user.birthday},#{user.sex},#{user.address})
    </foreach>
</insert>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值