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); }