mybatis mybatis实现模糊查询询 like a and like b怎么写

1138人阅读
JAVA编程(1)
Mybatis(2)
在使用mybatis写SQL时会遇见使用like的情况,如果是直接用sql写法会导致mybatis报错。
我的错误写法,见如下代码:
&select id=&getinfo_sql& parameterType=&map& resultMap=&CQ_output&&
&include refid=&col_checked&/&
FROM checkquestion_table
statuscode like &%#{statuscode}%&
ORDER BY modifytime desc
正确写法有很多种,一种是直接在代码里写成整个变量,另一种如下:
statuscode like &%&#{statuscode}&%&
传参数的方式和其它变量相同。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:171798次
积分:1084
积分:1084
排名:千里之外
原创:24篇
评论:43条
(1)(1)(11)(1)(2)(1)(1)(1)(3)(1)(4)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'mybatis分页及模糊查询功能实现
作者:forever_2h
字体:[ ] 类型:转载 时间:
这篇文章主要为大家详细为大家详细介绍了mybatis实现分页及模糊查询功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
mybatis中分页有3种方式来实现,通过sql语句(两种传参方式)来实现,通过mybatis 的 Rowbounds 来实现。
通过(自定义类型)传参 来实现分页:
映射文件:
&select id="findListBypage" parameterType="cn.wh.util.PageUtil" resultType="Role"&
select * from t_role limit #{index},#{size}
测试代码:
* 通过自定义类型来传参 实现分页功能 需要新建一个类型
public void testPage1(){
PageUtil pu = new PageUtil();
pu.setIndex(3);
pu.setSize(3);
List&Role& list = session.selectList("cn.wh.mapper.RoleMapper.findListBypage", pu);
for(Role r:list){
System.out.println(r.getName());
通过map传参实现:
映射文件:
&select id="findListBypage" parameterType="map " resultType="Role"&
select * from t_role limit #{index},#{size}
测试代码:
* 可以通过map来传参 这样可以不用新建新的类型
public void testPage2(){
Map&String,Integer& map = new HashMap&String,Integer&();
map.put("index", 0);
map.put("size", 3);
List&Role& list = session.selectList("cn.wh.mapper.RoleMapper.findListBypage", map);
for(Role r:list){
System.out.println(r.getName());
通过RowBounds来实现分页:
映射文件:
&select id="findAll" resultType="Role"&
select * from t_role
测试代码:
* 使用rowBounds来实现分页
public void testPage3(){
//第一个参数 是index,开始下标
//第二个参数 是size,每页显示记录数
RowBounds bounds = new RowBounds(3, 3);
List&Role& list = session.selectList("cn.wh.mapper.RoleMapper.findAll", null,bounds);
for(Role r:list){
System.out.println(r.getName());
注意:通常情况下使用 Map 传参来实现分页
映射文件:
&select id="selectLike" parameterType="string" resultType="Role"&
select *from t_role where name like #{name}
测试代码:
* 模糊查询
public void testLike1(){
List&Role& list = session.selectList("cn.wh.mapper.RoleMapper.selectLike","%会员");
for(Role r:list){
System.out.println(r.getName());
第二种方式:
&select id="selectLike1" parameterType="string" resultType="Role"&
select *from t_role where name like concat(#{name},'%');
测试代码:
* 模糊查询
public void testLike2(){
List&Role& list = session.selectList("cn.wh.mapper.RoleMapper.selectLike1","黄");
for(Role r:list){
System.out.println(r.getName());
注意:通常使用第二种方式实现模糊查询
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具今天需要用到mybatis 3.0.6的模糊查询, google了一下,很多人问问题,但是回答清楚的确没发现,以下是我项目中遇到问题的总结:
1. Mybatis sql语句的打印问题。 Mybatis本身集成了log4j,所以我们只需要在log4j的配置文件中改改就能让它打印出sql信息。在log4j.properties文件中加上:
log4j.logger.java.sql=debug,stdout
2. 模糊查询的问题。在Mapper文件中加上,
&select id="selectByExampleLike" parameterType="com.CLIENT" resultMap="BaseResultMap"&
CLIENT_ID, CLIENT_NAME, CLIENT_CODE, CLIENT_LNAME, CLIENT_FNAME, CLIENT_EMAIL, CLIENT_PASS,
CLIENT_ADDRESS
from CLIENT WHERE CLIENT_ID != ''
&if test="clientId != null"&
AND CLIENT_ID like CONCAT('%','${clientId}','%' )
&if test="clientName != null"&
AND CLIENT_NAME like CONCAT('%','${clientName}','%' )
&if test="clientCode != null"&
AND CLIENT_CODE like CONCAT('%','${clientCode}','%' )
&if test="clientLname != null"&
AND CLIENT_LNAME like CONCAT('%','${clientLname}','%' )
&if test="clientFname != null"&
AND CLIENT_FNAME like CONCAT('%','${clientFname}','%' )
&if test="clientEmail != null"&
AND CLIENT_EMAIL like CONCAT('%','${clientEmail}','%' )
&if test="clientPass != null"&
AND CLIENT_PASS like CONCAT('%','${clientPass}','%' )
&if test="clientAddress != null"&
AND CLIENT_ADDRESS like CONCAT('%','${clientAddress}','%' )
然后可以通过测试:
public class CLIENTServiceImplTest {
public static void main(String[] args) {
SqlSessionFactory factory =
factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("Configuration.xml"));
} catch (IOException e) {
e.printStackTrace();
SqlSession sqlSession = factory.openSession();
CLIENT example = new CLIENT();
example.setClientId(1);
example.setStart(2);
example.setLimit(3);
example.setOrderByClause("CLIENT_ID ASC");
List&CLIENT& list = sqlSession.getMapper(CLIENTMapper.class).selectByExampleLike(example);
for(CLIENT c : list){
System.out.println(c.getClientId()+"
"+c.getClientName());
System.out.println("------------------- list.size: " + list.size());
for(int i=0;i&10;i++){
list = sqlSession.getMapper(CLIENTMapper.class).selectByExample(example);
Configuration.xml:
&?xml version="1.0" encoding="UTF-8"?&
&!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"&
&configuration&
&environments default="myexample"&
&environment id="myexample"&
&transactionManager type="JDBC" /&
&dataSource type="POOLED"&
&property name="driver" value="com.mysql.jdbc.Driver" /&
&property name="url" value="jdbc:mysql://localhost:3306/test" /&
&property name="username" value="root" /&
&property name="password" value="root" /&
&/dataSource&
&/environment&
&/environments&
&mapper resource="coc/mg/dao/CLIENTMapper.xml" /&
&/mappers&
&/configuration&
好,模糊查询搞定!
浏览: 23438 次
来自: 武汉
weiqiulai 写道楼主,你好!有个问题,你的jms监控的 ...
楼主,你好!有个问题,你的jms监控的是另外一台服务器上的mq ...
额。。。很久没上来了。。都忘了这个没写完。。
楼主 期待后面的
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'做个备忘:&
xml特殊符号转义写法
& & & & & & & &&&
& & & & & & & && &
& & && & &&
& & & & & &&&
& & & & & &'
& & & & & &"
也可以使用&![CDATA[ ]]&符号进行说明,将此类符号不进行解析&
& & &![CDATA[ 这里写你的sql ]]& &
mysql like的写法
& & &like concat('%',#{param},'%') &或者&like '%${param}%' ,推荐使用前者,可以避免sql注入。
阅读(...) 评论()温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'Mybatis中模糊查询的各种写法',
blogAbstract:'原文地址:
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}}

我要回帖

更多关于 mybatis模糊查询 like 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信