The problem of multiple parameter passing in mybatis

There are several ways to pass in parameters in the interface:

  • Pass a parameter
    interface
    //根据用户id查询
    Account selectById(Integer aid);

Interface xml

    <!--根据id查询-->
    <select id="selectById" resultType="Account">
        select * from account where aid=#{aid}
    </select>

Test code

 @Test
    public void selectById(){
    
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //动态创建接口的实现类
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        Account account = mapper.selectById(1);
        System.out.println(JSON.toJSONString(account,true));
    }

Test Results

==>  Preparing: select * from account where aid=? 
==> Parameters: 1(Integer)
<==    Columns: aid, aname, alias_name, age
<==        Row: 1, 李瑞, 瑞瑞, 18
<==      Total: 1

interface

//使用这种方式必须遵守约定  AccountMapper.xml中#{para1},#{param2}
    Account selectByName(String name,String alias);

Interface xml

<!--两个参数查询使用方式: aname=#{param1} and alias_name=#{param2}-->
    <select id="selectByName" resultType="Account">
        select * from account where aname=#{param1} and alias_name=#{param2}
    </select>

Test code

 @Test
    public void selectByName() {
    
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //动态创建接口的实现类
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        Account account = mapper.selectByName("李杰","杰伦");
        System.out.println(JSON.toJSONString(account, true));
    }

Test Results

==>  Preparing: select * from account where aname=? and alias_name=? 
==> Parameters: 李杰(String), 杰伦(String)
<==    Columns: aid, aname, alias_name, age
<==        Row: 4, 李杰, 杰伦, 21
<==      Total: 1
  • Two parameters use @Param
    interface
 //使用注解解决两个参数的问题
 Account selectByName2(@Param("aname") String name,@Param("alias_name") String alias);

Interface xml

 <!--两个参数查询使用方式:@Param-->
    <select id="selectByName2" resultType="Account">
        select * from account where aname=#{aname} and alias_name=#{alias_name}
    </select>

Test code

    @Test
    public void selectByName2() {
    
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //动态创建接口的实现类
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        Account account = mapper.selectByName2("李瑞", "瑞瑞");
        System.out.println(JSON.toJSONString(account, true));
    }

Test Results

==>  Preparing: select * from account where aname=? and alias_name=? 
==> Parameters: 李瑞(String), 瑞瑞(String)
<==    Columns: aid, aname, alias_name, age
<==        Row: 1, 李瑞, 瑞瑞, 18
<==      Total: 1
  • Map method to deal with the problem of too many parameters
    interface
    //使用Map方式处理参数过多的问题
    Account selectByName3(Map<String,Object> map);

Interface xml

    <!-- 使用map解决参数过多的问题-->
    <!-- #{}花括号里面的值要和map.put中key一一对应-->
    <select id="selectByName3" resultType="Account">
        select * from account where aname=#{aname} and alias_name=#{alias_name}
    </select>

Test code

 @Test
    public void selectByName3() {
    
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //动态创建接口的实现类
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("aname","李华");
        map.put("alias_name","华华");
        Account account = mapper.selectByName3(map);
        System.out.println(JSON.toJSONString(account, true));
    }

Test Results

==>  Preparing: select * from account where aname=? and alias_name=? 
==> Parameters: 李华(String), 华华(String)
<==    Columns: aid, aname, alias_name, age
<==        Row: 2, 李华, 华华, 27
<==      Total: 1
  • Object way to solve the problem of too many parameters
    interface
  //使用参数为对象的方式解决参数过多问题
    Account selectByName4(Account account);

Interface xml

    <select id="selectByName4" resultType="Account">
        select * from account where aname=#{aname} and alias_name=#{aliasname}
    </select>

Test code

    @Test
    public void selectByName4() {
    
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //动态创建接口的实现类
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        Account a = new Account();
        a.setAname("李智");
        a.setAliasname("智智");
        Account account = mapper.selectByName4(a);
        System.out.println(JSON.toJSONString(account,true));
    }

Test Results

==>  Preparing: select * from account where aname=? and alias_name=? 
==> Parameters: 李智(String), 智智(String)
<==    Columns: aid, aname, alias_name, age
<==        Row: 3, 李智,
  • Multiple objects are encapsulated in the
    entity class Entity class
public class QueryVo {
    
    
    private Account account;
    //等等
    public Account getAccount() {
    
    
        return account;
    }

    public void setAccount(Account account) {
    
    
        this.account = account;
    }
}

interface

   //现在有一种情况 如果要查询的是多张表那么就是需要将多张表的实体类封装到一个新的实体类中
    Account selectByName5(QueryVo queryVo);

Interface xml

 <!--使用QueryVo 封装实体类-->
    <select id="selectByName5" resultType="Account">
        select * from account where aname=#{account.aname} and alias_name=#{account.aliasname}
    </select>

Test code

 @Test
    public void selectByName5() {
    
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //动态创建接口的实现类
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        QueryVo queryVo = new QueryVo();
        Account a = new Account();
        a.setAname("李瑞");
        a.setAliasname("瑞瑞");
        queryVo.setAccount(a);
        Account account = mapper.selectByName5(queryVo);
        System.out.println(JSON.toJSONString(account,true));
    }

Test Results

==>  Preparing: select * from account where aname=? and alias_name=? 
==> Parameters: 李瑞(String), 瑞瑞(String)
<==    Columns: aid, aname, alias_name, age
<==        Row: 1, 李瑞, 瑞瑞, 18
<==      Total: 1

Guess you like

Origin blog.csdn.net/lirui1212/article/details/108590321