各位老铁们好,相信很多人对java写网站源码分享都不是特别的了解,因此呢,今天就来为大家分享下关于java写网站源码分享以及java 门户网站源码的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!
用来循环容器的标签forEach,查看例子concat模糊查询choose(when,otherwise)标签selectKey标签if标签if+where的条件判断if+set实现修改语句if+trim代替where/set标签foreach
用来循环容器的标签forEach,查看例子
foreach元素的属性主要有item,index,collection,open,separator,close。
item:集合中元素迭代时的别名,index:集合中元素迭代时的索引open:常用语where语句中,表示以什么开始,比如以'(‘开始separator:表示在每次进行迭代时的分隔符,close常用语where语句中,表示以什么结束,
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
针对最后一条,我们来看一下官方说法:
注意你可以将一个List实例或者数组作为参数对象传给MyBatis,当你这么做的时候,MyBatis会自动将它包装在一个Map中并以名称为键。List实例将会以“list”作为键,而数组实例的键将是“array”。
所以,不管是多参数还是单参数的list,array类型,都可以封装为map进行传递。如果传递的是一个List,则mybatis会封装为一个list为key,list值为object的map,如果是array,则封装成一个array为key,array的值为object的map,如果自己封装呢,则colloection里放的是自己封装的map里的key值
//mapper中我们要为这个方法传递的是一个容器,将容器中的元素一个一个的\n//拼接到xml的方法中就要使用这个forEach这个标签了\npublicList<Entity>queryById(List<String>userids);\n//对应的xml中如下\n<selectid=”queryById”resultMap=”BaseReslutMap”>\nselect*FROMentity\nwhereidin\n<foreachcollection=”userids”item=”userid”index=”index”open=”(“separator=”,”close=”)”>\n{name},’%’))\n</if>\n</where>\n</select>\n
choose(when,otherwise)标签
choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行otherwise中的sql。类似于Java的switch语句,choose为switch,when为case,otherwise则为default。
例如下面例子,同样把所有可以限制的条件都写上,方面使用。choose会从上到下选择一个when标签的test为true的sql执行。安全考虑,我们使用where将choose包起来,放置关键字多于错误。
<!–choose(判断参数)-按顺序将实体类User第一个不为空的属性作为:where条件–>\n<selectid=”getUserList_choose”resultMap=”resultMap_user”parameterType=”com.yiibai.pojo.User”>\nSELECT*\nFROMUseru\n<where>\n<choose>\n<whentest=”username!=null”>\nu.usernameLIKECONCAT(CONCAT(‘%’,{sex,jdbcType=INTEGER}\n</when>\n<whentest=”birthday!=null”>\nANDu.birthday={studentId},\n{studentSex},\n{studentPhoto,javaType=byte[],jdbcType=BLOB,typeHandler=org.apache.ibatis.type.BlobTypeHandler},\n{placeId})\n</insert>\n
调用接口方法,和获取自动生成key
StudentEntityentity=newStudentEntity();\nentity.setStudentName(“黎明你好”);\nentity.setStudentSex(1);\nentity.setStudentBirthday(DateUtil.parse(“1985-05-28”));\nentity.setClassId(“20000001”);\nentity.setPlaceId(“70000001”);\nthis.dynamicSqlMapper.createStudentAutoKey(entity);\nSystem.out.println(“新增学生ID:”+entity.getStudentId());\n
if标签
if标签可用在许多类型的sql语句中,我们以查询为例。首先看一个很普通的查询:
<!–查询学生list,like姓名–>\n<selectid=”getStudentListLikeName”parameterType=”StudentEntity”resultMap=”studentResultMap”>\nSELECT*fromSTUDENT_TBLST\nWHEREST.STUDENT_NAMELIKECONCAT(CONCAT(‘%’,{studentName,jdbcType=VARCHAR}),’%’)\n</if>\n<iftest=”studentSex!=nullandstudentSex!=””>\nANDST.STUDENT_SEX={studentBirthday,jdbcType=DATE}\n</if>\n<iftest=”classId!=nullandclassId!=””>\nANDST.CLASS_ID={classEntity.classId,jdbcType=VARCHAR}\n</if>\n<iftest=”placeId!=nullandplaceId!=””>\nANDST.PLACE_ID={placeEntity.placeId,jdbcType=VARCHAR}\n</if>\n<iftest=”studentId!=nullandstudentId!=””>\nANDST.STUDENT_ID={studentName,jdbcType=VARCHAR}),’%’)\n</if>\n<iftest=”studentSex!=nullandstudentSex!=””>\nANDST.STUDENT_SEX={studentBirthday,jdbcType=DATE}\n</if>\n<iftest=”classId!=nullandclassId!=””>\nANDST.CLASS_ID={classEntity.classId,jdbcType=VARCHAR}\n</if>\n<iftest=”placeId!=nullandplaceId!=””>\nANDST.PLACE_ID={placeEntity.placeId,jdbcType=VARCHAR}\n</if>\n<iftest=”studentId!=nullandstudentId!=””>\nANDST.STUDENT_ID={studentName},\n</if>\n<iftest=”studentSex!=nullandstudentSex!=””>\nSTUDENT_TBL.STUDENT_SEX={studentBirthday},\n</if>\n<iftest=”studentPhoto!=null”>\nSTUDENT_TBL.STUDENT_PHOTO={classId}\n</if>\n<iftest=”placeId!=””>\nSTUDENT_TBL.PLACE_ID={studentId};\n</update>\n
if+trim代替where/set标签
trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。
trim代替where
<!–5.1if/trim代替where(判断参数)-将实体类不为空的属性作为where条件–>\n<selectid=”getStudentList_if_trim”resultMap=”resultMap_studentEntity”>\nSELECTST.STUDENT_ID,\nST.STUDENT_NAME,\nST.STUDENT_SEX,\nST.STUDENT_BIRTHDAY,\nST.STUDENT_PHOTO,\nST.CLASS_ID,\nST.PLACE_ID\nFROMSTUDENT_TBLST\n<trimprefix=”WHERE”prefixOverrides=”AND|OR”>\n<iftest=”studentName!=null”>\nST.STUDENT_NAMELIKECONCAT(CONCAT(‘%’,{studentSex,jdbcType=INTEGER}\n</if>\n<iftest=”studentBirthday!=null”>\nANDST.STUDENT_BIRTHDAY={classId,jdbcType=VARCHAR}\n</if>\n<iftest=”classEntity!=nullandclassEntity.classId!=nullandclassEntity.classId!=””>\nANDST.CLASS_ID={placeId,jdbcType=VARCHAR}\n</if>\n<iftest=”placeEntity!=nullandplaceEntity.placeId!=nullandplaceEntity.placeId!=””>\nANDST.PLACE_ID={studentId,jdbcType=VARCHAR}\n</if>\n</trim>\n</select>\n
trim代替set
<!–5.2if/trim代替set(判断参数)-将实体类不为空的属性更新–>\n<updateid=”updateStudent_if_trim”parameterType=”liming.student.manager.data.model.StudentEntity”>\nUPDATESTUDENT_TBL\n<trimprefix=”SET”suffixOverrides=”,”>\n<iftest=”studentName!=nullandstudentName!=””>\nSTUDENT_TBL.STUDENT_NAME={studentSex},\n</if>\n<iftest=”studentBirthday!=null”>\nSTUDENT_TBL.STUDENT_BIRTHDAY={studentPhoto,javaType=byte[],jdbcType=BLOB,typeHandler=org.apache.ibatis.type.BlobTypeHandler},\n</if>\n<iftest=”classId!=””>\nSTUDENT_TBL.CLASS_ID={placeId}\n</if>\n</trim>\nWHERESTUDENT_TBL.STUDENT_ID={classIds}\n</foreach>\n</select>\n
测试代码,查询学生中,在20000001、20000002这两个班级的学生:
@Testpublicvoidtest7_foreach(){String[]classIds={“20000001″,”20000002”};List<StudentEntity>list=this.dynamicSqlMapper.getStudentListByClassIds_foreach_array(classIds);for(StudentEntitye:list){System.out.println(e.toString());}}\n
2参数为list示例的写法接口的方法声明:
publicList<StudentEntity>getStudentListByClassIds_foreach_list(List<String>classIdList);\n
动态SQL语句:
<!–7.2foreach(循环List<String>参数)-作为where中in的条件–>\n<selectid=”getStudentListByClassIds_foreach_list”resultMap=”resultMap_studentEntity”>\nSELECTST.STUDENT_ID,\nST.STUDENT_NAME,\nST.STUDENT_SEX,\nST.STUDENT_BIRTHDAY,\nST.STUDENT_PHOTO,\nST.CLASS_ID,\nST.PLACE_ID\nFROMSTUDENT_TBLST\nWHEREST.CLASS_IDIN\n<foreachcollection=”list”item=”classIdList”open=”(“separator=”,”close=”)”>\n{orderId}\n</select>\n
END
来源:http://t.cn/EoEHulz
搜索微信号(ID:芋道源码),可以获得各种Java源码解析。
并且,回复【书籍】后,可以领取笔者推荐的各种Java从入门到架构的书籍。
来吧,骚年~
关于java写网站源码分享到此分享完毕,希望能帮助到您。
