06-Mybatis参数的使用之一--简单参数的使用

上一篇:05-2-Mybatis番外篇:复制一个maven module到另一个module https://blog.csdn.net/fsjwin/article/details/109667706

参数怎么传入到sql语句中的呢?底层就是jdbc原始的做法,使用占位符的方式。

1. 简单参数

  1. dao代码:
    /**
     *  按id查询学生
     *
     */
    public Student selectStudentById(Integer id);
  1. StudentDao.xml代码
  <!--parameterType="int 可以不写
    mybatis内置类型:https://mybatis.org/mybatis-3/zh/configuration.html#typeAliases

    #{id} 可以写成任意字符比如: #{dfsdf}
    -->
    <select id="selectStudentById" parameterType="int" resultType="com.yuhl.domain.Student">
        select id,name,email,age from student where id =#{dfsdf}
    </select>
  1. parameterType=“int” 可以写也可以不写
    可以参考:https://mybatis.org/mybatis-3/zh/configuration.html#typeAliases

  2. #{id}
    #{id} 可以写成任意字符比如: #{dfsdf} 这个里面可以写任意字符、任意字符、任意字符记住了。

2. 测试通过:

 @Test
    public void test3() throws IOException {
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
        //StudentDao studentDao = new StudentDaoImpl();
        Student student = studentDao.selectStudentById(1001);
        System.out.println(student);
    }

结果:

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 278934944.
==>  Preparing: select id,name,email,age from student where id =?
==> Parameters: 1001(Integer)
<==    Columns: id, name, email, age
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
Student{id=1001, name='张三', email='zhangsan@qq.com', age=20}

3. 总结

简单参数的使用一定要掌握的。也很简单

下一篇:07-Mybatis参数的使用之二–多个参数的使用@Param https://blog.csdn.net/fsjwin/article/details/109669306

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值