451、Java框架105 -【MyBatis - 多对一】 2020.12.24

目录

0、基于XML方式的多对一进行

1、多对一效果

2、CategoryMapper

3、ProductMapper

4、mybatis-config.xml

5、测试类

6、参考链接


 

0、基于XML方式的多对一进行

本知识点在XML方式的多对一的基础上,进行修改,以实现注解方式的一对多

1、多对一效果

451、Java框架105 -【MyBatis - 多对一】 2020.12.24

2、CategoryMapper

新增CategoryMapper接口,提供get方法

package com.how2java.mapper;
  
import org.apache.ibatis.annotations.Select;
 
import com.how2java.pojo.Category;
  
public interface CategoryMapper {
    @Select(" select * from category_ where id = #{id}")
    public Category get(int id);
     
}

3、ProductMapper

新增ProductMapper接口,提供list方法

package com.how2java.mapper;
  
import java.util.List;
 
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
 
import com.how2java.pojo.Product;
  
public interface ProductMapper {
    @Select(" select * from product_ ")
    @Results({ 
        @Result(property="category",column="cid",one=@One(select="com.how2java.mapper.CategoryMapper.get")) 
    })
    public List<Product> list();
}

4、mybatis-config.xml

在22,23行增加对CategoryMapper和ProductMapper的映射

<?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="com.how2java.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://localhost:3306/how2java?characterEncoding=UTF-8"/>
            <property name="username" value="root"/>
            <property name="password" value="admin"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/how2java/pojo/Category.xml"/>
        <mapper class="com.how2java.mapper.CategoryMapper"/> 
        <mapper class="com.how2java.mapper.ProductMapper"/> 
    </mappers>
</configuration>

5、测试类

运行测试类,看到如图所示效果

451、Java框架105 -【MyBatis - 多对一】 2020.12.24

package com.how2java;
   
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 com.how2java.mapper.ProductMapper;
import com.how2java.pojo.Product;
   
public class TestMybatis {
   
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
 
        List<Product> ps= mapper.list();
        for (Product p : ps) {
            System.out.println(p + "\t对应的分类是:\t" + p.getCategory().getName());
        }
 
        session.commit();
        session.close();
   
    }
}

6、参考链接

[01] How2j - MyBatis - 多对一

上一篇:springboot2.x整合tkmapper


下一篇:编译安装 gperftools