mybatis实现模糊查询,分页和别名配置的方法

  介绍

mybatis实现模糊查询,分页和别名配置的方法?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

<强>第一种

 select *等用户,用户名“%”;#{名称}“%”

<强>第二种

 select *等用户,用户名“% $ {value} %“

<强>第三种

& lt; !- - - concat拼接字符串mysql独有的函数——比;   select *从用户用户名像concat (“%”, # {username},“%”)

全网都这样说,但具体怎实现注入,我也不知道怎么搞。

    <李> <强> #{}是预编译处理,${}是字符串替换。 Mybatis在处理#{}时,会将sql中的#{}替换为,# 63;号,调用PreparedStatement的设置方法来赋值,李 <李> Mybatis在处理${}时,就是把${}替换成变量的值。 <李>使用#{}可以有效的防止sql注入,提高系统安全性。

<强>第一种方式:在mybatis-config.xml核心配置文件中的<代码> & lt; configuration> & lt;/configuration> 中添加:

& lt; typeAlias类型=癱om.lu.pojo.User"别名=癠ser"/祝辞

用户可以用在任何使用com.lu.pojo。用户的地方。

<强>第二种方式:
也可以指定一个包名,MyBatis会在包名下面搜索需要的Java Bean,比如:

& lt; typeAliases>   & lt;包名称=癱om.lu.pojo"/比;   & lt;/typeAliases>

在没有注解的情况下,会使用Bean的首字母小写的非限定类名来作为它的别名。比如com.lu.pojo。用户的别名为用户;若有注解,则别名为其注解值比。如:

@Alias (“user")   公开课用户{   …   }

问题:数据库中密码是pwd表示,实体类中是密码表示。查询结果会是null。

 mybatis实现模糊查询,分页和别名配置的方法

<强>第一种方式:sql语在句中用作为起别名。

& lt;选择id=癵etUserById"resultType=癱om.lu.pojo.User"比;   选择id、用户名,pwd作为密码的用户id=# {id}   & lt;/select>

<强>第二种方式:使用resultMap进行映射。

& lt; !——结果集映射——比;   & lt; resultMap id=皍serMap"类型=癱om.lu.pojo.User"比;   & lt; !——财产是实体类中的属性,列是数据库中的字段——比;   & lt;结果属性=皃assword"列=皃wd"/比;   & lt;/resultMap>      & lt;选择id=癵etUserById"resultMap=皍serMap"比;   选择id、用户名、pwd从用户id=# {id}   & lt;/select>

1,通过控制实现分页

映射器接口:

//使用限制实现分页   ListInteger> getUserByLimit (Map<字符串;地图),

映射器。xml中:

& lt;选择id=癵etUserByLimit"parameterType=癿ap"resultType=癱om.lu.pojo.User"比;   从用户限制#{抵消}select *, #{页大小}   & lt;/select>

测试方法:

@Test   公共空间getUserByLimit () {   SqlSession SqlSession=MybatisUtils.getSqlSession ();   usermap usermap=sqlSession.getMapper (UserMapper.class);      Integer> Map<字符串;地图=new HashMap<字符串,Integer> ();   map.put (“offset", 1);   map.put (“pageSize" 2);   List用户=userMapper.getUserByLimit(地图);   (用户用户:用户){   System.out.println(用户);   }   sqlSession.close ();   }

2,通过RowBounds实现分页

映射器接口:

//通过RowBounds实现分页
  ListgetUserByRowBounds (RowBounds RowBounds); 

映射器。xml中:

& lt;选择id=癵etUserByRowBounds"resultType=癱om.lu.pojo.User"比;   从用户选择*   & lt;/select>

测试方法:

 @Test
  公共空间getUserByRowBounds () {
  SqlSession SqlSession=MybatisUtils.getSqlSession ();
  usermap usermap=sqlSession.getMapper (UserMapper.class);
  
  RowBounds RowBounds=new RowBounds (1、2);
  List用户=userMapper.getUserByRowBounds (rowBounds);
  
  (用户用户:用户){
  System.out.println(用户);
  }
  sqlSession.close ();
  }

3,通过分页插件pagehelper实现分页

砰的一声。xml中导入依赖:

 & lt; dependency>
  & lt; groupId> com.github.pagehelper

mybatis实现模糊查询,分页和别名配置的方法