mybatis项目使用知识点

一、 MyBatis获取参数值的两种方式

MyBatis获取参数值的两种方式:${}和#{}

${}的本质就是字符串拼接,    #{}的本质就是占位符赋值
mapper接口中,方法传入参数时
在mybatyis的底层,当执行mapper.xml中的sql语句时,会自动检测mapper接口中的方法
当检测到方法中有多个参数时,它会自动将这些参数放到map集合里,
并以param为键,以参数为值。
所以,当我们想要获取某一个键所对应的值时,我们必须在#{}或${}里直接访问map集合的键


所以,当我们在mapper接口中传入参数时,一般都是直接设置map的key值即param的值



例: List<User> getUserById(@Param("id") int id);


然后sql语句就可以直接访问,不会报错了。

<select id="getUserById" resultType="User"> 
	select * from t_user where id = #{id} 
</select>

以后开发中 告诉自己:

mapper接口传入参数时,只有两种情况:
1、实体类 类型
2@param("")命名参数类型

查询功能时,全部用list集合接收
查询数据如果使用map集合接收,键都是String值都是Object

List<Map<String,Object>> selectUser();

二、自定义映射


属性:
property:设置映射关系中java 实体类中的属性名
column:设置映射关系中sql 表中的字段名


 <resultMap id="empResultMap" type="Emp">
     <id property="eid" column="eid"></id>
     <result property="empName" column="emp_name"></result>
     <result property="age" column="age"></result>
     <result property="sex" column="sex"></result>
     <result property="email" column="email"></result>
 </resultMap>


<select id="getAllEmp" resultMap="empResultMap">
    select * from t_emp;
</select>

三、多对一映射处理 使用association

    
    <resultMap id="empAndResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>

        <!--
             association:专门用来处理多对一的映射关系
             property:需要处理多对一,多的一方中设置的属性名
             javaType:该属性的类型
        -->
        <association property="dept" javaType="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        </association>

    </resultMap>
    
    <select id="getEmpAndDept" resultMap="empAndResultMap">
        select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.eid=#{eid}
    </select>



association 分步查询 多对一

分布查询:(处理一对多、多对一的常用方式)
思路:
1、每个表根据每个表的id查询出自己的数据,只是简单的单独的先查询出自己的数据
2、在其中一个.xml文件中使用<select id="" resultMap="">方式进行查询
3、resultMap 正常写,
association property="要处理的属性" 
        select="使用哪一个方法的sql语句,方法的全限定名称(copy reference)" 
        column="根据哪个 条件 去查询关联的对象信息">




======================================================================================================


EmpMapper:
    /**
     * 通过分步查询 查询员工以及员工所对应的部门信息
     * 分布查询第一步:查询员工信息
     */
    Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
}


EmpMapper.xml:
 	
    <resultMap id="empAndDeptByStepResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>

        <association property="dept" 
        select="com.cn.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" 
        column="did">
        </association>

    </resultMap>

    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from  t_emp where eid=#{eid}
    </select>
}



======================================================================================================



DeptMapper:
    /**
     * 通过分步查询 查询员工以及员工所对应的部门信息
     * 分布查询第而二:通过did查询员工所对应的部门
     */
    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);


DeptMapper.xml:


<mapper namespace="com.cn.mybatis.mapper.DeptMapper">

    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select  * from t_dept where did=#{did}
    </select>



四、一对多映射处理 使用 collection集合

//总结:
少的一方封装多的一方对象时,
select语句中要使用 resultMap,然后创建resultMap标签

resultMap中先列出自己实体类中的数据:
		<id property="" column=""></id>
        <result property="" column=""></result>	
        					
然后在列出多的一方的数据时,使用 collection集合:
		 <collection property="要处理的属性名" ofType="该属性的类型">
            <id property="empId" column="emp_id"></id>
            <id property="empName" column="emp_name"></id>
            <id property="xxx" column="xxx"></id>
            ...........
        </collection>



collection 分布查询 一对多

2、在主表中查询语句使用 resultMap,之后通过collection 中的 select引入副表的 sql语句

