笔记52 Mybatis快速入门(三)

一、更多查询

1.模糊查询

修改Category.xml,提供listCategoryByName查询语句select * from category where name like concat('%',#{0},'%')。concat('%',#{0},'%') 这是mysql的写法,如果是oracle,写法是select * from category_ where name like '%'||#{0}||'%'。

1 <select id="listCategoryByName" parameterType="string" resultType="Category">
2         select * from   category  where name like concat('%',#{0},'%')
3 </select>
1     private void queryByName() throws IOException {
2         create();
3         List<Category> categories = session.selectList("listCategoryByName", "i");
4         for (Category category : categories) {
5             System.out.println(category.getName());
6         }
7         close();
8     }

2.多条件查询

结合前面的模糊查询,多一个限定id的条件。因为是多个参数,而selectList方法又只接受一个参数对象,所以需要把多个参数放在Map里,然后把这个Map对象作为参数传递进去。

 1     private void queryByNameId() throws IOException {
 2         create();
 3         Map<String, Object> params = new HashMap<String, Object>();
 4         params.put("id", 8);
 5         params.put("name", "xiao");
 6         List<Category> categories = session.selectList("listCategoryByNameId", params);
 7         for (Category category : categories) {
 8             System.out.println(category.getName());
 9         }
10         close();
11     }
1     <select id="listCategoryByNameId" parameterType="map"
2         resultType="Category">
3         select * from category where id>#{id} and name like
4         concat('%',#{0},'%')
5     </select>

二、Mybatis的关系映射

1.一对多(一个分类对应多个产品)

<1>创建产品表以及产品表对应的实体。

 1 package mybatis.pojo;
 2 
 3 public class Product {
 4     private int id;
 5     private String name;
 6     private float price;
 7 
 8     public int getId() {
 9         return id;
10     }
11 
12     public void setId(int id) {
13         this.id = id;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public float getPrice() {
25         return price;
26     }
27 
28     public void setPrice(float price) {
29         this.price = price;
30     }
31 
32     public String toString() {
33         return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
34     }
35 
36 }

<2>修改Category实体类,提供product集合。

 

 1 package mybatis.pojo;
 2 
 3 import java.util.List;
 4 
 5 public class Category {
 6     private int id;
 7     private String name;
 8     private List<Product> products;
 9 
10     public int getId() {
11         return id;
12     }
13 
14     public void setId(int id) {
15         this.id = id;
16     }
17 
18     public String getName() {
19         return name;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26     public List<Product> getProducts() {
27         return products;
28     }
29 
30     public void setProducts(List<Product> products) {
31         this.products = products;
32     }
33 
34     public String toString() {
35         return "Category [id=" + id + ", name=" + name + "]";
36     }
37 }

 

<3>修改Category.xml

通过left join关联查询,对Category和Product表进行关联查询。与前面学习的有所区别,这里不是用的resultType, 而是resultMap,通过resultMap把数据取出来放在对应的对象属性里。

注: Category的id字段和Product的id字段同名,Mybatis不知道谁是谁的,所以需要通过取别名cid,pid来区分,name字段同理。

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper
 3     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <mapper namespace="mybatis.pojo">
 7     <resultMap type="Category" id="categoryBean">
 8         <id column="cid" property="id" />
 9         <result column="cname" property="name" />
10 
11         <!-- 一对多关系 -->
12         <!-- property:指定的是集合属性的值,ofType:指的是集合中元素的类型 -->
13         <collection property="products" ofType="Product">
14             <id column="pid" property="id"></id>
15             <result column="pname" property="name" />
16             <result column="price" property="price" />
17         </collection>
18     </resultMap>
19     <!-- 关联查询分类和产品表 -->
20     <select id="listCategory" resultMap="categoryBean">
21         select c.*, p.*, c.id 'cid', p.id 'pid', c.name 'cname', p.name 'pname' from
22         category c left join product p on c.id = p.cid
23     </select>
24 </mapper>

 

<4>测试

 

 1 package mybatis.test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 import mybatis.pojo.Category;
12 import mybatis.pojo.Product;
13 
14 public class testOneToMany {
15 
16     public static void main(String[] args) throws IOException {
17         // TODO Auto-generated method stub
18         String resource = "mybatis-config.xml";
19         InputStream inputStream = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
20         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
21         SqlSession session = sqlSessionFactory.openSession();
22         List<Category> categories = session.selectList("listCategory");
23         for (Category c : categories) {
24             System.out.println(c);
25             List<Product> products = c.getProducts();
26             for (Product product : products) {
27                 System.out.println("\t" + product.toString());
28             }
29         }
30         session.commit();
31         session.close();
32     }
33 
34 }

 

2.多对一

3.多对多

猜你喜欢

转载自www.cnblogs.com/lyj-gyq/p/9230358.html
52