共计 3865 个字符,预计需要花费 10 分钟才能阅读完成。
1、多表关系映射说明
1.1、一对一
@One 注解(一对一)
代替了标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@One 注解属性介绍:
select 指定用来多表查询的 sqlmapper
fetchType 会覆盖全局的配置参数 lazyLoadingEnabled
使用格式:
@Result(column=”“,property=””,one=@One(select=””))
1.2、一对多
@Many 注解(多对一)
代替了
使用格式:
@Result(property=””,column=””,many=@Many(select=””))
注意:
聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType(一般为 ArrayList)但是注解中可以不定义;
2、一对一
2.1、需求分析
查询学生信息,加载该学生的班级信息
2.2、编写 Classes.java 文件
import java.io.Serializable;
public class Classes implements Serializable {private int cid;
private String cname;
public int getCid() {return cid;
}
public void setCid(int cid) {this.cid = cid;
}
public String getCname() {return cname;
}
public void setCname(String cname) {this.cname = cname;
}
@Override
public String toString() {return "Classes{" +
"cid=" + cid +
", cname='" + cname + '\'' +
'}';
}
}
2.3、修改 Students.java 文件
private Classes classes;
public Classes getClasses() {return classes;
}
public void setClasses(Classes classes) {this.classes = classes;
}
2.4、修改 IStudentsDao.java 文件
@Select("select sid,sname,ssex,sage,cid as c_id from students")
@Results(id="studentsMap1",value={@Result(id=true,column = "sid",property = "sid"),
@Result(column = "sname",property = "sname"),
@Result(column = "ssex",property = "ssex"),
@Result(column = "sage",property = "sage"),
@Result(column = "c_id",property = "cid"),
@Result(column = "c_id",property = "classes",
one=@One(select = "com.tianyi.dao.IClassesDao.findById",fetchType = FetchType.EAGER))
})
List<Students> findAll1();
2.5、编写 IClassesDao.java 文件
import com.tianyi.javabean.Classes;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
public interface IClassesDao {@Select("select * from classes where cid=#{cid}")
@Results(id="classesMap",value={@Result(id=true,column = "cid",property = "cid"),
@Result(column = "cname",property = "cname")
})
Classes findById(int cid);
}
2.6、修改 MbStudentsTest.java 测试类
@Test
public void findAll1(){List<Students> list=studentsDao.findAll1();
for (Students s:list) {System.out.println(s);
//System.out.println(s+":"+s.getClasses());
}
}
3、一对多
3.1、需求分析
完成班级表与学生表的一对多的关系,查询出班级的信息和学生信息,学生信息只有在需要的时候才会显示。
3.2、编写 Classes.java 文件
private List<Students> studentsList;
public List<Students> getStudentsList() {return studentsList;
}
public void setStudentsList(List<Students> studentsList) {this.studentsList = studentsList;
}
3.3、编写 IClassesDao.java 文件
@Select("select * from classes")
@Results(id="classesMap1",value={@Result(id=true,column = "cid",property = "cid"),
@Result(column = "cname",property = "cname"),
@Result(column = "cid",property = "studentsList",many = @Many(select = "com.tianyi.dao.IStudentsDao.findById1",fetchType = FetchType.LAZY
))
})
List<Classes> findAll();
3.4、修改 IStudentsDao.java 文件
@Select("select * from students where cid=#{cid}")
Students findById1(int cid);
3.5、编写 MbClassesTest.java 文件
import com.tianyi.dao.IClassesDao;
import com.tianyi.dao.IStudentsDao;
import com.tianyi.javabean.Classes;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MbClassesTest {private InputStream in;
private SqlSession session;
private IClassesDao classesDao;
@Test
public void findAll(){List<Classes> list=classesDao.findAll();
for(Classes c:list){System.out.println(c);
}
}
@Before
public void init()throws IOException {in= Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
session=factory.openSession();
classesDao=session.getMapper(IClassesDao.class);
}
@After
public void destroy() throws IOException {session.commit();
session.close();
in.close();}
}
4、二级缓存的开启
4.1、修改 SqlMapConfig.xml 文件
<!-- 配置二级缓存 -->
<settings>
<!-- 开启二级缓存的支持 -->
<setting name="cacheEnabled" value="true"/>
</settings>
4.2、持久层使用二级缓存
@CacheNamespace(blocking = true)
public interface IClassesDao {
}
正文完
星哥玩云-微信公众号