10-Mybatis参数的使用之五--通过Map传值-了解-不建议用

前一篇:09-Mybatis参数的使用之四–通过索引传值-了解
https://blog.csdn.net/fsjwin/article/details/109670182

使用map传参,也很简单,直接看栗子,了解即可,别用啊!

1. StudentDao

   /**
     *  map传值
     *
     */
    public List<Student> selectStudentByMap(Map map);

2. StudentDao.xml

    <!--Map传值-->
    <select id="selectStudentByMap"  resultType="com.yuhl.domain.Student">
        select id,name,email,age from student where name =#{name} or age =#{age}
    </select>

3. 测试

 @Test
    public void test7() {
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
        //可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
        Map map = new HashMap();
        map.put("name", "yuhl");
        map.put("age", 11);
        List<Student> studentList = studentDao.selectStudentByMap(map);
        studentList.forEach(stu -> System.out.println(stu));
    }

测试通过

Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1739876329.
==>  Preparing: select id,name,email,age from student where name =? or age =?
==> Parameters: yuhl(String), 11(Integer)
<==    Columns: id, name, email, age
<==        Row: 1003, yuhl, yuhl@163.com, 20
<==      Total: 1
Student{id=1003, name='yuhl', email='yuhl@163.com', age=20}

4. 总结

简单参数、@Param、对象传参掌握。后面这两个就不要掌握了。知道就好。权当玩玩就行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值