0048 MyBatis动态SQL简易入门示例

假如有如下的关于书籍基本信息的表:

DROP DATABASE IF EXISTS `books`;
CREATE DATABASE `books`;
USE books;

DROP TABLE IF EXISTS `book`;

CREATE TABLE `book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) DEFAULT NULL,
  `author` varchar(64) DEFAULT NULL,
  `press` varchar(64) DEFAULT NULL,
  `douban` FLOAT DEFAULT NULL,
  `isbn` varchar(13) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

INSERT INTO `book` (`name`,`author`,`press`,`douban`,`isbn`) VALUES('Java编程思想 第4版','(美)Bruce Eckel著','机械工业出版社',9.1,'9787111213826');

INSERT INTO `book` (`name`,`author`,`press`,`douban`,`isbn`) VALUES('深入理解Java虚拟机 JVM高级特性与最佳实践','周志明著','机械工业出版社',8.8,'9787111421900');

INSERT INTO `book` (`name`,`author`,`press`,`douban`,`isbn`) VALUES('疯狂Java讲义 第3版','李刚著','电子工业出版社',7.8,'9787121236693');

INSERT INTO `book` (`name`,`author`,`press`,`douban`,`isbn`) VALUES('深入理解Java 7 核心技术与最佳实践','成富著','机械工业出版社',6.9,'9787111380399');

用户可能用书名、出版社、豆瓣评分、ISBN中的1~4个字段进行查询,如果用固定的sql语句,那么得写C(4,1)+C(4,2)+C(4,3)+C(4,4)=4+6+4+1=15条sql和15个Dao方法

如果用动态sql,一条语句即可完成,比如这样:

<select id="multiSearch" parameterType="map" resultType="net.sonng.entity.Book">
    SELECT * FROM book 
    <where>
        <if test="name!=null">
            name LIKE #{name}
        </if>
        <if test="press!=null">
            AND press LIKE #{press}
        </if>
        <if test="douban!=null">
            AND douban>=#{douban}
        </if>
        <if test="isbn!=null">
            AND isbn=#{isbn}
        </if>
    </where>
</select>

动态sql,可以根据用户对字段选择和输入,动态生成一条sql执行
实体类Book,略。
BookDao,略
Controller略
Service如下:

package net.sonng.service;

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

import javax.annotation.Resource;

import net.sonng.dao.BookDao;
import net.sonng.entity.Book;

import org.springframework.stereotype.Service;

@Service
public class TestService {
    @Resource
    private BookDao bookDao;
    public List<Book> multiSearch(String name,String press,String douban,String isbn){
        Map<String,String> map=new HashMap();
        if (name!=null && !name.equals("")) {
            map.put("name", "%"+name+"%");
        }
        if (press!=null && !press.equals("")) {
            map.put("press","%"+press+"%");
        }
        if (douban!=null && !douban.equals("")){
            map.put("douban", douban);
        }
        if (isbn!=null && !isbn.equals("")) {
            map.put("isbn", isbn);
        }
        return bookDao.multiSearch(map);
    }
}

测试类如下:

package net.sonng.test;

import java.util.List;

import net.sonng.controller.TestController;
import net.sonng.entity.Book;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args){
        ApplicationContext ac=new ClassPathXmlApplicationContext("ac.xml");
        TestController tc=ac.getBean("testController",TestController.class);//下面的4种查询语句,都可以一条动态SQL执行
        //List<Book> books=tc.multiSearch("Java", "", "", "");                //查询到4条
        //List<Book> books=tc.multiSearch("Java", "机械", "", "");            //3条
        //List<Book> books=tc.multiSearch("Java", "机械", "8", "");           //2条
        List<Book> books=tc.multiSearch("Java" ,"机械", "8", "9787111213826");//1条
        for (Book book:books){
            System.out.println(book.getId());
            System.out.println(book.getName());
            System.out.println(book.getIsbn());
            System.out.println(book.getAuthor());
            System.out.println(book.getPress());
            System.out.println(book.getDouban());
            System.out.println("----------------------------------");
        }
    }
}
posted @ 2017-03-23 11:20  sonng  阅读(337)  评论(0编辑  收藏  举报