1. 程式人生 > 實用技巧 >MyBatis註解之多對一

MyBatis註解之多對一

MyBatis註解之多對一

準備Mapper

  • CategoryMapper介面中追加get2方法:
    @Select(" select * from category_ where id = #{id}")
    public Category get2(int id);
點選檢視完整CategoryMapper

package com.nyf.mappers;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Many;

import com.nyf.pojo.Category;

public interface CategoryMapper {
    @Insert(" insert into category_ ( name ) values (#{name}) ") 
    public int add(Category category); 
        
    @Delete(" delete from category_ where id= #{id} ") 
    public void delete(int id); 
        
    @Select("select * from category_ where id= #{id} ") 
    public Category get(int id); 
      
    @Update("update category_ set name=#{name} where id=#{id} ") 
    public int update(Category category);  
        
    @Select(" select * from category_ ") 
    public List list(); 
    
    @Select(" select * from category_ ")
    @Results({ 
                @Result(property = "id", column = "id"),
                @Result(property = "products", javaType = List.class, column = "id", many = @Many(select = "com.nyf.mappers.ProductMapper.listByCategory") )
            })
    public List listOneToMany();
    
    @Select(" select * from category_ where id = #{id}")
    public Category get2(int id);    
}
  • ProductMapper中追加listManyToOne方法:
    @Select(" select * from product_ ")
    @Results({ 
        @Result(property="category",column="cid",one=@One(select="com.nyf.mappers.CategoryMapper.get2")) 
    })
    public List<Product> listManyToOne();
點選檢視完整ProductMapper

package com.nyf.mappers;
  
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.nyf.pojo.Product;
  
public interface ProductMapper {
  
    @Select(" select * from product_ where cid = #{cid}")
    public List listByCategory(int cid);
     
    @Select(" select * from product_ ")
    @Results({ 
        @Result(property="category",column="cid",one=@One(select="com.nyf.mappers.CategoryMapper.get2")) 
    })
    public List listManyToOne();
}

編寫測試類ManyToOne

點選檢視完整

package com.nyf.tests;

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.nyf.mappers.ProductMapper;
import com.nyf.pojo.Product;
public class ManyToOne {
	public static void main(String[] args) throws IOException {
        String resource = "com/nyf/config/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 ps= mapper.listManyToOne();
        for (Product p : ps) {
            System.out.println(p + "\t對應的分類是:\t" + p.getCategory().getName());
        }
        session.commit();
        session.close();
  
    }
}

結果驗證

Product [id=1, name=product a, price=88.88]	對應的分類是:	category1
Product [id=2, name=product b, price=88.88]	對應的分類是:	category1
Product [id=3, name=product c, price=88.88]	對應的分類是:	category1
Product [id=4, name=product x, price=88.88]	對應的分類是:	category2
Product [id=5, name=product y, price=88.88]	對應的分類是:	category2
Product [id=6, name=product zzzzzz, price=99.99]	對應的分類是:	category2