先上表
student表:
t_agetype表:
这里的表并没有建立主外键,没有建立主外建的表未必没有关联,个人认为主外建只是表关系的具体体现
定义StudentAd类作为student表的映射类(因为Student类之前用过,偷个懒):
public class StudentAd {
private int id;
private String name;
private int age;
private AgeType ageType;
public StudentAd() {
// TODO Auto-generated constructor stub
System.out.println("created success");
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public AgeType getAgeType() {
return ageType;
}
public void setAgeType(AgeType ageType) {
this.ageType = ageType;
}
@Override
public String toString() {
return "StudentAd [id=" + id + ", name=" + name + ", age=" + age
+ ", ageType=" +ageType.toString() + "]";
}
}
public class AgeType {
private int tage;
private String type;
public int getTage() {
return tage;
}
public void setTage(int tage) {
this.tage = tage;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "AgeType [tage=" + tage + ", type=" + type + "]";
}
}
定义StudentMapper接口和AgeTypeMapper接口:
public interface StudentMapper {
public StudentAd smfind(Integer id);
}
public interface AgeTypeMapper {
public AgeType amfind(int tage);
}
配置各自的映射文件:
AgeTypeMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czy.mappers.AgeTypeMapper">
<resultMap type="AgeType" id="ageType">
<id property="tage" column="tage"/>
<result property="type" column="type"/>
</resultMap>
<select id="amfind" parameterType="Integer" resultType="AgeType">
select *from t_agetype where tage=#{tage}
</select>
</mapper>
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czy.mappers.StudentMapper">
<resultMap type="StudentAd" id="studentad">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="ageType" select="com.czy.mappers.AgeTypeMapper.amfind" column="age"/><!--将AgeType类中的方法amfind的返回值赋给ageType,column为student表中的外键-->
</resultMap>
<select id="smfind" resultMap="studentad" parameterType="Integer">
select * from student t1,t_agetype t2 where t1.age=t2.tage and t1.id=#{id}
</select>
</mapper>
AgeTypeMapper.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czy.mappers.AgeTypeMapper">
<resultMap type="AgeType" id="ageType">
<id property="tage" column="tage"/>
<result property="type" column="type"/>
</resultMap>
<select id="amfind" parameterType="Integer" resultType="AgeType">
select *from t_agetype where tage=#{tage}
</select>
</mapper>
项目配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"/>
<typeAliases><!--设置包下的所有类名作为本类的别名-->
<package name="com.czy.bean"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.czy.mappers"/>
</mappers>
<!--自动加载该包下的所有xml文件-->
</configuration>
运行:
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import com.czy.bean.StudentAd;
import com.czy.factory.SessionFactoryUtil;
import com.czy.mappers.StudentMapper;
public class Tmain {
public static Logger logger=Logger.getLogger(Tmain.class);
public static void main(String[] args) {
SqlSession session=SessionFactoryUtil.openSession();//通过工具类获取SqlSession
StudentMapper studentMapper=session.getMapper(StudentMapper.class);
StudentAd one=studentMapper.smfind(1);
System.out.println(one);
System.out.println(one.getAgeType());
session.commit();
session.close();
}
}
结果:
完成!!
附上包图: