mybatis一对多查询

时间:2022-06-14 09:35:23

建表 

建好的表和字段

Category表:

mybatis一对多查询


product表:

mybatis一对多查询

实体类 Category.java

在一的这端定义一个多端的一个List属性

package cn.pojo;

import java.util.List;

public class Category {

	private int id;
	private String name;
	List<Product> products;     // 在一的这端添加多的 List 类型的属性(一对多)

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	@Override
	public String toString() {
		return "Category [id=" + id + ", name=" + name+"]"  ;
	}	
}

实体映射文件 Category.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="cn.pojo">
    
    	<!-- type:对应实体类,id:可以随便取,下边sql查询语句用到,column是别名,property对应表的字段名 -->
    	<resultMap type="Category" id="CPBean">
    		<id column="cid" property="id" />
    		<result column="cname" property="name"/>
    		
    		<!-- 多对一的关系 -->
            <!-- property: 指的是属性名称, ofType:指的是属性的类型 -->
    		<collection property="products" ofType="Product">
    			<id column="pid" property="id"/>
    			<result column="pname" property="name"/>
    			<result column="price" property="price"/>
    		</collection>
    	</resultMap>
    	<select id="list" resultMap="CPBean">
    		select c.*,p.*,c.id cid,c.name cname,p.id pid,p.name pname,price
    		from Category c left join Product p on c.id=p.cid
    	</select>
    </mapper>


mybatis配置文件  mybatis-cfg,xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<!-- 配置数据源 -->
<configuration>
    <typeAliases>
      <package name="cn.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/student?characterEncoding=UTF-8"/>
                <property name="username" value="long"/>
                <property name="password" value="long"/>
            </dataSource>
        </environment>
    </environments>
    
    <!-- 在mybatis-config.xml中增加对于Product.xml的映射 -->
    <mappers>
        <mapper resource="cn/pojo/Category.xml"/> 
    </mappers>
</configuration>

测试类  NoeToMore.java

package cn.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.pojo.Category;
import cn.pojo.Product;

public class NoeToMore {

	public static void main(String[] args) throws IOException {
		String r="mybatis-cfg.xml";
		InputStream input=Resources.getResourceAsStream(r);
		SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(input);
		SqlSession session=sf.openSession();
		List<Category> cs=session.selectList("list");
		for(Category c : cs){
			System.out.println(c);
			List<Product> ps=c.getProducts();
			for(Product p:ps){
				System.out.println("\t"+p);
			}
		}
		session.commit();
		session.close();
	}
}

测试结果

mybatis一对多查询