您的位置:首页 > 其它

maybatis 各种零散知识总结(传参,返回值,标签等)

2018-01-19 17:17 218 查看
1. MyBatis的传入参数parameterType类型分两种

1. ①. 基本数据类型:int,string,long,Date;

1. ②. 复杂数据类型:类和map
(parameterType="java.util.HashMap")

③. 如果传参为 数组,List (parameterType="java.util.ArrayList"
一般用于for each标签 下面将会介绍到)

像1。这种基本传参类型 我就不说了 。下面简单介绍一下②

一般遇到传如多个参数的时候 一般会选择会新建个实体 或者 创个map

1.1 实体传参 (假设参数的实体为 User 路径为:com.vanke.Entity.User 传入四个参数 (name,age,sex,idnumber))

serviceimpl 中 一般用 实体.set 设置属性就可以了

User user=new User();

user.setName('张三');



user.setAge(18);



user.setSex('男');

user.setIdnumber('1888888888');

一般 mapper .xml文件配置为

<select id="queryCarMakerList" resultMap="BaseResultMap" parameterType="com.vanke.Entity.User">

select

*

from common_car_make cm

where 1=1

<if test="name!= null">

and cm.name= #{name,jdbcType=DECIMAL}

</if>

<if test="age!= null">

and cm.name = #{carDeptName,jdbcType=VARCHAR}

</if>

<if test="sex!= null">

and cm.sex = #{carMakerName,jdbcType=VARCHAR}

</if>

<if test="idnumber
!= null" >

and cm.idnumber = #{hotType,jdbcType=number}

</if>

ORDER BY cm.id

</select>

2. map集合传参(传入四个参数 (name,age,sex,idnumber))

serviceimpl 中

Map map=new HashMap();

map.put("name","张三");

map.put("age","18");

map.put("sex","男");

map.put("idnumber","18888888888888");

mapper .xml文件配置为

<select id="queryCarMakerList" resultMap="BaseResultMap" parameterType="map">

select

*

from common_car_make cm

where 1=1

<if test="name!= null">

and cm.name= #{name,jdbcType=DECIMAL}

</if>

<if test="age!= null">

and cm.name = #{carDeptName,jdbcType=VARCHAR}

</if>

<if test="sex!= null">

and cm.sex = #{carMakerName,jdbcType=VARCHAR}

</if>

<if test="idnumber
!= null" >

and cm.idnumber = #{hotType,jdbcType=number}

</if>

ORDER BY cm.id

</select>

2.MyBatis的返回参数类型分两种

1.1. resultMap :(自己指定返回的参数 )

① 先定义一个<resultMap id 即为 resultMap 的命名>

<resultMap id="HouseResultMap" type="com.vankeHouse">
<id property="id" column="id"/>
<result property="dev_code" column="dev_code"/>
<result property="dev_name" column="dev_name"/>
<result property="pro_code" column="pro_code"/>


② 设置返回类型 resultMap=“HouseResultMap”

<select id="getHouserRoom" parameterType="int" resultType="HouseResultMap">
SELECT <include refid="getHouserR"/> from house where id=#{id}
</select>


1.2. resultType :int,string,long,实体

如果有的小伙伴要问 要返回 List 怎么办

1. 如果返回 List<String> 返回类型为 String
即可

2. 如果返回 List<Entity> 返回类型为
实体 即可

3 . mybatis中#和$绑定参数的区别
以及用途

① #()方式能够很大程度防止sql注入。

② $方式无法防止Sql注入。

默认情况下,使用#{}
格式的语法会导致MyBatis创建预处理语句属性并以它为背景设置安全的值(比如?)。这样做很安全,很迅速也是首选做法,有时你只是想直接在SQL语句中插入一个不改变的字符串。比如,像ORDER BY,你可以这样来使用:

ORDER BY ${columnName}

#{}
将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #{id},如果传入的值是111,那么解析成sql时的值为order by “111”,
如果传入的值是id,则解析成的sql为order by “id”。 (sql报错)

${}
将传入的数据直接显示生成在sql中。如:order by

${id},如果传入的值是111,那么解析成sql时的值为order by 111, 如果传入的值是id,则解析成的sql为order

by id。


4.Mybatis中 常用的<if><where><foreach><set><choose>等标签详解

sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息。这是我们可以使用动态sql,增加一个判断,当参数不符合要求的时候,我们可以不去判断此查询条件。

一个很普通的查询:

<!-- 查询学生list,姓名 -->

<select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from user
WHERE name=#{name}

</select>

但是此时如果name是null或空字符串,此语句很可能报错或查询结果为空。此时我们使用if动态sql语句先进行判断,如果值为null或等于空字符串,我们就不进行此条件的判断。

一。<if>标签:

<select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from user

<if test="name!=null and name!='' ">

WHERE
name=#{name}

</if>

</select>

由于参数是Java的实体类,所以我们可以把所有条件都附加上,使用时比较灵活, new一个这样的实体类,我们需要限制那个条件,只需要附上相应的值就会where这个条件,相反不去赋值就可以不在where中判断。

二 。<where > <if> 标签组合 (适合多个 if 判断的)
①:第一种写法

<!-- 查询学生list,like姓名,=性别、=生日、=班级,使用where,参数entity类型 -->

<select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from user

<where>

<if test="name!=null and name!='' ">

name=#{name}

</if>

<if test="Sex!= null and
Sex!= '' ">

AND
sex= #{Sex}

</if>

<if test="Birthday!=null
and
Birthday!=''">

AND Birthday= #{Birthday}

</if>

</where>

</select>

② 第二种写法 (加个 where 1=1)

<select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from user

where 1=1

<if test="name!=null and name!='' ">

AND name=#{name}

</if>

