MyBatis SQL应用
来源:互联网 发布:mac 桌面壁纸存放路径 编辑:程序博客网 时间:2024/06/03 18:10
1.数据库使用xml文件中防止SQL注入,涉及模糊查询的语句
<if test="objectName!=null and objectName!=''"> AND OBJECT_NAME like concat(concat('%',#{objectName}),'%') </if>
2.
map和List,array相比,map是用K,V存储的,在foreach中,使用map时,index属性值为map中的Key的值。
因为map中的Key不同于list,array中的索引,所以会有更丰富的用法。
批量插入
<insert id="ins_string_string"> insert into string_string (key, value) values <foreach item="item" index="key" collection="map" open="" separator="," close="">(#{key}, #{item})</foreach></insert>
3.select的例子
<select id="sel_key_cols" resultType="int"> select count(*) from key_cols where <foreach item="item" index="key" collection="map" open="" separator="AND" close="">${key} = #{item}</foreach></select>
可以看到这里用key=value来作为查询条件,对于动态的查询,这种处理方式可以借鉴。一定要注意到$和#的区别,$的参数直接输出,#的参数会被替换为?,然后传入参数值执行。
DEBUG [main] - ==> Preparing: select count(*) from key_cols where col_a = ? AND col_b = ? DEBUG [main] - ==> Parameters: 22(Integer), 222(Integer)DEBUG [main] - <== Total: 1
4.foreach 循环LIST
- item:循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。
<select id="countByUserList" resultType="_int" parameterType="list">select count(*) from users <where> id in <foreach item="item" collection="list" separator="," open="(" close=")" index=""> #{item.id, jdbcType=NUMERIC} </foreach> </where></select>
注:select count(*) from users WHERE id in ( ? , ? )
5.foreach 循环数组
JAVA传入参数
public void testQuery() { ColInfoDao dao=(ColInfoDao)ctx.getBean("colInfoDao"); Map map = new HashMap(); map.put("userId", "tom"); map.put("password", "123"); String[] a = { "20000001", "20000002" }; map.put("classIds", Arrays.asList(a)); Object password = dao.query(map); System.out.println("password:" + password); Assert.assertEquals("123", password);}
XML文件使用
<select id="queryPasswordByUserId" parameterClass="java.util.Map" resultClass="java.lang.String"> <![CDATA[ select PASSWORD as password from T_S_P_USER ]]> <dynamic prepend="where"> <isNotEmpty prepend="AND" property="userId"> USER_ID=#userId# </isNotEmpty> <isNotEmpty prepend="AND" property="password"> PASSWORD=#password# </isNotEmpty> <isNotEmpty prepend="AND" property="classIds"> <iterate property="classIds" open="(" conjunction="OR" close=")"> CLASS_ID = #classIds[]# </iterate> </isNotEmpty> </dynamic></select>
0 0
- Mybatis应用:Sql Annotation
- MyBatis SQL应用
- Mybatis中sql语法应用
- 记录mybatis的sql语句应用
- MyBatis注解应用之动态SQL语句
- mybatis框架中动态sql的应用
- MyBatis注解应用之动态SQL语句
- mybatis学习笔记2-SQL的简单应用
- mybatis 应用
- mybatis 应用
- mybatis-sql
- Mybatis-sql
- mybatis sql
- sql mybatis
- Mybatis之动态sql语句中时间判断(if)的应用
- mybatis OGNL应用扩展
- Mybatis应用(oracle)
- mybatis分页应用
- amlogic平台android 系统linux内核中添加i2c设备实现i2c的读写
- Sourcetree配置GitHub远程代码库
- Hibernate框架下使用Gson遇到StackOverflowError时如何解决
- 如何将tomcat改为开发模式
- 使用FFmpeg实现视频的裁剪,不需要调用JNI实现
- MyBatis SQL应用
- 初来乍到的小菜鸟
- 简单工厂模式
- 多线程:Java内置锁与synchronized关键字
- Linux 下NFS挂载共享资源
- 数据库与文件进行数据存储有哪些区别?
- js、angularjs 在ng-repeat里用ng-if隐藏某个标签
- const分别在C和C++语言里的含义和实现机制
- Iterator模式