DeptMapper.xml和EmpMapper.xml:

    <resultMap id="GetDeptAndEmp" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>

        <collection property="emps"//要处理的属性
                    select="com.cn.mybatis.mapper.EmpMapper.getEmp"//使用哪一个方法的sql语句,方法的全限定名称
                    column="did"//根据哪个 条件 去查询关联的对象信息

        </collection>

    </resultMap>
    <select id="getDeptAndEmpStep" resultMap="GetDeptAndEmp">
        select * from  t_dept where did=#{did}
    </select>

======================================================================================================


   <select id="getEmp" resultType="Emp">
        select * from  t_emp where did=#{did}
    </select>

五、动态SQL

where – if

其实就是在sql语句上加一个判断条件而已,没啥特别的!!



where和if要结合使用:
1、若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
2、若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and或or去掉
注意:where标签不能去掉条件最后多余的and

    <select id="DynamicSQL" resultType="xxx">

        select * from t_emp
        <where>

        <if test="userName !=null and userName !=''">
            and user_name = #{userName}
        </if>
        <if test="age !=null and age !=''">
            and age = #{age}
        </if>
        <if test="email !=null and email !=''">
            and email = #{email}
        </if>

        </where>
    </select>




trim


trim用于去掉或添加标签中的内容


常用属性:
prefix:在trim标签中的内容的前面添加某些内容
prefixOverrides:在trim标签中的内容的前面去掉某些内容

suffix:在trim标签中的内容的后面添加某些内容
suffixOverrides:在trim标签中的内容的后面去掉某些内容


    <select id="getDynamicSQL" resultType="xxx">
        select * from t_emp

        <trim prefix="where" suffixOverrides="and | or">

            <if test="name != '' and name != null">
                ename = #{ename} and
            </if>
            <if test="age != '' and age != null">
                age = #{age} and
            </if>
            <if test="sex != '' and sex != null">
                sex = #{sex}
            </if>

        </trim>
    </select>


choose、when、otherwise


    <select id="getEmpListByChoose" resultType="Emp">
        select * from t_emp
        
        <where>
            <choose>
                <when test="name != '' and name != null">
                    name = #{name}
                </when>
                <when test="age != '' and age != null">
                    age = #{age}
                </when>
                <when test="sex != '' and sex != null">
                    sex = #{sex}
                </when>
                <when test="email != '' and email != null">
                    email = #{email}
                </when>
                
                <otherwise>
				    did=1
				</otherwise>
				
            </choose>
        </where>
        
    </select>

foreach

mybatis的foreach标签经常用于遍历集合,构建in条件语句或者批量操作语句。


<foreach collection="集合类型" open="开始的字符" close="结束的字符"
    	 item="集合中的成员" separator="集合成员之间的分割符">
    #{item的值}
</foreach>

======================================================================================================
案例:

<!--foreach循环简单类型的List-->

    <select id="selectForeachOne" resultType="com.itjuzi.entity.Student">
        select * from student
        <if test="list != null and list.size>0">
            where id in
            <foreach collection="list" open="(" close=")" separator="," item="myId">
                #{myId}
            </foreach>
        </if>
    </select>
    

======================================================================================================



<!--foreach循环对象List<Student>-->

    <select id="selectForeachTwo" resultType="com.itjuzi.entity.Student">
        select * from student
        <if test="list != null and list.size>0">
            where id in
            <foreach collection="list" open="(" close=")" separator="," item="student">
                #{student.id}
            </foreach>
        </if>
    </select>

SQL片段

设置sql片段:<sql id="Columns">id,name,age,sex</sql>
引用sql片段:<include refid="Columns"></include>

<!--定义代码p片段-->
    <sql id="studentSelect">
        select * from student
    </sql>
    
 <select id="selectByIf" resultType="com.itjuzi.entity.Student">
 
        <include refid="studentSelect"/>
        
        where id=-1
        <if test="name != null and name!='' ">
            or name like "%" #{name} "%"
        </if>
        <if test="age > 0">
            or age >= #{age}
        </if>
    </select>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

如青春如烈火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值