mybatis(5)参数处理+语句查询

1、简单单个参数

简单类型包括:
● byte short int long float double char
● Byte Short Integer Long Float Double Character
● String
● java.util.Date
● java.sql.Date

总而言之就是 mybaits可以自动匹配参数类型,之后通过setXXX来注入。
我们也可以显示标注类型,省去mybatis的类型匹配。
比如 char 类 我们可以通过parameterType 来告诉mybatis 参数类型是什么,其他基本数据类型都一样,不在举例。

 <select id="selectbysex" resultType="student" parameterType="java.lang.Character">
        select *from t_student where sex=#{sex}
    </select>

2、Map参数

注意的是 我们传的如果是map,则我们#{map的key值},不能是其他的。

    <select id="selectBYmap" resultType="student">
        select * from t_student where name=#{namekey} and age=#{agekey}
    </select>
   @Test
    public  void testMap(){
   
   
        SqlSession sqlSession = MybatisUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Map<String,Object> map=new HashMap<>();
        map.put("namekey","cky");
        map.put("agekey",18);
        mapper.selectBYmap(map).forEach(student -> System.out.println(student));
        sqlSession.close();
    }

3、实体类参数

注意:如果我们传的是实体类,则#{},{}里应该是实体类的属性名,不能是其他。

 <select id="selectByclass" resultType="student">
        select * from t_student where name=#{name} and age=#{age}
    </select>
   @Test
    public  void testClass(){
   
   
        SqlSession sqlSession = MybatisUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student=new Student();
        student.setAge(18);
        student.setId(10L);
        student.setBirth(new Date());
        student.setHeight(1.65);
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值