###传参
#{}
的本质就是占位符赋值?,${}
的本质就是字符串拼接,${}
需要手动加单引号
- 字段传参
//单个参数,${}和#{}可以用任意的名称获取参数的值
SysUser queryUser(Integer userId);
<select id="queryUser" resultType="SysUser">
select * from sys_user where user_id = #{parm}
</select>
//多个参数,MyBatis会自动将这些参数放在一个map集合中,以arg0,arg1...或param1,param2...为键,以参数为值
SysUser queryUser(Integer userId,Integer deptId);
<select id="queryUser" resultType="SysUser">
select * from sys_user where user_id = #{param1} and dept_id = #{param2}
</select>
//可以使用@Param标识参数,以@Param注解的value属性值或param1,param2...为键,以参数为值
SysUser queryUser(@Param(value = "userId") Integer userId,@Param(value = "deptId") Integer deptId);
<select id="queryUser" resultType="SysUser">
select * from sys_user where user_id = #{userId} and dept_id = #{deptId}
</select>
2.Map集合传参
3.实体类传参:使用${}
和#{}
,通过访问实体类对象中的属性名获取属性值,注意${}
需要手动加单引号
查询
-
resultType:自动映射,用于属性名和表中字段名一致的情况 resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况
-
resultType
在MyBatis中,对于Java中常用的类型都设置了类型别名
别名 | 类型 | 别名 | 类型 | 别名 | 类型 |
---|---|---|---|---|---|
_byte | byte | _char (since 3.5.10) | char | _character (since 3.5.10) | char |
_long | long | _short | short | _short | short |
_int | int | _integer | int | _double | double |
_float | float | _boolean | boolean | string | String |
byte | Byte | char(since 3.5.10) | Character | character (since 3.5.10) | Character |
long | Long | short | Short | int | Integer |
integer | Integer | double | Double | float | Float |
boolean | Boolean | date | Date | decimal | BigDecimal |
bigdecimal | BigDecimal | biginteger | BigInteger | object | Object |
- 查询多条Map处理方式
1.使用List<Map>
接受
List<Map<String,Object>> queryUser2Map();
<select id="queryUser2Map" resultType="map">
select * from sys_user
</select>
2.使用@MapKey
//返回一个以user_id字段值为key,以key对应的单个Map为值的Map
@MapKey("user_id")
Map queryUser2Map();
<select id="queryUser2Map" resultType="map">
select * from sys_user
</select>
{10003={del_flag=0, user_name=李六, sex=0, phonenumber=, avatar=, login_ip=, create_by=, password=, user_type=男, user_id=10003, nick_name=liliu, dept_id=1, update_by=, email=12345@qq.com, status=0}, 10004={del_flag=0, user_name=李六, sex=0, phonenumber=, avatar=, login_ip=, create_by=, password=, user_type=男, user_id=10004, nick_name=liliu, dept_id=1, update_by=, email=12345@qq.com, status=0}}
- resultMap
若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射
<!--type:查询的数据要映射的实体类的类型 id:表示自定义映射的唯一标识 -->
<resultMap type="com.liliu.mybatis.pojo.SysUser" id="sysUserResultMap">
<!--id:设置主键的映射关系 result:设置普通字段的映射关系
association:设置多对一的映射关系 collection:设置一对多的映射关系-->
<id column="user_id" property="userId"/>
<result column="dept_id" property="deptId"/>
<result column="user_name" property="userName"/>
</resultMap>
1.多对一映射
<resultMap type="com.liliu.mybatis.pojo.SysUser" id="sysUserResultMap">
<id column="user_id" property="userId"/>
<result column="dept_id" property="deptId"/>
<result column="user_name" property="userName"/>
<result column="user_name" property="userName"/>
<result column="nick_name" property="nickName"/>
<result column="user_type" property="userType"/>
<!--方法1-->
<!--<result column="parent_id" property="sysDept.parentId"/>
<result column="dept_name" property="sysDept.deptName"/>-->
<!--方法2-->
<association property="sysDept" javaType="com.liliu.mybatis.pojo.SysDept" >
<id column="dept_id" property="deptId"/>
<result column="parent_id" property="parentId"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap>
2.一对多映射
<resultMap type="com.liliu.mybatis.pojo.SysDept" id="sysDeptResultMap">
<id column="dept_id" property="deptId"/>
<result column="parent_id" property="parentId"/>
<result column="dept_name" property="deptName"/>
<result column="phone" property="phone"/>
<collection property="sysUserList" ofType="com.liliu.mybatis.pojo.SysUser">
<id column="user_id" property="userId"/>
<result column="dept_id" property="deptId"/>
<result column="user_name" property="userName"/>
<result column="user_name" property="userName"/>
<result column="nick_name" property="nickName"/>
<result column="user_type" property="userType"/>
</collection>
</resultMap>
3.分布查询
<resultMap type="com.liliu.mybatis.pojo.SysDept" id="sysDeptResultMap2">
<id column="dept_id" property="deptId"/>
<result column="parent_id" property="parentId"/>
<result column="dept_name" property="deptName"/>
<result column="phone" property="phone"/>
<!--分布查询fetchType="lazy(延迟加载)|eager(立即加载) select=子查询接口方法 column=关联字段-->
<collection property="sysUserList" fetchType="lazy" select="com.liliu.mybatis.mapper.SysUserMapper.queryUserList" column="dept_id"/>
</resultMap>
实现延迟加载必须在核心配置文件中设置全局配置信息: lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载 aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载 此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和 collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType=“lazy(延迟加载)|eager(立即加载)”
动态sql
- if、where
<delete id="delSysDept">
delete from sys_dept
/*where和if一般结合使用:若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键
若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and去掉
where标签无法去掉条件最后多余的and */
<where>
/*if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行*/
<if test="param1 != null and param1 != ''">
sys_dept.dept_id = #{param1}
</if>
<if test="param2 != null and param2 != ''">
and sys_dept.dept_name = #{param2}
</if>
</where>
</delete>
- trim、include
<sql id="delsql">
delete from sys_dept
</sql>
<delete id="delSysDept2">
/*sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入*/
<include refid="delsql" />
/*prefix:在trim标签中的内容的前面添加某些内容
prefixOverrides:在trim标签中的内容的前面去掉某些内容
suffix:在trim标签中的内容的后面添加某些内容
suffixOverrides:在trim标签中的内容的后面去掉某些内容*/
<trim prefix="where" prefixOverrides="and">
<if test="param1 != null and param1 != ''">
sys_dept.dept_id = #{param1} and
</if>
<if test="param2 != null and param2 != ''">
sys_dept.dept_name = #{param2}
</if>
</trim>
</delete>
- choose、when、otherwise
<select id="getEmpListByChoose" resultType="Emp">
select <include refid="empColumns"></include> from t_emp
<where>
<choose>
<when test="ename != '' and ename != null">
ename = #{ename}
</when>
<when test="age != '' and age != null">
age = #{age}
</when>
<when test="sex != '' and sex != null">
sex = #{sex}
</when>
</choose>
</where>
</select>
- foreach