Mybatis传入参数栗子

Mybatis 传入参数栗子:

1.dao层,只需要写接口类

public interface IPersonDao {

    //1、传递单个参数 方法一 注解方式  对象返回:{"id":1,"name":"hahahha","addr":"jiangci","company":"xinzhii1"}
    public Person findByid(@Param("id") Integer id);
    //1、传递单个参数 方法二 这里没有用注解 所以.xml文件的<select>标签需要加上parameterType属性
    public Person findByid1(Integer id);
    //2.传递多个参数 方法一  List 集合返回(有中括号) :[{"id":3,"name":"wx2","addr":"jiangci2","company":"xinzhii12"}]
    public List<Person> findPerByidAndNa(@Param("id")Integer id,@Param("name")String name);
    //2.传递多个参数 方法二 在对应的.xml文件中需要写#{0}
    public List<Person> findPerByidAndNa1(Integer id,String name);
    //3.传递多个参数之Map集合封装
    public Map findPerByMap(HashMap map);

    //4.传递多个参数之List集合封装  [{"id":1,"name":"hahahha","addr":"jiangci","company":"xinzhii1"},{"id":2,"name":"wuxia","addr":"jiangci1","company":"xinzhii11"}]
    public List findPerByList(List<Integer> list);
}

2.Person.xml 文件

  <!--1、传递单个参数 方法一 -->
   <select id="findByid" resultType="com.wx.springbootdemo.entity.Person">
            select * from tbl_person where id = #{id}
   </select>
   </select>
    <!--1、传递单个参数 方法二 -->
    <select id="findByid1" parameterType="java.lang.Integer" resultType="com.wx.springbootdemo.entity.Person" >
            select * from tbl_person where id = #{id}
   </select>
    <!-- 2.传递多个参数 方法一  多个参数不能写parameterType 参数-->
    <select id="findPerByidAndNa" resultType="com.wx.springbootdemo.entity.Person">
            select * from tbl_person where id = #{id} and name=#{name}
   </select>
    <!-- 2.传递多个参数 方法二  -->
    <select id="findPerByidAndNa1" resultType="com.wx.springbootdemo.entity.Person">
            select * from tbl_person where id = #{0} and name=#{1}
   </select>

    <!--3.传递多个参数之Map集合封装-->
    <!--haspmap 是mybatis 自己配置好的,直接使用就行,map中的key对应#{}中的值-->
    <select id="findPerByMap" parameterType="java.util.HashMap" resultType="java.util.HashMap">
            select * from tbl_person where id = #{id1} and name=#{name1}
   </select>
    <!--4.传递多个参数之List集合封装-->
    <!--foreach 最后的效果是select 字段... from XXX where id in ('1','2')-->
    <select id="findPerByList" resultType="com.wx.springbootdemo.entity.Person">
            select * from tbl_person where id in
            <foreach item ="item" index ="index" collection="list" open="(" separator="," close=")">
                #{item}
            </foreach>
    </select>

3.使用@Select 注解:

//使用了@Mapper注解 ,就不需要再写.xml文件
@Mapper
public interface IEmployeeDao {
    @Select("select * from tbl_emp e where e.name=#{name} and e.id=#{id}")
    public List<Employee> findEmpByName(@Param("name") String name,@Param("id") Integer id);

    /*@Select("select *from tbl_emp e where e.id in <foreach collection='ids' " +
            "item = 'item' open = '(' separator = ',' close=')' > #{item} </foreach>")*/
   /* 注意:
    1,@Select后面的括号包含大括号
    2, 使用<script>标签
    3,@Select后面大括号中的代码,每行后面使用逗号结束*/
    @Select({
            "<script>",
            "select *from tbl_emp e where e.id in ",
            "<foreach collection='ids' item = 'item' open = '(' separator = ',' close=')'> ",
            "#{item}",
            "</foreach>",
            "</script>"
            })
    public List<Employee> findEmpByIds(@Param("ids") List<Long> ids);
}

结果如下所示:
方法一的结果:
在这里插入图片描述
方法二的结果:
在这里插入图片描述
4.上面的代码演示了Mybatis 的两种接口绑定方式:
一种是:在xml文件写sql
一种是:注解绑定,即通过在Mapper接口(dao层接口)中直接写注解@Select,@Update等注解,简单的sql查询 ,使用注解比较方便

5.使用MyBatis的mapper接口调用时有哪些要求?
① Mapper接口方法名和mapper.xml中定义的每个sql的id相同;
② Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同;
③ Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同;
④ Mapper.xml文件中的namespace即是mapper接口的类路径(完整的类路径名)。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值