mybatis 参数处理:
1.单个参数
直接使用#{参数名称},直接取就可以了。
2.多个参数
1.param1,param2 参数少可以这样使用
java dao层代码如下
public Employee getEmpByIdAndLastName(Integer id,String lastName);
maper.xml
<select id="getEmpByIdAndLastName" resultType="com.www.mybatis.bean.Employee">
select id,last_name lastName ,gender,email from table_employee where id=#{param1} and last_name=#{param2}
</select>
2.@param进行查找
java dao 层代码如下:
public Employee getEmpByIdAndLastName(@param ("id")Integer id,@param("lastName")String lastName);
mapper.xml
<select id="getEmpByIdAndLastName" resultType="com.www.mybatis.bean.Employee">
select id,last_name lastName ,gender,email from table_employee where id=#{id} and last_name=#{lastName}
</select>
3.如果是多个参数,正好是业务逻辑的数据模型
- 直接传入pojo #{属性名} 直接取出属性值;
- 如果有多个参数不是业务模型的数据,没有对应的pojo;可以传入map进行处理
- 如果是多个参数,没有对应的业务模型,而且经常使用的话,可以编写一个TO(Transfer Object)数据传输对象来完成。
如果是collection类型(List,set)或者是数组,会特殊处理,也是把list或者数据封装在map中。
如果是collection ,key为collection,list可以使用collection或者list
数组就是array;
public employee getEmpById(List<Integer> ids);
取值:取出第一个id的值:#{list[0]}
#{},${}的用法
1.安全的预处理相比${}
2.#{} 可以使用的属性:javaType,jdbctype的使用。
当数据为null的时候,有些数据库不能识别mybatis对null的使用。如oracle数据库;
在oracle的使用的时候,mybatis对所有的null映射的都是jdbctype为other的类型,所有会导致oracle不认识other类型,所以会报错,而mysql可以正常的使用null,因为mysql遵循sql规范比较好,所以可以正常的使用。
#{name,jdbcType=null}
如果oracle对字段有非空约束,即使你修改了mybatis的jdbctype=null 也无法使用null。
在日常建表的时候,可以将为null的字段默认设置为-1.这样就可以使用了。
解决方法:
-
每次写的sql语句中直接指定,jdbctype=null
-
在mybatis的配置文件中,指定对null的处理为
-
<Settings> <setting name="jdbcTypeForNull" value="NULL"/> <!--大小写转换,支持驼峰命名数据库和pojo字段的对应关系> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
3.select的使用(resultType)
1.返回一个map
key 就是表的列名,值就是对应的值;
返回值为map:
public Map<String,Object> getEmpByIdReturnMap(Integer id);
mapper文件如下:直接使用map,mybatis已经做过了别名。java.util.map对应的别名为map
<!-- 返回值为一个map -->
<select id="getEmpByIdReturnMap" resultType="map">
select id,last_name lastName ,gender,email from table_employee where id=#{id}
</select>
2.将返回值封装为map,map的key为id,value为数据库一行的数据,使用的@MapKey(“id”)注解指定
@MapKey("id")
public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);
mapper文件如下:
<select id="getEmpByLastNameLikeReturnMap" resultType="com.www.mybatis.bean.Employee">
select id,last_name lastName ,gender,email from table_employee where last_name like #{lastName}
</select>
4.resultMap
列名和pojo不对应,解决方法如下:
1.查询的时候。可以指定数据库列名的别名
2.开启驼峰命名法,自动适配。
3.使用resultMap
代码如下:
5 动态sql
1.if 标签和where标签的使用
<select id="getEmpByConditonIf1" resultType="com.www.mybatis.bean.Employee">
select id,last_name lastName,gender ,email , d_id dept from table_employee
where
1=1
<!-- 判断test里面的 el表达式很像,使用OGNL表达式 -->
<if test="id!=null">
and id=#{id}
</if>
<if test="lastName!=null and lastName!=''">
and last_name=#{lastName}
</if>
<if test="gender!=null and gender!=''">
and gender =#{gender}
</if>
<if test="email!=null and email!=''">
and email=#{email}
</if>
</select>
<select id="getEmpByConditonIf" resultType="com.www.mybatis.bean.Employee">
select id,last_name lastName,gender ,email , d_id dept from table_employee
<where>
<!-- 判断test里面的 el表达式很像,使用OGNL表达式 -->
<if test="id!=null">
id=#{id}
</if>
<if test="lastName!=null and lastName!=''">
and last_name=#{lastName}
</if>
<if test="gender!=null and gender!=''">
and gender =#{gender}
</if>
<if test="email!=null and email!=''">
and email=#{email}
</if>
</where>
</select>