let a = {o: "091"} a['8'] = "90" a[Symbol.iterator] = function* () { yield1; yield2; yield3; }; Object.getPrototypeOf(a).a = 898 let b = Object.create(a) Object.getPrototypeOf(b).a = 666 let car = newCar("ma","mo","ye") //Object.getOwnPropertyNames(Object.getPrototypeOf(a)) console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(car)));
functionshowProps(obj, objName) { var result = ""; for (var i in obj) { if (obj.hasOwnProperty(i)) { result += objName + "." + i + " = " + obj[i] + "\n"; } } return result; }
//字节流返回文件 ,也可以用common-io:common-io:2.6 这个库的IOUtils.copy(fileInput,resp.outputStream)替代 val fileInput = FileInputStream("本机文件路径") val bytes = ByteArray(1024) var len = 0 do { len = fileInput.read(bytes) if (len != -1) { resp.outputStream.write(bytes, 0, len) } } while (len != -1)
//mapper.xml SELECT * FROM tb_use WHERE username like #{ userName } and password like #{ password }
多条件查询优化
1.以上多条件查询时,可能存在后面参数password 不传参问题,那整个SQL就会变为SELECT * FROM tb_use WHERE username like #{userName} and password like ,查询就会出错, 可以用标签优化为
1 2 3 4 5 6 7
SELECT * FROM tb_use WHERE <if test="userName!=null and userName!='' "> username like #{userName} </if> <if test="password!=null and password!=''"> and password like #{password} </if>
2.此时如果出现第一个参数username不传仍然有问题,此时可以将所有条件都加上and开头然后 2.1 以一个恒等式 eg: where 1=1 <if test="userName!=null and userName!='' "> and username like #{userName}</if> 来避免 2.2 使用Mybatis的<where>标签实现2.1功能 即 <where> 1=1 <if test="userName!=null and userName!='' "> and username like #{userName}</if> <if>...</if></where>
动态单条件查询
choose (when,otherwise)选择类似switch
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<selectid="selectChooseParams"resultType="user"> SELECT * FROM tb_use where <choose> <whentest="userName!=null and userName!='' "> username like #{userName} <!--可以加and,<where>标签会动态去除--> </when> <whentest="password!=null and password!=''"> password like #{password} </when> </choose> </select>
同理以上语句当所有条件都不成立时会变成SELECT * FROM tb_use where,会出问题,解决方案
<updateid="update"> UPDATE tb_use <set> <iftest="userName!=null and userName!=''"> username = #{userName}, </if> <iftest="password!=null and password!=''"> password = #{password} </if> <iftest="age>0"> age = #{age} </if> </set> WHERE id = #{id} </update>
delete
1
fundeleteSome(@Param("ids") ids:List<Int>)
1 2 3 4 5 6 7 8
<deleteid="deleteSome"> DELETE FROM tb_use WHERE id in <!-- ids需要在接口中通过@Param("ids")手动指定, 否者collection参数类型,mybatis有自己定义key的规则,如list的key就是list,数组的key是array,而不是参数名--> <foreachcollection="ids"item="id"separator=","open="("close=")"> #{id} </foreach> </delete>
注解生成sql
eg:
1 2 3
@Delete("delete from tb_use where id=#{id}") fundelete(id: Int): Int //这样就不用在对应的mapper文件中增加标签