mybatis不同參數傳遞取值方式


1)傳單個基本數據類型或String對象,#{參數名};

2)傳遞的是單個引用數據類型對象,#{對象的屬性名} 直接獲取對象的屬性值;

3)傳遞多個對象時,需要在參數前添加@Param注解指定參數的別名。取值#{參數別名};

4)傳遞Map時,可以直接通過#{key}的形式獲取value;

5)傳遞List set 數組等集合。使用foreach標簽進行遍歷。

package com.seecen.mybatis3.mapper;

import com.seecen.mybatis3.pojo.Product;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface ProductMapper {
    int insert(Product product);
    int deleteById(Integer id);
    int update(Product product);
    Product selectById(Integer id);
    List<Product> selectAll();
    //查詢所有字段   orderColumn:指定排序列
    List<Product> selectALL(String orderColumn);
    //根據id或name查找數據 演示多個參數的取值方式,通過@Param取別名
    List<Product> selectByIdOrName(@Param("id") Integer id,@Param("name") String name);
    //參數傳遞的是Map時,#{key}取value
    List<Product> selectMap(Map<String,Object> map);
    //批量刪除  傳遞數組/集合
    int deleteByIds(Integer[] ids);
    //批量插入
    int batchInsert(List<Product>productList);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.seecen.mybatis3.mapper.ProductMapper">

    <insert id="insert" parameterType="Product">
           <selectKey keyProperty="id" order="BEFORE" resultType="integer">
                select p_pro.nextval from dual
           </selectKey>
        insert into Product(id,name) values(#{id},#{name})
    </insert>
    <update id="update" parameterType="Product">
        update Product set name = #{name} where id=#{id}
    </update>
    <delete id="deleteById">
        delete from Product where id=#{id}
    </delete>

    <select id="selectById" parameterType="integer" resultType="Product">
        select * from Product where id=#{id}
    </select>
    <select id="selectAll" resultType="Product">
        select * from Product
    </select>


    <select id="selectALL" resultType="Product">
        select * from Product order by ${value}
    </select>
    <select id="selectByIdOrName" resultType="Product">
        select * from Product where id=#{id} or name=#{name}
    </select>
    <select id="selectMap" resultType="Product" parameterType="java.util.Map">
        select * from Product
        <where>
           <if test="id!=null">
               id=#{id}
           </if>
           <if test="name!=null and name!=''">
               or name=#{name}
           </if>
        </where>
    </select>
    <delete id="deleteByIds">
        delete from Product where id in
        <foreach collection="array" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
    <insert id="batchInsert" parameterType="java.util.List">
        begin
           <foreach collection="list" item="product">
               insert into Product(id,name) values(p_pro.nextval,#{product.name});
           </foreach>
        commit;
        end;
    </insert>
</mapper>
@Test
    public void Test2() throws IOException {
        InputStream is=Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        ProductMapper mapper=sqlSession.getMapper(ProductMapper.class);
        //插入
        Product product2=new Product();
        product2.setName("油條");
        int count=mapper.insert(product2);
        System.out.println("插入記錄數:"+count);
        //刪除
        int i=mapper.deleteById(3);
        System.out.println("刪除記錄數:"+i);
        //查詢
        Product product=mapper.selectById(4);
        System.out.println(product);
        //修改
        product.setName("盼盼小面包");
        int j=mapper.update(product);
        System.out.println("修改記錄數:"+j);
        System.out.println(product);
        //查詢所有數據
        List<Product> products=mapper.selectAll();
        for (Product product1:products){
            System.out.println(product1);
        }
        sqlSession.commit();
        sqlSession.close();
        is.close();
    }
    @Test
    public void Test3() throws IOException {
        InputStream is=Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        ProductMapper mapper=sqlSession.getMapper(ProductMapper.class);
        //按指定序列查詢所有字段
        List<Product> products = mapper.selectALL("id");
        for (Product product:products){
            System.out.println(product);
        }
        //通過id或name查詢,演示多個參數的取值方式
        List<Product> products1 = mapper.selectByIdOrName(4, "汽水");
        for (Product product:products1){
            System.out.println(product);
        }
        //參數傳遞的是map時查詢
        HashMap<String,Object> map=new HashMap<>();
        map.put("id",4);
        map.put("name","汽水");
        List<Product> products2 = mapper.selectMap(map);
        for (Product product:products2){
            System.out.println(product);
        }
        //批量刪除
        int i = mapper.deleteByIds(new Integer[]{26,27});
        System.out.println(i);
        //批量增加
        List<Product> products3 = Arrays.asList(
                new Product("魚干"),
                new Product("小魚"),
                new Product("貓咪"));
        int i1 = mapper.batchInsert(products3);
        System.out.println(i1);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM