假如有如下的关于书籍基本信息的表:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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,一条语句即可完成,比如这样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<select id= "multipSearch" 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如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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> multipSearch(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.multipSearch(map);
}
}
|
测试类如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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" );
//List<Book> books=tc.multipSearch("Java", "", "", ""); //查询到4条
//List<Book> books=tc.multipSearch("Java", "机械", "", ""); //3条
//List<Book> books=tc.multipSearch("Java", "机械", "8", ""); //2条
List<Book> books=tc.multipSearch( "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( "----------------------------------" );
}
}
}
|
以上所述是小编给大家介绍的史上最简单的MyBatis动态SQL入门示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/sonng/archive/2017/03/23/6604135.html