Mybatis中的SQL查询

Mybatis中的SQL查询问题:

1.简单查询。当传入参数用#{参数名}表示。

1 <select id="queryProductByName" parameterType="java.lang.String" resultMap="BaseResultMap">
2     select *
3     from products
4     where name = #{name}
5 </select>

2.模糊查询。

当传入参数为String类型,将具体参数名(如name)用_parameter替换。

此外,模糊查询的一种方式是,'%${_parameter}%'

1 <select id="queryProductByName" parameterType="java.lang.String" resultMap="BaseResultMap">
2     select *
3     from products
4     where name like '%${_parameter}%'
5 </select>

还可以使用SQL字符串拼接函数CONCAT。还有其他方式此不赘述。

1 <select id="queryProductByName" parameterType="java.lang.String" resultMap="BaseResultMap">
2     select *
3     from products
4     where name like CONCAT(CONCAT('%',#{name},'%'))
5 </select>

当传入参数为Object对象:

1 <select id="queryProductByCondition" parameterType="cn.com.mvc.model.Product" resultMap="BaseResultMap">
2     select * 
3     from products 
4     where name like '%${name}%' and detail like '%${detail}%'
5 </select>

 

posted @ 2019-07-25 14:57  糖人潘  阅读(491)  评论(0)    收藏  举报