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>
serviceimpl 中
- Map map=new HashMap();
- map.put("name","张三");
- map.put("age","18");
- map.put("sex","男");
- map.put("idnumber","18888888888888");
- <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>
③ 当返回类型为 map时 (① 返回结果为String)
service:
2. 返回为sum时
*****
原因是,sum() 的结果是作为 java.math.BigDecimal 来处理的, 而他不能直接转换成 java.lang.Integer,所以报错。
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>等标签详解
- <!-- 查询学生list,姓名 -->
- <select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">
- SELECT * from userWHERE name=#{name}
- </select>
- <select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap">
- SELECT * from user
- <if test="name!=null and name!='' ">
- WHERE name=#{name}
- </if>
- </select>
- <!-- 查询学生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!= '' ">
- ANDsex= #{Sex}
- </if>
- <if test="Birthday!=null and Birthday!=''">
- AND Birthday= #{Birthday}
- </if>
- </where>
- </select>
- <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>
- <!-- 更新学生信息 -->
- <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>
- <!-- 更新学生信息 -->
- <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>
- <!-- 查询学生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>
- <!-- 更新学生信息 -->
- <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>
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。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());
- }
- <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>
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: