JavaEE中用eclipse学习Mybatis查询的快速上手(2)
文章目录
Mybatis查询上手教程2
知识回顾
之前没有看过教程1的小伙伴,可以点击下面的连接进行学习!
JavaEE中用eclipse学习Mybatis查询的快速上手(1)
一、必要的java类
请注意一下:如果在上一篇博文写了的java类,在这篇博文会有一些数据更新了
1.BookStorage
package com.ppx.mybatis.domian;
import java.util.List;
public class BookStorage {
private String bookbarcode;
private String bookid;
private String bookintime;
private String bookstatus;
private List<Reader> readerlist;
public String getBookbarcode() {
return bookbarcode;
}
public void setBookbarcode(String bookbarcode) {
this.bookbarcode = bookbarcode;
}
public String getBookid() {
return bookid;
}
public void setBookid(String bookid) {
this.bookid = bookid;
}
public String getBookintime() {
return bookintime;
}
public void setBookintime(String bookintime) {
this.bookintime = bookintime;
}
public String getBookstatus() {
return bookstatus;
}
public void setBookstatus(String bookstatus) {
this.bookstatus = bookstatus;
}
public List<Reader> getReaderlist() {
return readerlist;
}
public void setReaderlist(List<Reader> readerlist) {
this.readerlist = readerlist;
}
@Override
public String toString() {
return "BookStorage [bookbarcode=" + bookbarcode + ", bookid=" + bookid + ", bookintime=" + bookintime
+ ", bookstatus=" + bookstatus + ", readerlist=" + readerlist + "]";
}
}
2.BookBorrow
package com.ppx.mybatis.domian;
public class BookBorrow {
private String borrowid;
private String bookbarcode;
private String readerid;
private String borrowtime;
private String returntime;
private String borrowstatus;
public String getBorrowid() {
return borrowid;
}
public void setBorrowid(String borrowid) {
this.borrowid = borrowid;
}
public String getBookbarcode() {
return bookbarcode;
}
public void setBookbarcode(String bookbarcode) {
this.bookbarcode = bookbarcode;
}
public String getReaderid() {
return readerid;
}
public void setReaderid(String readerid) {
this.readerid = readerid;
}
public String getBorrowtime() {
return borrowtime;
}
public void setBorrowtime(String borrowtime) {
this.borrowtime = borrowtime;
}
public String getReturntime() {
return returntime;
}
public void setReturntime(String returntime) {
this.returntime = returntime;
}
public String getBorrowstatus() {
return borrowstatus;
}
public void setBorrowstatus(String borrowstatus) {
this.borrowstatus = borrowstatus;
}
@Override
public String toString() {
return "BookBorrow [borrowid=" + borrowid + ", bookbarcode=" + bookbarcode + ", readerid=" + readerid
+ ", borrowtime=" + borrowtime + ", returntime=" + returntime + ", borrowstatus=" + borrowstatus + "]";
}
}
3.Reader
package com.ppx.mybatis.domian;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class Reader {
private String readerid;
private String readername;
private String readerpass;
private int retypeid;
private String readerdate;
private String readerstatus;
private List<BookBorrow> bookBorrowlist;
private List<BookStorage> bookstoragelist;
public List<BookBorrow> getBookBorrowlist() {
return bookBorrowlist;
}
public void setBookBorrowlist(List<BookBorrow> bookBorrowlist) {
this.bookBorrowlist = bookBorrowlist;
}
public String getReaderid() {
return readerid;
}
public void setReaderid(String readerid) {
this.readerid = readerid;
}
public String getReadername() {
return readername;
}
public void setReadername(String readername) {
this.readername = readername;
}
public String getReaderpass() {
return readerpass;
}
public void setReaderpass(String readerpass) {
this.readerpass = readerpass;
}
public int getRetypeid() {
return retypeid;
}
public void setRetypeid(int retypeid) {
this.retypeid = retypeid;
}
public String getReaderdate() {
return readerdate;
}
public void setReaderdate(String readerdate) {
this.readerdate = readerdate;
}
public String getReaderstatus() {
return readerstatus;
}
public void setReaderstatus(String readerstatus) {
this.readerstatus = readerstatus;
}
public List<BookStorage> getBookstoragelist() {
return bookstoragelist;
}
public void setBookstoragelist(List<BookStorage> bookstoragelist) {
this.bookstoragelist = bookstoragelist;
}
@Override
public String toString() {
return "Reader [readerid=" + readerid + ", readername=" + readername + ", readerpass=" + readerpass
+ ", retypeid=" + retypeid + ", readerdate=" + readerdate + ", readerstatus=" + readerstatus
+ ", bookBorrowlist=" + bookBorrowlist + ", bookstoragelist=" + bookstoragelist + "]";
}
}
二、XML配置文件即元素说明
BookBorrowMapper.xml
<?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.ppx.mybatis.mapper.BookBorrowMapper">
<!-- <foreach>元素使用 -->
<select id="findBookBorrowByBorrowids" parameterType="List"
resultType="BookBorrow"
>
select * from bookborrow where borrowid in
<foreach item="borrowid" index="index" collection="list"
open="(" separator="," close=")">
#{borrowid}
</foreach>
</select>
<!-- <bind>元素使用:根据读者编号进行模糊查询 -->
<select id="findBookBorrowByReaderId" parameterType="BookBorrow"
resultType="BookBorrow"
>
<bind name="pattern_readerid" value="'%'+_parameter.getReaderid()+'%'"/>
select * from bookborrow where readerid like #{pattern_readerid}
</select>
</mapper>
1.foreach元素
item:写你#{xxx}那里的xxx 如 #{borrowid} ;item="borrowid";
index:写迭代次数,可以不写,也可以写,写的话建议写index;
collection:写接收的类型;
open:写开始的包裹的符号;
close:写结束的包裹符号;
separator:写分格符号。
2.bind元素
name:写#{xxx}里面的xxx 如:#{pattern_readerid};name="pattern_readerid";
value:写传递过来的参数也可以直接写它变量名如:_parameter.getReaderid()=readerid.
3.一对多查询
ReaderMapper.XML(增加的配置)
<!-- 一对一嵌套结果查询 -->
<select id="findReaderWithBookBorrow" parameterType="String"
resultMap="ReaderWithBookBorrow"
>
SELECT bookborrow.*,reader.readername
FROM reader, bookborrow
WHERE reader.readerid = bookborrow.readerid
AND reader.readerid=#{readerid}
</select>
<resultMap type="Reader" id="ReaderWithBookBorrow">
<id property="readerid" column="readerid"/>
<result property="readername" column="readername"/>
<collection property="bookBorrowlist" ofType="BookBorrow">
<id property="borrowid" column="borrowid"/>
<result property="borrowstatus" column="borrowstatus"/>
</collection>
</resultMap>
<!-- 嵌套查询多对多 -->
<select id="findReaderByReaderid" parameterType="String" resultType="Reader">
SELECT * FROM reader WHERE readerid IN(
SELECT readerid FROM bookborrow WHERE bookbarcode=#{bookbarcode}
);
</select>
Mybatis就是通过collection元素来处理一对多关联关系
collection与association元素大部分相同(association在上篇文章介绍过这里就不再介绍了)
其中ofType属性与javaType属性对应
4.多对多查询
BookStorageMapper.xml
<?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.ppx.mybatis.mapper.BookStorageMapper">
<!-- 嵌套查询多对多 -->
<select id="findBookStorageMapperWhitReader1" parameterType="String"
resultMap="BookStorageMapperWhitReader1"
>
select *from bookstorage where bookbarcode=#{bookbarcode}
</select>
<resultMap type="BookStorage" id="BookStorageMapperWhitReader1">
<id property="bookbarcode" column="bookbarcode"/>
<result property="bookid" column="bookid"/>
<collection property="readerlist" column="bookbarcode" ofType="Reader"
select="com.ppx.mybatis.mapper.ReaderMapper.findReaderByReaderid">
</collection>
</resultMap>
<!-- 嵌套结果查询多对多 -->
<select id="findBookStorageMapperWhitReader2" parameterType="String"
resultMap="BookStorageMapperWhitReader2"
>
select bs.*,r.readername
from reader r,bookstorage bs,bookborrow bb
where r.readerid=bb.readerid
and bs.bookbarcode=bb.bookbarcode
and bs.bookbarcode=#{bookbarcode}
</select>
<resultMap type="BookStorage" id="BookStorageMapperWhitReader2">
<id property="bookbarcode" column="bookbarcode"/>
<result property="bookid" column="bookid"/>
<collection property="readerlist" ofType="Reader">
<id property="readerid" column="readerid"/>
<result property="readername" column="readername"/>
</collection>
</resultMap>
</mapper>
三、测试类及配置文件增加的代码
MybatisTest
//测试<foreach>元素
@Test
public void findBookBorrowByBorrowidsTest() {
SqlSession session = MybatisUtils.getSession();
List<String> ids = new ArrayList<String>();
ids.add("001328");
ids.add("001356");
List<BookBorrow> bookBorrows= session.selectList(
"com.ppx.mybatis.mapper.BookBorrowMapper.findBookBorrowByBorrowids",ids);
for (BookBorrow bookBorrow : bookBorrows) {
System.out.println(bookBorrow);
}
session.close();
}
//测试<bind>元素
@Test
public void findBookBorrowByReaderIdTest() {
SqlSession session = MybatisUtils.getSession();
BookBorrow bookBorrow = new BookBorrow();
bookBorrow.setReaderid("4");
List<BookBorrow> bookBorrows= session.selectList(
"com.ppx.mybatis.mapper.BookBorrowMapper.findBookBorrowByReaderId",bookBorrow);
for (BookBorrow bookBorrow2 : bookBorrows) {
System.out.println(bookBorrow2);
}
session.close();
}
//一对多查询
@Test
public void findReaderWithBookBorrowTest() {
SqlSession session = MybatisUtils.getSession();
Reader reader = session.selectOne(
"com.ppx.mybatis.mapper.ReaderMapper.findReaderWithBookBorrow", "0042");
System.out.println(reader);
session.close();
}
//多对多嵌套查询
@Test
public void findBookStorageMapperWhitReaderTest1() {
SqlSession session = MybatisUtils.getSession();
BookStorage bookStorage = session.selectOne(
"com.ppx.mybatis.mapper.BookStorageMapper.findBookStorageMapperWhitReader1", "190232");
System.out.println(bookStorage);
session.close();
}
//多对多嵌套结果查询
@Test
public void findBookStorageMapperWhitReaderTest2() {
SqlSession session = MybatisUtils.getSession();
BookStorage bookStorage = session.selectOne(
"com.ppx.mybatis.mapper.BookStorageMapper.findBookStorageMapperWhitReader2", "190232");
System.out.println(bookStorage);
session.close();
}
mybatis-config.xml
<!--配置Mapper的位置 -->
<mappers>
<mapper resource="com/ppx/mybatis/mapper/BookTypsMapper.xml" />
<mapper resource="com/ppx/mybatis/mapper/BookMapper.xml" />
<mapper resource="com/ppx/mybatis/mapper/ReaderMapper.xml" />
<mapper resource="com/ppx/mybatis/mapper/BookBorrowMapper.xml"/>
<mapper resource="com/ppx/mybatis/mapper/BookStorageMapper.xml"/>
</mappers>
总结
查询就介绍到这里了
String a =“继续学习,使我们的知识更加丰富。”;
System.out.printf("%s",a);