1:MyBatis基于XML的详细使用-参数、返回结果处理
1.1参数的获取
<!--获取参数的方式:
1.#{} ==> jdbc String sql=" SELECT id,user_name FROM EMP WHERE id=?"
1.会经过JDBC当中PreparedStatement的预编译,会根据不同的数据类型来编译成对应数据库所对应的数据。
2.能够有效的防止SQL注入。 推荐使用!!
特殊用法:
自带很多内置参数的属性:通常不会使用。了解
javaType、jdbcType、mode、numericScale、resultMap、typeHandler.
比如 需要改变默认的NULL===>OTHER:#{id,javaType=NULL}
想保留小数点后两位:#{id,numericScale=2}
2.${} ==> jdbc String sql=" SELECT id,user_name FROM EMP WHERE id="+id
1.不会进行预编译,会直接将输入进来的数据拼接在SQL中。
2.存在SQL注入的风险。不推荐使用。
特殊用法:
1.调试情况下可以临时使用。
2.实现一些特殊功能:前提一定要保证数据的安全性。
比如:动态表、动态列. 动态SQL.
-->
<select id="SelectEmp" resultType="Emp" resultMap="emp_map" >
SELECT id,user_name,create_date FROM EMP where id=#{id}
</select>
1.2 参数的传递
-
单个参数 SelectEmp(Integer id);
mybatis 不会做任何特殊要求
获取方式: #:{输入任何字符获取参数} -
多个参数:Emp SelectEmp(Integer id,String username);
mybatis 会进行封装 会将传进来的参数封装成map:
1个值就会对应2个map项 : id===> {key:arg0 ,value:id的值},{key:param1 ,value:id的值}
username===> {key:arg1 ,value:id的值},{key:param2 ,value:id的值}
获取方式:没使用了@Param:
id=====> #{arg0} 或者 #{param1}
username=====> #{arg1} 或者 #{param2}设置参数的别名:@Param(""):SelectEmp(@Param(“id”) Integer id,@Param(“username”) String username);
当使用了@Param:
id=====> #{id} 或者 #{param1}
username=====> #{username} 或者 #{param2} -
javaBean的参数:
单个参数:Emp SelectEmp(Emp emp);
获取方式:可以直接使用属性名
emp.id=====>#{id}
emp.username=====>#{username}多个参数:Emp SelectEmp(Integer num,Emp emp);
num===> #{param1} 或者 @Param
emp===> 必须加上对象别名: emp.id===> #{param2.id} 或者 @Param(“emp”)Emp emp ==>#{emp.id}
emp.username=> #{param2.username} 或者 @Param(“emp”)Emp emp ====>#{emp.username} -
集合或者数组参数:
Emp SelectEmp(List usernames);
如果是list,MyBatis会自动封装为map:
{key:“list”:value:usernames}没用@Param("")要获得:usernames.get(0) =====> #{list[0]} :usernames.get(0) =====> #{agr0[0]} 有@Param("usernames")要获得:usernames.get(0) =====> #{usernames[0]} :usernames.get(0) =====> #{param1[0]} 如果是数组,MyBatis会自动封装为map: {key:"array":value:usernames} 没用@Param("")要获得:usernames.get(0) =====> #{array[0]} :usernames.get(0) =====> #{agr0[0]} 有@Param("usernames")要获得:usernames.get(0) =====> #{usernames[0]} :usernames.get(0) =====> #{param1[0]}
-
map参数
-
一般情况下:
请求进来的参数 和pojo对应,就用pojo
请求进来的参数 没有和pojo对应,就用map
请求进来的参数 没有和pojo对应上,但是使用频率很高,就用TO、DTO(就是单独为这些参数创 建一个对应的javaBean出来,使参数传递更规范、更重用)
<!--
接口:SelectEmp(String username,@Param("id") Integer id);
username====> #{arg0} #{param1}
id====> #{id} #{param2}
接口:SelectEmp(@Param("beginDate") String beginDate,
String endDate,
Emp emp);
beginDate====> #{beginDate} #{param1}
endDate====> #{arg1} #{param2}
emp.id====>#{arg2.id} #{param2.id}
接口:SelectEmp(List<Integer> ids,
String[] usernames,
@Param("beginDate") String beginDate,
String endDate,);
ids.get(0)=====> #{list[0]} #{param1[0]}
usernames[0]=====> #{array[0]} #{param2[0]}
beginDate====> #{beginDate} #{param3}
end====> #{arg3} #{param4}
-->
1.3处理集合返回结果
<!--当返回值的结果是集合的时候,返回值的类型依然写的是集合中具体的类型-->
<select id="selectAllEmp" resultType="cn.tulingxueyuan.bean.Emp">
select * from emp
</select>
<!--在查询的时候可以设置返回值的类型为map,当mybatis查询完成之后会把列的名称作为key
列的值作为value,转换到map中
-->
<select id="selectEmpByEmpReturnMap" resultType="map">
select * from emp where empno = #{empno}
</select>
<!--注意,当返回的结果是一个集合对象的是,返回值的类型一定要写集合具体value的类型
同时在dao的方法上要添加@MapKey的注解,来设置key是什么结果
@MapKey("empno")
Map<Integer,Emp> getAllEmpReturnMap();-->
<select id="getAllEmpReturnMap" resultType="cn.tulingxueyuan.bean.Emp">
select * from emp
</select>
public interface EmpDao {
public Emp findEmpByEmpno(Integer empno);
public int updateEmp(Emp emp);
public int deleteEmp(Integer empno);
public int insertEmp(Emp emp);
Emp selectEmpByNoAndName(@Param("empno") Integer empno, @Param("ename") String ename,@Param("t") String tablename);
Emp selectEmpByNoAndName2(Map<String,Object> map);
List<Emp> selectAllEmp();
Map<String,Object> selectEmpByEmpReturnMap(Integer empno);
@MapKey("empno")
Map<Integer,Emp> getAllEmpReturnMap();
}
1.4 自定义结果集–resultMap
<!--1.声明resultMap自定义结果集 resultType 和 resultMap 只能使用一个。
id 唯一标识, 需要和<select 上的resultMap 进行对应
type 需要映射的pojo对象, 可以设置别名
autoMapping 自动映射,(默认=true) 只要字段名和属性名遵循映射规则就可以自动映射
,但是不建议,哪怕属性名和字段名一一对应上了也要显示的配置映射
extends 如果多个resultMap有重复映射,可以声明父resultMap,将公共的映射提取出来,
可以减少子resultMap的映射冗余
-->
<resultMap id="emp_map" type="emp" autoMapping="false" extends="common_map">
<result column="create_date" property="cjsj"></result>
</resultMap>
<resultMap id="common_map" type="emp" autoMapping="false" >
<!-- <id> 主键必须使用 对底层存储有性能作用
column 需要映射的数据库字段名
property 需要映射的pojo属性名
-->
<id column="id" property="id"></id>
<result column="user_name" property="username"></result>
</resultMap>
<!--2.使用resultMap 关联 自定义结果集的id-->
<select id="SelectEmp" resultType="Emp" resultMap="emp_map" >
SELECT id,user_name,create_date FROM EMP where id=#{id}
</select>
2:MyBatis基于XML的详细使用——高级结果映射
2.1联合查询
<!-- 实现表联结查询的方式: 可以映射: DTO -->
<resultMap id="QueryEmp_Map" type="QueryEmpDTO">
<id column="e_id" property="id"></id>
<result column="user_name" property="username"></result>
<result column="d_id" property="deptId"></result>
<result column="dept_name" property="deptName"></result>
</resultMap>
<select id="QueryEmp" resultMap="QueryEmp_Map">
select t1.id as e_id,t1.user_name,t2.id as d_id,t2.dept_name from emp t1
INNER JOIN dept t2 on t1.dept_id=t2.id
where t1.id=#{id}
</select>
<!-- 实现表联结查询的方式: 可以映射map -->
<resultMap id="QueryEmp_Map" type="map">
<id column="e_id" property="id"></id>
<result column="user_name" property="username"></result>
<result column="d_id" property="deptId"></result>
<result column="dept_name" property="deptName"></result>
</resultMap>
<select id="QueryEmp" resultMap="QueryEmp_Map">
select t1.id as e_id,t1.user_name,t2.id as d_id,t2.dept_name from emp t1
INNER JOIN dept t2 on t1.dept_id=t2.id
where t1.id=#{id}
</select>
2.2嵌套结果
- 多对一
<!--嵌套结果 多 对 一 -->
<resultMap id="QueryEmp_Map2" type="Emp">
<id column="e_id" property="id"></id>
<result column="user_name" property="username"></result>
<!--
association 实现多对一中的 “一”
property 指定对象中的嵌套对象属性
-->
<association property="dept">
<id column="d_id" property="id"></id>
<id column="dept_name" property="deptName"></id>
</association>
</resultMap>
<select id="QueryEmp2" resultMap="QueryEmp_Map2">
select t1.id as e_id,t1.user_name,t2.id as d_id,t2.dept_name from emp t1
INNER JOIN dept t2 on t1.dept_id=t2.id
where t1.id=#{id}
- 一对多
- <!-- 嵌套结果: 一对多 查询部门及所有员工 -->
<resultMap id="SelectDeptAndEmpsMap" type="Dept">
<id column="d_id" property="id"></id>
<id column="dept_name" property="deptName"></id>
<!--
<collection 映射一对多中的 “多”
property 指定需要映射的“多”的属性,一般声明为List
ofType 需要指定list的类型
-->
<collection property="emps" ofType="Emp" >
<id column="e_id" property="id"></id>
<result column="user_name" property="username"></result>
<result column="create_date" property="createDate"></result>
</collection>
</resultMap>
<select id="SelectDeptAndEmps" resultMap="SelectDeptAndEmpsMap">
select t1.id as d_id,t1.dept_name,t2.id e_id,t2.user_name,t2.create_date from dept t1
LEFT JOIN emp t2 on t1.id=t2.dept_id
where t1.id=#{id}
</select>
2.3嵌套查询
在上述逻辑的查询中,是由我们自己来完成sql语句的关联查询的,那么,我们能让mybatis帮我们实现自动的关联查询吗?
- 多对一
<!--嵌套查询(分步查询) 多 对 一
联合查询和分步查询区别: 性能区别不大
分部查询支持 懒加载(延迟加载)
需要设置懒加载,一定要使用嵌套查询的。
要启动懒加载可以在全局配置文件中设置 lazyLoadingEnabled=true
还可以单独为某个分步查询设置立即加载 <association fetchType="eager"
-->
<resultMap id="QueryEmp_Map3" type="Emp">
<id column="id" property="id"></id>
<result column="user_name" property="username"></result>
<!-- association 实现多对一中的 “一”
property 指定对象中的嵌套对象属性
column 指定将哪个字段传到分步查询中
select 指定分步查询的 命名空间+ID
以上3个属性是实现分步查询必须的属性
fetchType 可选, eager|lazy eager立即加载 lazy跟随全局配置文件中的lazyLoadingEnabled
-->
<association property="dept" column="dept_id" select="cn.tulingxueyuan.mapper.DeptMapper.SelectDept">
</association>
</resultMap>
<select id="QueryEmp3" resultMap="QueryEmp_Map3">
select * from emp where id=#{id}
</select>
xxx
<!-- 根据部门id查询部门-->
<select id="SelectDept" resultType="dept">
SELECT * FROM dept where id=#{id}
</select>
- 一对多
- <!-- 嵌套查询(异步查询): 一对多 查询部门及所有员工 -->
<resultMap id="SelectDeptAndEmpsMap2" type="Dept">
<id column="d_id" property="id"></id>
<id column="dept_name" property="deptName"></id>
<!--
<collection 映射一对多中的 “多”
property 指定需要映射的“多”的属性,一般声明为List
ofType 需要指定list的类型
column 需要将当前查询的字段传递到异步查询的参数
select 指定异步查询
-->
<collection property="emps" ofType="Emp" column="id" select="cn.tulingxueyuan.mapper.EmpMapper.SelectEmpByDeptId" >
</collection>
</resultMap>
<select id="SelectDeptAndEmps2" resultMap="SelectDeptAndEmpsMap2">
SELECT * FROM dept where id=#{id}
</select>
EmpMapper.xml
<!-- 根据部门id所有员工 -->
<select id="SelectEmpByDeptId" resultType="emp">
select * from emp where dept_id=#{id}
</select>
2.4延迟查询
当我们在进行表关联的时候,有可能在查询结果的时候不需要关联对象的属性值,那么此时可以通过延迟加载来实现功能。在全局配置文件中添加如下属性
mybatis-config.xml
<!-- 开启延迟加载,所有分步查询都是懒加载 (默认是立即加载)-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--当开启式, 使用pojo中任意属性都会加载延迟查询 ,默认是false
<setting name="aggressiveLazyLoading" value="false"/>-->
<!--设置对象的哪些方法调用会加载延迟查询 默认:equals,clone,hashCode,toString-->
<setting name="lazyLoadTriggerMethods" value=""/>
如果设置了全局加载,但是希望在某一个sql语句查询的时候不使用延时策略,可以添加fetchType下属性:
<association property="dept" fetchType="eager" column="dept_id" select="cn.tulingxueyuan.mapper.DeptMapper.SelectDept">
</association>
总结:
https://note.youdao.com/ynoteshare1/index.html?id=5d41fd41d970f1af9185ea2ec0647b64&type=notebook#/735C4CF2879944A99F5ED8DFF3AC52FD