参数传递:有时Mapper接口需要将一个或多个参数传递到XML映射文件中,那么XML文件如何接收到来自Mapper接口的参数数据呢?
1、Mapper接口方法只有一个参数时,则XML映射文件中获取该参数数据时硬性的要求,比如:接口抽象方法参数为String id,则XML映射文件中可通过#{任意字符串 }获取到该参数的数据。
2、Mapper接口方法当有多个参数时,默认情况下MyBatis将这些参数放在Map集合中,key为arg0...argn或param1...paramn,在XML映射文件中通过#{key}获取key所对应的参数数据;但为了在获取参数数据时XML映射文件中的key有意义,可以通过在Mapper接口相应抽象方法的参数数据类型前添加@Param注解的方式指定key的值
3、当参数为自定义引用类型时,则XML映射文件直接使用#{成员变量名}的方式获取相应成员变量的值。
${}与#{}区别:${}直接将数据和sql进行拼接,无法防止SQL注入;#{}以?方式作为占位符,可以防止SQL注入;
增删改查:MyBatis框架中通过在XML映射配置文件定义insert、delete、update和select标签进而定义相应SQL语句的方式实现增删改查操作:
insert标签:
返回添加数据的id:
①、数据库支持自增:在insert标签指定useGeneratedKeys标签属性为true,keyProperty标签属性指定将返回的主键值赋给哪个自定义类中的属性
delete标签:
update标签:
select标签:
返回List集合:
①、resultType为自定义类型,此时需要开启MyBatis自动映射功能,必要时需要开启驼峰命名规则;
②、使用resultMap标签进行自定义字段与成员变量之间的映射关系,并在select标签中设定resultMap标签属性。注意:resultType和resultMap不能同时使用
返回Map集合:
①、返回一条数据:key为字段名,value为字段对应的值——select标签resultType标签属性为map即可;
②、返回一条数据:key为指定列的值(比如id),value为自定义类型对象——select标签resultType标签属性为自定义类型,在Mapper接口相应抽象方法添加@MapKey注解,其值为自定义类型成员变量属性名;
resultMap标签属性用于自定义结果集映射规则:
单表自定义映射:
<!-- type:单个元素的数据类型 -->
<resultMap type="com.lq.vo.UserInfo" id="userInfo">
<!-- column:指定列名 property:指定把当前列中的某个数据赋给Java类中的哪个属性-->
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<!-- 不指定的列会自动封装 -->
<result column="password" property="password"/>
</resultMap>
<select id="selectByUserName" resultMap="userInfo">
select * from user_info where user_name like #{userName}
</select>
多表自定义映射:
一对一:一个用户只有一个地址
<resultMap type="com.lq.vo.UserInfo" id="userInfo">
<id column="user_id" property="id"/>
<result column="user_name" property="userName"/>
....
<!-- address.id:address为UserInfo Address类型属性名;id为Address类成员变量 -->
<result column="address_id" property="address.id"/>
....
</resultMap>
<select id="selectById" resultMap="userInfo">
select ui.id user_id,user_name,password,addr.id address_id,real_name,mobile,detail
from user_info ui
left address addr join on ui.id=addr.user_id
where ui.id = #{id}
</select>
<resultMap type="com.lq.vo.UserInfo" id="userInfo">
<id column="user_id" property="id"/>
<result column="user_name" property="userName"/>
......
<!-- property标签属性值为UserInfo Address类型属性名;javaType:指定address属性数据类型[不能省略] -->
<association property="address" javaType="com.lq.vo.Address">
<id column="address_id" property="id"/>
<result column="real_name" property="realName"/>
......
</association>
</resultMap>
<select id="selectById" resultMap="userInfo">
select ui.id user_id,user_name,password,addr.id address_id,real_name,mobile,detail
from user_info ui
left address addr join on ui.id=addr.user_id
where ui.id = #{id}
</select>
一对多:一个用户有多个地址
<resultMap type="com.lq.vo.UserInfo" id="userInfo">
<id column="user_id" property="id"/>
<result column="user_name" property="userName"/>
......
<!--
property:addressList为UserInfo类成员变量
ofType:集合元素数据类型
-->
<collection property="addressList" ofType="com.lq.vo.Address">
<id column="address_id" property="id"/>
<result column="real_name" property="realName"/>
......
</collection>
</resultMap>
<select id="selectById" resultMap="userInfo">
select ui.id user_id,user_name,password,addr.id address_id,real_name,mobile,detail
from user_info ui
left address addr join on ui.id=addr.user_id
where ui.id = #{id}
</select>