目录结构
import cn.tri.pojo.Clazz;
协同
- public interface ClazzMapper {
- public Clazz findClazzId(int id);
- }
|
- <?xml
version="1.0"
encoding="UTF-8"?><!--xxMapper.xml-->
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper
namespace="cn.tri.mapper.ClazzMapper">
-
<select
id="findClazzId"
parameterType="int"
resultMap="mapClazz">
- SELECT * FROM teacher t,student s,Clazz c where c.c_id = #{id} and c.c_id = s.cid and t.t_id = s.tid;
-
</select>
-
<resultMap
type="cn.tri.pojo.Clazz"
id="mapClazz">
-
<id
property="id"
column="c_id"/>
-
<result
property="name"
column="c_name"/>
-
<collection
property="list"
ofType="cn.tri.pojo.Student">
-
<id
property="id"
column="s_id"/>
-
<result
property="name"
column="s_name"/>
-
<association
property="teacher"
javaType="teacher">
-
<id
property="id"
column="t_id"/>
-
<result
property="name"
column="t_name"/>
-
</association>
-
</collection>
-
</resultMap>
- </mapper>
|
实体类
- package cn.tri.pojo;
- import java.util.List;
- public class Clazz {
- private
int id;
- private String name;
- private List<Student> 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<Student>
getList() {
- return list;
- }
- public
void
setList(List<Student> list) {
- this.list = list;
- }
- public
Clazz(int id, String name, List<Student> list) {
- super();
- this.id = id;
- this.name = name;
- this.list = list;
- }
- public
Clazz() {
- super();
- }
- @Override
- public String toString() {
- return
"Clazz [id="
+ id +
", name="
+ name +
", list="
+ list +
"]";
- }
- }
|
测试
- //测试
import java.io.IOException;
- import java.io.InputStream;
- 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.Before;
- import org.junit.Test;
- import cn.tri.mapper.ClazzMapper;
- import cn.tri.pojo.Clazz;
- public class ClazzTest {
- SqlSessionFactory factory = null;
- @Before
- public
void
init()
throws IOException{
- String url =
"mybaties-config.xml";
- InputStream is = Resources.getResourceAsStream(url);
- factory =
new
SqlSessionFactoryBuilder().build(is);
- System.out.println(factory);
- }
-
- @Test
- public
void
findClazzId(){
- SqlSession session = factory.openSession();
- String statement =
"cn.tri.mapper.ClazzMapper.findClazzId";
- Clazz clazz = session.selectOne(statement,
1);
- //session.clearCache();//清楚缓存
System.out.println(clazz);
- //session.close(); //关闭连接
}
- }
|