继上一篇IBatis初体验
上一篇介绍了基本配置和一个简单的插入(其它RUD操作也类似)
本篇主要是对于一对多和多对一的基本使用
还是Userinfo 与 Score 双向一对多关系
javaBean:
public class Userinfo {
private int id;
private String userName;
private String pwd;
private int age;
private List<Score>socresList=new ArrayList<Score>();
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Score> getSocresList() {
return socresList;
}
public void setSocresList(List<Score> socresList) {
this.socresList = socresList;
}
}
public class Score {
private int id;
private int chinese;
private int english;
private Userinfo userinfo;
public Userinfo getUserinfo() {
return userinfo;
}
public void setUserinfo(Userinfo userinfo) {
this.userinfo = userinfo;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
sqlmap.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="UserInfo" >
<typeAlias alias="user" type="com.entity.Userinfo"/>
<typeAlias alias="score" type="com.entity.Score"/>
<insert id="abatorgenerated_insert" parameterClass="com.entity.Userinfo" >
<selectKey resultClass="int" type="post" keyProperty="id" >
select @@IDENTITY as value
</selectKey>
insert into UserInfo (userName, pwd, age)
values (#userName:VARCHAR#, #pwd:VARCHAR#, #age:INTEGER#)
</insert>
<resultMap id="UserinfoResult" class="user" >
<result column="id" property="id" />
<result column="userName" property="userName" jdbcType="VARCHAR" />
<result column="pwd" property="pwd" />
<result column="age" property="age" />
<result property="socresList" column="id" select="getScoreByUserId"/><!-- 一对多关系体现 -->
</resultMap>
<resultMap id="s" class="score" >
<result property="id" column="id"/>
<result property="chinese" column="chinese"/>
<result property="english" column="english"/>
<result property="userinfo" column="userId" select="getUserById"/><!-- 多对一关系体现 -->
</resultMap>
<select id="getScoreByUserId" parameterClass="int" resultMap="s">
select * from score where userId=#id#
</select>
<select id="getUserById" resultClass="user" parameterClass="int">
select * from userinfo where id=#userId#
</select>
<select id="getScore" resultMap="s">
select * from score
</select>
<select id="getUserInfo" resultMap="UserinfoResult">
select * from userinfo
</select>
</sqlMap>
Test类:
List list = mapClient.queryForList("getUserInfo");//查询用户列表
if (list.size() > 0) {
//取第一个用户
Userinfo userinfo = (Userinfo) list.get(0);
System.out.println("用户:" + userinfo.getUserName());
//取该用户的所有语文成绩
for (Score score : userinfo.getSocresList()) {
System.out.println("语文:" + score.getChinese());
}
} else
System.out.println("没有查到任何用户!");
//多对一体现
List list2=mapClient.queryForList("getScore");
if (list2.size()>0) {
Score score=(Score)list2.get(0);
System.out.println(score.getUserinfo().getUserName()+" 的语文成绩是:"+score.getChinese());
}
else{
System.out.println("无记录!");
}
完毕!