<if test="Sex!= null and
Sex!= '' ">

AND sex= #{Sex}

</if>

<if test="Birthday!=null
and Birthday!=''">

AND Birthday= #{Birthday}

</if>

</select>

三. <set> <if> 标签组合 (一般用于更新语句)

一个常见的更新代码:( 没有使用if标签时,如果有一个参数为null,都会导致错误)

[html] view
plain copy

<!-- 更新学生信息 -->

<update id="updateStudent" parameterType="StudentEntity">

UPDATE STUDENT_TBL

SET STUDENT_TBL.STUDENT_NAME = #{studentName},

STUDENT_TBL.STUDENT_SEX = #{studentSex},

STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},

STUDENT_TBL.CLASS_ID = #{classEntity.classID}

WHERE STUDENT_TBL.STUDENT_ID = #{studentID};

</update>

使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值。

<!-- 更新学生信息 -->

<update id="updateStudent" parameterType="StudentEntity">

UPDATE STUDENT_TBL

<set>

<if test="studentName!=null and studentName!='' ">

STUDENT_TBL.STUDENT_NAME = #{studentName},

</if>

<if test="studentSex!=null and studentSex!='' ">

STUDENT_TBL.STUDENT_SEX = #{studentSex},

</if>

<if test="studentBirthday!=null ">

STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},

</if>

<if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">

STUDENT_TBL.CLASS_ID = #{classEntity.classID}

</if>

</set>

WHERE STUDENT_TBL.STUDENT_ID = #{studentID};

</update>

四。<tirm>标签 ( trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果)

where例子的等效trim语句:

<!-- 查询学生list,like姓名,=性别 -->

<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from STUDENT_TBL ST

<trim prefix="WHERE" prefixOverrides="AND|OR">

<if test="studentName!=null and studentName!='' ">

ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')

</if>

<if test="studentSex!= null and studentSex!= '' ">

AND ST.STUDENT_SEX = #{studentSex}

</if>

</trim>

</select>

set例子的等效trim语句:

<!-- 更新学生信息 -->

<update id="updateStudent" parameterType="StudentEntity">

UPDATE STUDENT_TBL

<trim prefix="SET" suffixOverrides=",">

<if test="studentName!=null and studentName!='' ">

STUDENT_TBL.STUDENT_NAME = #{studentName},

</if>

<if test="studentSex!=null and studentSex!='' ">

STUDENT_TBL.STUDENT_SEX = #{studentSex},

</if>

<if test="studentBirthday!=null ">

STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},

</if>

<if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">

STUDENT_TBL.CLASS_ID = #{classEntity.classID}

</if>

</trim>

WHERE STUDENT_TBL.STUDENT_ID = #{studentID};

</update>

* 五。 <choose >(when, otherwise) 标签

有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。

if是与(and)的关系,而choose是或(or)的关系

案例 1:

<!-- 根据usertype 决定执行哪条sql 语句>

<select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">

<where>

<choose>

<when test="usertype
== 1">

select * from user where usertype=1

</when>

<when test="usertype
== 2 ">

select
* from user where usertype=2

</when>

<when test="usertype
== 3">

select
* from user where usertype=3

</when>

<when test="usertype
== 4 ">

select
* from user where usertype=4

</when>

<otherwise>

</otherwise>

</choose>

</where>

</select>

案例 2:

<!--
根据usertype 决定按照什么分组><select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from STUDENT_TBL ST

<where>

<choose>

<when test="usertype
== 1">

group by age

</when>

<when test="usertype
== 2">

group by
name

</when>

<when test=""usertype
== 3">

group by sex

</when>

<otherwise>

</otherwise>

</choose>

</where>

</select>

六。 <foreach> 标签 (对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。

List 实例将使用“list”做为键,数组实例以“array” 做为键。)

① 当传入list的时候

一。Dao接口 代码写法

public List<StudentEntity> getStudentListByClassIDs(List<String> classList);

测试代码,查询学生中,在20000002、20000003这两个班级的学生:

二。 java代码 主要是往 list 里放点数据

List<String> classList = new ArrayList<String>();

classList.add("20000002");

classList.add("20000003");

List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList);

for( StudentEntity entityTemp : studentList){

System.out.println(entityTemp.toString());

}

三。mapper

<select id="getStudentListByClassIDs" resultMap="studentResultMap">

SELECT * FROM STUDENT_TBL ST

WHERE ST.CLASS_ID IN

<foreach collection="list" item="classList"
index="index" open="(" separator="," close=")">

#{classList}

</foreach>

</select>

单独传入list时,foreach中的collection必须是list,不不管变量的具体名称是什么。比如这里变量名为idList,

collection却是是list。

② 当传入 数组 的时候

一。Dao接口 代码写法

public List<StudentEntity> getStudentListByClassIDs(String[] ids);

测试代码,查询学生中,在20000002、20000003这两个班级的学生:

二。 java代码 主要是往 数组 里放点数据

String[] ids = new String[2];

ids[0] = "20000002";

ids[1] = "20000003";

List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(ids);

for( StudentEntity entityTemp : studentList){

System.out.println(entityTemp.toString());

}

三。 mapper

<select id="getStudentListByClassIDs" resultMap="studentResultMap">

SELECT * FROM STUDENT_TBL ST

WHERE ST.CLASS_ID IN

<foreach collection="array" item="ids"
index="index" open="(" separator="," close=")">

#{ids}

</foreach>

</select>

单独传入数组时,foreach中的collection必须是array,不不管变量的具体名称是什么。比如这里变量名为ids,

collection却是是array

③ 当传入多个list的时候

Dao里面的写法:

public List<HistorySelect> findAll(@Param("list1")List<Integer> zphids,@Param("list2")List<String> zphbatchNums);



mapper:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: