一.MyBatis获取参数值的两种方式(重点)
MyBatis获取参数值的两种方式:${}和#{}
${}的本质就是字符串拼接
#{}的本质就是占位符赋值
${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但是#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号
/** * MyBatis获取参数值的各种情况:
* 1、mapper接口方法的参数为单个的字面量类型
* 可以通过${}和#{}以任意的名称获取参数值,但是需要注意${}的单引号问题
* 2、mapper接口方法的参数为多个时
* 此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储
* a>以arg0,arg1...为键,以参数为值
* b>以param1,param2...为键,以参数为值
* 因此只需要通过#{}和${}以键的方式访问值即可,但是需要注意${}的单引号问题
* 3、若mapper接口方法的参数有多个时,可以手动将这些参数放在一个map中存储
* 只需要通过#{}和${}以键的方式访问值即可,但是需要注意${}的单引号问题
* 4、mapper接口方法的参数是实体类类型的参数
* 只需要通过#{}和${}以属性的方式访问属性值即可,但是需要注意${}的单引号问题
* 5、使用@Param注解命名参数
* 此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储
* a>以@Param注解的值为键,以参数为值
* b>以param1,param2...为键,以参数为值
* 因此只需要通过#{}和${}以键的方式访问值即可,但是需要注意${}的单引号问题
*/
二.MyBatis的各种查询功能
1.查詢一个实体类对象
//根据id查询用户信息
User getUserById(@Param("id") Integer id);
<select id="getUserById" resultType="User">
select * from t_user where id = #{id}
</select>
2.查询一个list集合
//查询所有用户信息
List<User> getUserList();
<select id="getUserList" resultType="User">
select * from t_user
</select>
3.查询单个数据
// 查询用户信息的总记录数
Integer getCount();
<select id="getCount" resultType="java.lang.Integer">
select count(*) from t_user
</select>
4.查询一条数据为map集合
//根据id查询用户信息为一个map集合
Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id = #{id}
</select>
5.查询多条数据为map集合
方式一:
// 查询所有用户信息为map集合
List<Map<String, Object>> getAllUserToMap();
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
结果为:
<!--[{password=123456, sex=男, id=1, age=23, email=12345@qq.com, username=admin},
{password=23456, sex=男, id=2, age=20, email=13862458@qq.cm, username=la},
{password=123, sex=男, id=3, age=23, email=123@qq.com, username=李四}] -->
方式二:
//添加@MapKey注解,此时就可以将每条数据转换的map集合作为值,以某个字段的值作为键,放在同一个map集合中
@MapKey("id")
Map<String, Object> getAllUserToMap();
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select
结果为:
<!-- {1={password=123456, sex=男, id=1, age=23, email=12345@qq.com, username=admin},
2={password=23456, sex=男, id=2, age=20, email=13862458@qq.cm, username=la},
3={password=123, sex=男, id=3, age=23, email=123@qq.com, username=李四}}-->
三.自定义映射resultMap
1、resultMap处理字段和属性的映射关系
若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射
<!-- resultMap:设置自定义映射关系
id:唯一标识,不能重复
type:设置映射关系中的实体类类型
子标签:
id:设置主键的映射关系
result:设置普通字段的映射关系
collection:处理一对多的映射关系
association:处理一对一映射关系
属性:
property:设置映射关系中的属性名,必须是type属性所设置的实体类类型中的属性名
column:设置映射关系中的字段名,必须是sql语句查询出的字段名 -->
<resultMap id="deptAndEmpResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<!--collection:处理一对多的映射关系
ofType:表示该属性所对应的集合中存储数据的类型-->
<collection property="emps" ofType="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>
</collection>
</resultMap>
<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>
<!--分步查询 -->
<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>
<!--select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
column:设置分布查询的条件
fetchType:当开启了全局的延迟加载之后,可通过此属性手动控制延迟加载的效果
fetchType="lazy|eager":lazy表示延迟加载,eager表示立即加载-->
<association property="dept"
select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did"
fetchType="eager"></association>
</resultMap>
分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:
lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载
此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType=“lazy(延迟加载)|eager(立即加载)”
在mybatis-config.xml中设置: