门户网站建设与开发,wordpress略缩图插件,外贸专用网站,建设企业网站公司价格映射文件的sql语句中 #{} 和 ${} 区别以及实现模糊查询
目录
sql 语句中的 #{}#{} 模糊查询错误用法#{} 实现模糊查询sql 语句中的 ${}${} 实现模糊查询#{} 与 ${} 对比sql 语句中的 #{}
表示一个占位符号#xff0c;通过 #{} 可以实现 preparedStatement 向占位符中设置值…映射文件的sql语句中 #{} 和 ${} 区别以及实现模糊查询
目录
sql 语句中的 #{}#{} 模糊查询错误用法#{} 实现模糊查询sql 语句中的 ${}${} 实现模糊查询#{} 与 ${} 对比sql 语句中的 #{}
表示一个占位符号通过 #{} 可以实现 preparedStatement 向占位符中设置值。自动进行 java 类型和 jdbc 类型转换。可以有效防止 sql 注入。可以接收简单类型值或 pojo 属性值。如果 parameterType 传输单个简单类型值#{} 括号中可以是任意名称。
#{} 模糊查询错误用法
在模糊查询中#{} 在以下用法是错误的查询名字中带有李字的人
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacemyTest!--根据用户名模糊查询客户--
select idqueryCustomerByName parameterTypeString resultTypecom.itlike.domain.CustomerSELECT * FROM customer WHERE cust_name LIKE %#{cust_name}%;
/select/mapper查询代码如下
public void test3(){SqlSession sqlSession MybatisUtils.openSession();ListCustomer customers sqlSession.selectList(queryCustomerByName, 李);for (Customer customer : customers) {System.out.println(customer);}sqlSession.close();
}由执行结果可见这样写的话运行时的 sql 语句如下
SELECT * FROM customer WHERE cust_name LIKE %?%传入的参数为 “李” 由于 #{} 会自动进行类型转换给 添加单引号替换成 ’ ’所以实际的 sql 语句如下
SELECT * FROM customer WHERE cust_name LIKE %李%很明显这个是错误的 sql 语句所以模糊查询时无法这么实现。
#{} 实现模糊查询
如果一定要用 #{} 实现模糊查询必须以下面的方式实现查询名字中带有李字的人
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacemyTest!--根据用户名模糊查询客户--
select idqueryCustomerByName parameterTypeString resultTypecom.itlike.domain.CustomerSELECT * FROM customer WHERE cust_name LIKE #{cust_name}
/select/mapper查询代码如下 Testpublic void test3(){SqlSession sqlSession MybatisUtils.openSession();ListCustomer customers sqlSession.selectList(queryCustomerByName, %李%);for (Customer customer : customers) {System.out.println(customer);}sqlSession.close();}运行效果成功查询出表中姓名有李的人并且封装成对象。 Preparing: SELECT * FROM customer WHERE cust_name LIKE ? Parameters: %李%(String)Columns: cust_id, cust_name, cust_profession, cust_phone, emailRow: 2, 李白, 刺客, 18977665521, libai163.comRow: 11, 李信, 战士, 13728964922, lixinqq.comTotal: 2
Customer{cust_id2, cust_name李白, cust_profession刺客, cust_phone18977665521, emaillibai163.com}
Customer{cust_id11, cust_name李信, cust_profession战士, cust_phone13728964922, emaillixinqq.com}sql 语句中的 ${}
表示拼接字符串不防 sql 注入。通过 ${} 可以将 parameterType 传入的内容拼接在 sql 中并且不进行 jdbc 类型转换。可以接收简单类型或 pojo 属性值。如果 parameterType 传输单个简单类型值${} 括号中只能是 value。
${} 实现模糊查询
用 ${} 实现模糊查询方法如下查询名字中带有李字的人
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacemyTest!--根据用户名模糊查询客户--
select idqueryCustomerByName parameterTypeString resultTypecom.itlike.domain.CustomerSELECT * FROM customer WHERE cust_name LIKE %${value}%;
/select/mapper查询代码如下
public void test3(){SqlSession sqlSession MybatisUtils.openSession();ListCustomer customers sqlSession.selectList(queryCustomerByName, 李);for (Customer customer : customers) {System.out.println(customer);}sqlSession.close();
}运行结果成功查询出表中姓名中有李的人并且封装成对象。· Preparing: SELECT * FROM customer WHERE cust_name LIKE %李%; Parameters: Columns: cust_id, cust_name, cust_profession, cust_phone, emailRow: 2, 李白, 刺客, 18977665521, libai163.comRow: 11, 李信, 战士, 13728964922, lixinqq.comTotal: 2
Customer{cust_id2, cust_name李白, cust_profession刺客, cust_phone18977665521, emaillibai163.com}
Customer{cust_id11, cust_name李信, cust_profession战士, cust_phone13728964922, emaillixinqq.com}#{} 与 ${} 对比
#{} 不可行方法
映射文件中的sqlSELECT * FROM customer WHERE cust_name LIKE %#{name}%; // 没有固定参数名可以不叫 name
传入参数李
运行时显示的sqlSELECT * FROM customer WHERE cust_name LIKE %?%;
实际的sqlSELECT * FROM customer WHERE cust_name LIKE %李%;#{} 可行方法
映射文件中的sqlSELECT * FROM customer WHERE cust_name LIKE #{name} // 没有固定参数名可以不叫 name
传入参数%李%
运行时显示的sqlSELECT * FROM customer WHERE cust_name LIKE ?
实际的sqlSELECT * FROM customer WHERE cust_name LIKE %李%;${} 可行方法
映射文件中的sqlSELECT * FROM customer WHERE cust_name LIKE %${value}%; // 参数名必须叫 value
传入参数李
运行时显示的sqlSELECT * FROM customer WHERE cust_name LIKE %李%;