mybatis-参数传入,结果集返回

一、参数传入:从java代码中把数据传入到mapper文件的sql语句中。

1)parameterType : 写在mapper文件中的 一个属性。 表示dao接口中方法的参数的数据类型。
                                    它的值是java的数据类型权限定名称或mybatis定义的别名 

可以写可以不写,一般不写,因为mybatis通过反射可以获取参数的数据类型。

2) 一个简单类型的参数:
     简单类型:mybatis把java的基本数据类型和String都叫简单类型。
     在mapper文件获取简单类型的一个参数的值,使用 #{任意字符}

      接口:public Student  selectStudentById(Integer id) 
      mapper:select id,name, email,age from student where id=#{studentId}

3) 多个参数,使用@Param命名参数
      接口 public List<Student> selectMulitParam(@Param("myname") String name, @Param("myage") Integer age)
      
     mapper文件:
         <select>
             select * from student where name=#{myname} or age=#{myage}
          </select>

4) 多个参数,使用java对象
       语法 #{属性名}

        mapper文件:

<select>

select id,name,email,age from student where name=#{paramName}or age=#{paramAge}

</select>

二、结果集返回

1)列名和属性名一样

sql语句:select id ,name,email,age from student

2)列名和属性名不一样

1.取别名:

sql语句:select id as stuid,name as stuname,email as stu email,age as stuage from student

列名->属性名

id -> stuid        name -> stuname         email -> email        age -> stuage

2.使用resultMap

①定义resultMap

②在select标签,使用resultMap来引用 column:Map列名 property:java类型的属性名

<resultMap id="studentMap" type="com.yuchang.domain.Student">
        <!--主键用id标签-->
        <id column="id" property="id"></id>
        <!--其他用result-->
        <result column="name" property="name"></result>
        <result column="age" property="age"></result>
        <result column="email" property="email"></result>
    </resultMap>
    <select id="selectAllStudents" resultMap="studentMap">
        select id,name,email,age from student
    </select>

三、resultType取别名

<!--定义resultType别名
    1.typeAliases
        type;全限定名称
        alias:自定义别名
    2.package
         name:指定一个包包中的类名就是别名
    -->
    <typeAliases>
        <!--<typeAlias type="com.yuchang.domain.Student" alias="stu"/>-->
        <package name="com.yuchang.domain"/>
    </typeAliases>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Τhat‐ 诋调 丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值