Mybatis、Mapper.xml中的#{}可以放什么

在mybatis中基于xml配置sql语句,总有疑惑,#{}是什么,条件查询的时候mapper接口传的参数该怎么放,自己总结了一下

我们在使用mapper代替dao开发的时候、可以不用自己实现接口了、由动态代理自动生成、但是要求对应的mapper.xml的

namespace 为mapper接口的限定名(完整包名加接口名) 接口的方法名对应到sql语句的标签的id上去,在这样在扫面mapper的时候才能找到对应的mapper和sql(我这里用的是ssm)

1.#{}其实就是一个占位符,当使用单条件操作时候、#{}里面可以放任何东西、mybatis

 例如根据id查询:select * from  where id =#{任意东西xxoo} 因为只有一个参数 不需要区分

2.当使用多条件查询的时候:可以多个参数封装为一个类、也可以封装成Map、也可以用参数的索引

其中:如果封装为对象:#{}里面的参数要和对象的 属性对应

  如果为map:map<key,value> 其中#{}要和key值对应 (见下面mapper.xml的示例)

如果不封装   、要使用多少个参数,就传多少个参数,例如:

List<Book> findBookIndex(String name,int number); 此时在#{}中就可以写参数的索引来区别 分别是 0 1....以此类推 

下面分别来演示集中操作:

实体类:省略了其他方法


public class Book {
    //图书id
    private   String bookId;
    //图书名
    private String bookName;
    //图书作者
    private String bookAuthor;
    //出版社
    private String bookPublish;
    //出版日期
    private String bookPublishTime;
    //价格
    private double bookPrice;
    //库存
    private int bookNumber;

 mapper接口:

public interface IMaintainBookMapper {
    //显示全部信息
    List<Book> findBookAll() throws Exception;

    //根据书名模糊查寻图书信息
    List<Book> findBook(String name) throws Exception;
    //封装为Book对象
    List<Book> findBookTest(Book book);
    //封装为map
    List<Book> findBookMap(Map<String,Object> map);
    //不封装 用参数索引
    List<Book> findBookIndex(String name,int number);

测试类:


@RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:spring/applicationContext.xml")
    public class IMaintainBookMapperTest {
        @Autowired
        IMaintainBookMapper iMaintainBookMapper;

        @Before
        public void setUp() throws Exception {
            System.out.println("==========开始测试===================");
        }

        @After
        public void tearDown() throws Exception {
            System.out.println("=============测试结束===================");
        }


    //多条件封装成类查询
        @Test
        public void findBookTest() {
            Book book = new Book();
            book.setBookName("西游记");
            book.setBookAuthor("jayson");
            book.setBookId("1001");
            List<Book> bookList = iMaintainBookMapper.findBookTest(book);
            for (Book book1 : bookList) {
                System.out.println(book1);
            }
        }
  //多条件封装成map查询
        @Test
        public void findBookMap() {
            Map<String , Object> map = new HashMap<>();
            map.put("name","西游记");
            map.put("number",5);
            List<Book> bookList =iMaintainBookMapper.findBookMap(map);
            for (Book book1 : bookList) {
                System.out.println(book1);
            }
        }
//多条件,不封装,根据参数索引查询 第一个为0 第二个为 1......
        @Test
        public void findBookIndex() {
            List<Book> bookList =iMaintainBookMapper.findBookIndex("西游记",5);
            for (Book book1 : bookList) {
                System.out.println(book1);
            }
        }
}

mapper.xml


    <!--多条件用封装对象、#{对象属性}-->
    <select id="findBookTest" resultMap="BookList">
        select * from book_information
        <where>
            id = #{bookId} and number > #{bookNumber}
        </where>
    </select>

    <!--多条件用map封装、#{键值}-->
    <select id="findBookMap" resultMap="BookList">
        select * from book_information
        <where>
           name = #{name} and number >#{number}
        </where>
    </select>
    <!-多条件查询-#{索引}-->
    <select id="findBookIndex" resultMap="BookList">
        select * from book_information
        <where>
            name = #{0} and number >#{1}
        </where>
    </select>

猜你喜欢

转载自blog.csdn.net/qq_41975950/article/details/81571041