一:mybatis中#{}和 ${} 向sql传参时的区别
二:动态拼装sql语句常用的标签(where if,foreacher)
三:mybatis的入参类型
四:resultType和resultMap的区别
五:mybatis的返回参数类型
一:mybatis中#{},${}
向sql传参时的区别
(1)首先一点就是,#{}传递参数时,会在传递的参数上加上引号,在传递属性比如 name=? 时,可以很方便的使用#{}。而${}
则不会添加引号,传递的是什么就会直接放到SQL中去执行。
(2)#{}传递的参数实际上是通过占位符去传入到已经预编译好的SQL中去的,所以此时的SQL已经完成编译,只需要传参数就完成执行了。而${}
在日志中显示的是直接将参数拼接成完整的SQL去DBMS中编译执行的。所以#{}方式实际上比${}
方式更加安全,不会引起SQL注入。但是在传入表名参数时,只能使用${}
,这时候,必须要在接受参数的时候加入逻辑判断,判断参数中是否存在SQL语句,以防引起注入
所谓的sql注入是指在传参的时候加入一些特别的判断,比如登录的时候传用户名username的时候 zhangsan+OR 1=1 这种类型的判断,导致就算密码输入错误也能成功登录。
二:动态拼装sql语句常用的标签
1 where if
<select id="getStudent" parameterType="java.lang.String" resultType="Student">
select nn_grade, nn_class, nn_name, nn_age, nn_sex, nn_birthdate, nn_address from student
<where>
<if test="#{0} != null and #{0} != ''">
and nn_grade=#{0}
</if>
<if test="#{1} != null and #{1} != ''">
and nn_class=#{1}
</if>
<if test="#{2} != null and #{2} != ''">
and nn_name=#{2}
</if>
</where>
</select>
2 foreacher
item: 循环体中的具体对象
collection: 要做foreach的对象
separator: 元素之间的分隔符,例如在in()的时候,separator=”,”会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
open: foreach代码的开始符号,一般是(和close=”)”合用。常用在in(),values()时。该参数可选。
close: foreach代码的关闭符号,一般是)和open=”(“合用。常用在in(),values()时。该参数可选。
index: 在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。
<select id="countByUserList" resultType="int" parameterType="list">
select count(*) from users
<where>
id in
<foreach item="item" collection="list" separator="," open="(" close=")" index="">
#{item.id, jdbcType=NUMERIC}
</foreach>
</where>
</select>
三:mybatis的入参类型
入参类型:简单类型,List,pojo
简单类型:parameterType直接写参数的简单类型即可
public User get(Long id) {
return (User) getSqlSession().selectOne("com.findUserListByIdList" , id);
}
<select id="findUserListByIdList" parameterType="java.lang.Long" resultType="User">
select * from user where id = #{id};
</select>
注意:
(多个参数的情况下:)
1 如果Dao层中注解@Param(“”)来定义参数,则不需要写parameterType注意“#{}”内的参数名必须跟你在Dao层中注解@Param(“”)内定义的名称一致。
2 如果没有@Param(“”)来定义参数,也不需要写parameterType,但是需要用#{0}与#{1}对应你在Dao里的参数顺序
List类型:parameterType直接写参数的List类型即可
public List<Area> findUserListByIdList(List<Long> idList) {
return getSqlSession().selectList("com.liulanghan.findUserListByIdList", idList);
}
<select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach item="guard" index="index" collection="list"
separator=","> #{guard} </foreach>
)
</where>
</select>
注意:单独传入list时,foreach中的collection必须是list,不管变量的具体名称是什么。
数组类型:parameterType直接写参数的java.util.HashList类型即可
public List<Area> findUserListByIdList(int[] ids) {
return getSqlSession().selectList("com.liulanghan.findUserListByIdList", ids);
}
<select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach item="guard" index="index" collection="array"
separator=","> #{guard} </foreach>
)
</where>
</select>
Map类型参数:parameterType直接写参数的java.util.HashMap类型即可
pojo类型参数:parameterType直接写参数的Pojo路径即可
<select id="findUserListByDTO" parameterType="UserDTO" resultType="java.lang.Integer">
SELECT COUNT(*) FROM USER user
<where>
<if test="code != null">
and user.CODE = #{code}
</if>
<if test="id != null">
and user.ID = #{id}
</if>
<if test="idList !=null ">
and user.ID in (
<foreach item="guard" index="index" collection="idList"
separator=","> #{guard} </foreach>
)
</if>
</where>
</select>
注意:java pojo对象中有list或array时,foreach中的collection必须是具体list或array的变量名。比如这里UserDTO含有一个名为idList的list,所以UserDTO中用idList取值,这点和单独传list或array时不太一样。
四:resultType和resultMap的区别
resultType和resultMap只能有一个成立,resultType是直接表示返回类型的,如果pojo的属性与数据库的字段不一致时会获取不到值,而resultMap则是对外部ResultMap的引用,resultMap解决复杂查询是的映射问题。比如:列名和对象属性名不一致时可以使用resultMap来配置;还有查询的对象中包含其他的对象等。
实现一对一查询:
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
如果没有查询结果的特殊要求建议使用resultType。
resultMap:需要单独定义resultMap,association(用于一对多)里面定义其他属性,实现有点麻烦,如果对查询结果有特殊的要求,使用resultMap可以完成将关联查询映射pojo的属性中。
resultMap可以实现延迟加载,resultType无法实现延迟加载
实现一对多查询:
mybatis使用resultMap的collection对关联查询的多条记录映射到一个list集合属性中。
使用resultType实现:
将订单明细映射到orders中的orderdetails中,需要自己处理,使用双重循环遍历,去掉重复记录,将订单明细放在orderdetails中。
五:mybatis的返回参数类型
resultType的类型可以为:
1、基本类型 :resultType=基本类型
2、pojo类型:单个pojo类型
3、List类型: resultType=List中元素的类型
4、Map类型 单条记录:resultType =map
多条记录:resultType =Map中value的类型
1、基本类型:直接返回字段类型
2、pojo类型:返回对象全路径即可
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.mybatis.dao.UserMapper">
<!--resultType直接写对象的全类名 -->
<select id="getHotel" resultType="com.test.mybatis.po.User">
select * from User
where
id=#{id}
</select>
</mapper>
3、List类型: resultType为List中对象的类型,如List<User>
,resultType为User
接口代码示例:
package com.test.mybatis.dao;
import java.util.List;
import com.test.mybatis.po.User;
public interface UserMapper {
// 返回值为List
public List<User> getUserl(Integer i);
}
mapper.xml实现:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.mybatis.dao.UserMapper">
<!-- 返回值为List,resultType为List中元素的全类名 -->
<select id="getUser" resultType="com.test.mybatis.po.User">
select * from user
where
username>#{username}
</select>
</mapper>
4、Map类型 单条记录:resultType =map
多条记录:resultType =Map中value的类型
单条记录:单挑记录情况下map的key为属性,值为属性值。resultType为map
mapper.java
package com.test.mybatis.dao;
import java.util.Map;
import com.test.mybatis.po.User;
public interface HotelUser {
// 返回值为Map,key为属性名,value为属性值
public Map<String, Object> getUser(Integer i);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.mybatis.dao.UserMapper">
<!-- 返回值为map,resultType为map -->
<select id="getUser" resultType="map">
select * from user
where
id=#{id}
</select>
</mapper>
多条记录:resultType=Map中value的类型,key为任意一属性,值为对象类型。如Map<String,User>
,resultType为User返回多条记录的map时,key为任意一属性,值为对象类型,不过key需要通过@MapKey(“userName”)指定对象中一个属性名为key
package com.pjf.mybatis.dao;
import java.util.Map;
import org.apache.ibatis.annotations.MapKey;
import com.test.mybatis.po.User;
public interface UserMapper {
// 返回值为Map,key需要通过@MapKey("属性名")来指定javaBean中的一个属性名为key,value为对象
@MapKey("userName")
public Map<String, User> getUser(Integer i);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.mybatis.dao.UserlMapper">
<!-- 返回值为map,resultType为对象的全类名 -->
<select id="getUser" resultType="com.test.mybatis.po.User">
select * from user
where
age>#{age}
</select>
</mapper>