MyBatis快速入门(下)

33 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第13天,点击查看活动详情

模糊查询

image-20221011234319075.png

image-20221011234927461.png

占位符在' '中所以不会预编译调用set方法 不能直接使用这种方法使用#{}

第一种方法解决

将'%#{值}%‘换成'%${值}%‘即可解决

第2种方法解决 image-20221011235305615.png 通过concat拼接可以完成模糊查询

第3种方法

image-20221011235510379.png

Mybatis处理批量删除

image-20221012201215753.png

要通过mybatis实现这样的效果只能使用

${}这一种方式

使用#号产生的真正的sql语句是这样的

image-20221012202426112.png

在masql中无法实现要想要的效果

动态设置表名

/**    SpecialSQLMapper
 * 动态设置表别名 查询所有的表名
* @author zzw
* @version 1.0
* Create by 2022/10/12  20:36
*/
List<User> getUserList(@Param("tableName") String tableName);
<select id="getUserList" resultType="User">
    select * from ${tableName}
</select>

image-20221012204118756.png

image-20221012204130716.png 因为表名在mysql中没有引号所以要用${}

添加功能获得自增的主键

image-20221012205035844.png

image-20221012204936497.png

<!--    useGeneratedKeys 表示当前添加功能使用自增的主键
keyProperty 将 添加的数据的自增住建委实体类类型的参数的属性复制-->
    <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
        insert into t_user values(null,#{uname},#{pwd},#{age},#{sex},#{email})
    </insert>

image-20221012210429676.png

自定义映射resultMap

使用全局配置处理字段命和属性名不一致的情况

字段名和属性名不一致的情况,如何处理映射关系

1.为查询的字段设置别名 和属性名保持一致

2.当字段符合MySQL的要求使用_,二属性符合java的要求使用驼峰

此时可以在myBatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰

在核心配置文件添加驼峰

    <settings>
<!--        将下划线映射为驼峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
<!--        name为映射下划线为驼峰-->
    </settings>

下划线映射驼峰的规则

emp_id:empId,emp_name:empName

image-20221012214158259.png

3.使用resultMap自定义映射处理

小tips:xml和html的注释不能嵌套

多表查询多对一的查询方式

image-20221012231221336.png

image-20221012231206387.png

使用resultmap的第一种方式

image-20221012231103250.png      select     t_emp.,t_dept.     from t_emp left join t_dept on     t_emp.dept_id=t_dept.dept_id     where emp_id=#{empId} 

<resultMap id="getEmpAndDeptByEmpIdMap" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
    <result column="dept_id" property="dept.deptId"></result>
    <result column="dept_name" property="dept.deptName"></result>
</resultMap>

image-20221012231103250.png

第二种通过association:处理多对一的映射关系 处理实体类类型的属性

property设置需要处理映射关系的属性的属性名

javaType:设置要处理的属性的类型

<select id="EmpAndDeptByEmpId" resultMap="getEmpAndDeptByEmpIdMap1">
    select
    t_emp.*,t_dept.*
    from t_emp left join t_dept on
    t_emp.dept_id=t_dept.dept_id
    where emp_id=#{empId}
</select>
<resultMap id="getEmpAndDeptByEmpIdMap1" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
   <association property="dept" javaType="Dept">
       <id column="dept_id" property="deptId"></id>
       <result column="dept_name" property="deptName"></result>
   </association>
</resultMap>

image-20221012231451074.png

在进行多表查询时出现了

image-20221012230332362.png

经过种种排查好像mybatis不能直接复制module可能会造成环境有问题更换了一个环境解决了这个问题

image-20221012230703751.png

第三种使用association完成分布查询

DeptMapper

List<Dept> getDeptById(@Param("deptId") Integer deptId);

DeptMapper.xml

<select id="getDeptById" resultType="Dept">
    select * from t_dept where dept_id=#{deptId}
</select>

EmpMapper.xml

<resultMap id="getEmpAndDeptByEmpIdMap2" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
    <association property="dept" select="com.atguigu.mybatis.mapper.DeptMapper.getDeptById" column="dept_id">
    </association>
    <select id="EmpAndDeptByEmpId1" resultMap="getEmpAndDeptByEmpIdMap2">
       select * from t_emp where emp_id=#{empId}
    </select>

EmpMapper

Emp EmpAndDeptByEmpId1(@Param("empId") Integer empId);

标签中的 property:设置需要处理映射关系的属性的属性名

select:设置分布查询的sql的唯一标识

column:将查询出的某个字段作为分布查询的sql的条件

延迟加载

分布查询的优点:可以查询实现延迟加载

在核心文件中添加实现延迟加载的配置

<!--        开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
<!--        按需加载-->
        <setting name="aggressiveLazyLoading" value="false"/>

image-20221013192135614.png

标签中的 property:设置需要处理映射关系的属性的属性名

select:设置分布查询的sql的唯一标识

column:将查询出的某个字段作为分布查询的sql的条件

fetchTyepe:在开启了延迟加载的环境中,通过该属性设置当前的分布查询是否使用延迟加载

fetchType="eager(立即加载)|lazy(延迟加载)"

在核心文件添加实现延迟加载的配置后

image-20221013192501209.png

只查询了一个表

在resultmap中添加lfetchtype参数为eager时

image-20221013192624633.png

image-20221013192624633.png

两个表都进行了查询

将fetchType参数改为lazy时

image-20221013192758800.png

又变成了只查询一个表

通过collection实现一对多的多表查询

<!-- 多表查询的方式-->
<resultMap id="DeptAndEempBYDeptIdMap" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emp" ofType="Emp">
<!--            ofType 设置集合类型的属性中存储的数据的类型-->
            <id column="emp_id" property="empId"></id>
            <result column="emp_name" property="empName"></result>
            <result column="age" property="age"></result>
            <result column="gender" property="gender"></result>
        </collection>
    </resultMap>
​
    <select id="getDeptAndEmpByDeptId" resultMap="DeptAndEempBYDeptIdMap">
        select * from t_dept left join t_emp on t_dept.dept_id=t_emp.dept_id
        where t_dept.dept_id=#{deptId}
    </select><!--            分步查询的方式-->
    <resultMap id="DeptAndEempBYDeptIdMap1" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emp" select="com.atguigu.mybatis.mapper.EmpMapper.getEmpByDeptId" column="dept_id">
            <!--            ofType 设置集合类型的属性中存储的数据的类型-->
        </collection>
    </resultMap>
    <select id="getDeptAndEmpByDeptId1" resultMap="DeptAndEempBYDeptIdMap1">
        select * from t_dept  where dept_id=#{deptId}
    </select>

一对一和多对多也要使用resultmap

如果在pojo中定义的是一个实体类那么就要使用association标签

如果定义的是一个列表那个就要使用collection标签

性质上差不多