这次主要学习关于一对多关系的相关思维模式以及代码,具体步骤如下所示(有些知识点需要结合上一个博客):
(1)在entity类中新建Tclass类,相关代码如下所示:
package com.baidu.lmj.entity;
import java.util.List;
public class TClass {
private int id;
private String cname;
private List<TStudent> stu;
public List<TStudent> getStu() {
return stu;
}
public void setStu(List<TStudent> stu) {
this.stu = stu;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
2.在dao层新建TClass.java接口,并写上查询class的抽象方法,代码如下所示:
package com.baidu.lmj.dao;
import java.util.List;
import com.baidu.lmj.entity.TClass;
public interface TClassDao {
public List<TClass> SelectAllTCalss();
}
3.在entity层中新建Tclass.xml解析文件,进行数据库查询以及映射配置,代码如下所示(两种方法哦):
<mapper namespace="com.baidu.lmj.dao.TClassDao">
<resultMap type="com.baidu.lmj.entity.TClass" id="map01">
<id column="cid" property="id"/>
<result column="cname" property="cname"/>
<collection property="stu" ofType="com.baidu.lmj.entity.TStudent">
<id column="sid" property="id"></id>
<result column="username" property="username"/>
<result column="cid" property="cid"/>
</collection>
</resultMap>
<select id="SelectAllTCalss" resultMap="map01">
select a.id AS sid,username,cid,cname FROM t_class b INNER JOIN t_student a ON b.id=a.cid
</select>
<!--另外一种-->
<resultMap type="com.baidu.lmj.entity.TClass" id="map02">
<id column="id" property="id"/>
<result column="cname" property="cname"/>
<collection property="stu" ofType="com.baidu.lmj.entity.TStudent" column="id" select="stu01">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="cid" property="cid"/>
</collection>
</resultMap>
<select id="SelectAllTClass1" resultMap="map02">
select * from t_class
</select>
<select id="stu01" parameterType="int" resultType="com.baidu.lmj.entity.TStudent">
select * from t_student where cid=#{id}
</select>
</mapper>
4.在dao中定义一个TClassDao.java类,代码如下所示:
package com.baidu.lmj.biz;
import java.io.IOException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.baidu.lmj.dao.TClassDao;
import com.baidu.lmj.entity.TClass;
import com.baidu.lmj.util.SessionFactory;
public class TClassBiz {
TClassDao dao;
public List<TClass> getAllTclass(){
List<TClass> list = null;
try {
SqlSession session=SessionFactory.getSession();
dao=session.getMapper(TClassDao.class);
list=dao.SelectAllTClass1();
SessionFactory.ClosedSession(session);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
5.在主方法中执行biz操作,操作方式如下所示:
package com.baidu.lmj.Main;
import java.util.List;
import com.baidu.lmj.biz.TClassBiz;
import com.baidu.lmj.biz.TStudentBiz;
import com.baidu.lmj.dao.TClassDao;
import com.baidu.lmj.entity.TClass;
import com.baidu.lmj.entity.TStudent;
public class Main {
public static void main(String[] args) {
// TStudentBiz biz=new TStudentBiz();
//
// List<TStudent> list=biz.getAllTStudent();
//
// for(TStudent t:list)
// {
// System.out.println(t.getId()+" "+t.getUsername()+" "+t.getTclass().getId()+"("+t.getTclass().getCname()+")");
// }
TClassBiz biz=new TClassBiz();
List<TClass> list=biz.getAllTclass();
for(TClass t:list)
{
System.out.println(t.getId()+" "+t.getCname());
for(TStudent stu:t.getStu())
{
System.out.println("--->"+stu.getUsername());
}
}
}
}